Login / Sign Up

Your cart has 9 products totalling 306.50 USD

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.

 

  • COMMENTS
  • SEARCH COMMENTS
ahmad contentonic

jos,jos….

Posted 5 Months Ago
Jason

I use a method like you mentioned with _xmouse and _ymouse. I had to use onRollOver for the child function, then my Tween.onMotionFinished triggered an onEnterFrame call to a function for the parent, so the child function could be kept seperate and active. Also making the child._visible=false helps prevent activating another onRollOver function until the parent function is complete.

parent.child.onRollOver = function() {
 parent.child._visible = false;
 var outTween = new Tween(parent, "_x", Strong.easeInOut, 1010, 790, 20, false);
 outTween.onMotionChanged = function() {
 parent._alpha -= 1;
 };
 outTween.onMotionFinished = function() {
 parent.onEnterFrame = slideBack;
 };
};
MovieClip.prototype.slideBack = function() {
 var xPos = parent._xmouse;
 var yPos = parent._ymouse;
 if (xPos < 1 || xPos > 224) {
 parent.onEnterFrame = null();
 var inTween = new Tween(parent, "_x", Strong.easeInOut, 790, 1010, 20, false);
 inTween.onMotionFinished = function() {
 parent._alpha = 100;
 parent.child._visible = true;
 };
 };
 if (yPos < 1 || yPos > 450) {
 parent.onEnterFrame = null();
 var inTween = new Tween(parent, "_x", Strong.easeInOut, 790, 1010, 20, false);
 inTween.onMotionFinished = function() {
 parent._alpha = 100;
 parent.child._visible = true;
 };
 };
};

Hope this helps, don’t know if you will be able to apply it to your situation.

support

Hi,

Thank you for posting this comment.
I’m sure that are users that will find this usefull.

Posted 5 Months Ago
shashi

hi

Posted 6 Months Ago
oldnewbie

Dealing With Flash Button Event Capturing (AS2)
http://senocular.com/flash/tutorials/buttoncapturing/

Posted 6 Months Ago

Leave a Reply

Only alphanumeric characters allowed
Please type a message!
Format your comments using Textile: *bold*, _italic_, "link text":address, @code@