onRollOver - onRollOut problem in Actionscript 2

I encountered many times the situation in which I had multiple nested movieclips and had assigned functions to onRollOver and onRollOut events for parent and child movieclips. The problem with that is when you define a function for a onRollOver / onRollOut event for a parent movieclip, all other functions defined for child movieclips are overwritten (meaning they are ignored).

I could only find one workaround for that so far.

So let’s say you’re having this situation:

var parent:MovieClip = createEmptyMovieClip("parent", 0);
var child:MovieClip = parent.createEmptyMovieClip("child", 0);
parent.onRollOver = function () {
create_something();
}
parent.onRollOut = function () {
destroy_something();
}
child.onRollOver = function() {
execute_something();
}
child.onRollOut = function() {
execute_something_else();
}

 

As frustrating this could be, execute_something() and execute_something_else() will not be executed.

So, the first thing I did, was to tell Flash that “child” is not the son of “parent” anymore (but they are brothers from now on):

var parent:MovieClip = createEmptyMovieClip("parent", 0);
var child:MovieClip = createEmptyMovieClip("child", 1);

 

In this way, all functions will work.

But now, when I’m rolling over child, Flash interprets I’m rolling out parent - and that’s why I nested them in the first place, so when you’re rolling over child, you’re rolling over parent too. Like this, destroy_something() will be executed when I’m rolling over child, and I don’t want to do that, because, at least theoretically, I’m still rolling over parent. So I’ll just simulate this, once the movieclips are no longer nested.

var rollOverParent:Boolean = false;
parent.onRollOver = function () {
create_something();
}
parent.onRollOut = function () {
if (!rollOverParent) destroy_something();
rollOverParent = false;
}
child.onRollOver = function() {
rollOverParent = true;
execute_something();
}
child.onRollOut = function() {
execute_something_else();
}

 

That would just work beautifully only if child.onRollOver would be executed before parent.onRollOut. But there’s no guarantee this would happen. In fact, to me it just happened the opposite.
So I just used a function to wait a few milliseconds before executing destroy_something() to see if when you rolled out parent, it happened to roll over child.

So my final solution would look like this:

import mx.utils.Delegate;
var parent:MovieClip = createEmptyMovieClip("parent", 0);
var child:MovieClip = createEmptyMovieClip("child", 1);
var rollOverParent:Boolean = false;
var now:Number;
var delay:Number;
parent.onRollOver = function () {
    create_something();
}
parent.onRollOut = function () {
    rollOverParent = false;
    now = getTimer();
    delay = 100;
    parent.onEnterFrame = Delegate.create(this, wait);
}
child.onRollOver = function() {
    rollOverParent = true;
    execute_something();
}
child.onRollOut = function() {
    execute_something_else();
}
function wait() {
    if ((getTimer() - now) >= delay) {
        delete parent.onEnterFrame;
        if (!rollOverParent) destroy_something();
    }
}

 

I spent a few days trying to find a workaround for this totally annoying ActionScript inconvenience (using _xmouse & _ymouse coordinates tests for example), but this was the first solution which fixed my problem completely.

Connect to us on Facebook Follow us on Twitter Subscribe to our RSS feed
Copyright © FlashXML.net 2009 - 2012