Skip to main content
Known Participant
September 30, 2011
Question

isChildOf() function?

  • September 30, 2011
  • 3 replies
  • 1138 views

thanks to your help my new flash app is running pretty well

i can still make it bug out for a few seconds if i try tho and i would like your help again to improve it

i have several objects on the stage and a MOUSE_OVER on any of these will add a menu popup child and tween it from off the stage

a MOUSE_OUT will tween the menu away and remove the menu with removeChild

heres the bug:

if i mouse out and then quickly mouse over the same object while the menu is tweening away before it has been removed, it doesn't add the menu again, and then on the next mouse out, it tries to remove the child menu, which isn't currently a child because it was just removed, and i get the error:

TypeError: Error #2007: Parameter child must be non-null.

     at flash.display::DisplayObjectContainer/removeChild()

     at menucaller/tweenOffTimerEnd()

     at flash.utils::Timer/_timerDispatch()

     at flash.utils::Timer/tick()

is there a way i can use an if statement to make sure that the menu is a child of the parent before removing it? i think this will fix the problem if it is possible to do so

This topic has been closed for replies.

3 replies

Participating Frequently
November 11, 2011

You are missing a fair bit of information here.

chopficaro2 wrote:

i have several objects on the stage and a MOUSE_OVER on any of these will add a menu popup child and tween it from off the stage

a MOUSE_OUT will tween the menu away and remove the menu with removeChild

Explain this a bit more:

  • What object adds/removes the child, is it a static method or the object it self?
  • Does each object listen for tweenOffTimerEnd() via menucaller - assuming menucaller is the object?
  • Why are you using a timer?

From what you describe above it seems you are adding the [menu] on each [MOUSE_OVER] with a tween (is this on a timer?).  When [MOUSE_OUT] is triggered, you tween the [menu] away and remove the child (with a timer)

The issue is the timer.  Although you can add execution times for tweens these are not actually 'real-time'.  Most tween engines use enterFrame events to execute their tweens - that being said your custom timer will almost NEVER be in sync with the tween timer - its not a timer. 

Solution (unless you are doing something else beyond what I described):

Use the tween completion event or callBack method.  Most tween engines allow you add a callBack method when the tween is completed.  Also you should cancel all tweens on both mouse events - this should stop your code from trying to add and remove children twice.

Solution to your post title:

//ReshapeMedia - isChildOf()

public function isChildOf(child:DispalayObject,parent:DisplayObjectContainer):Boolean{

     if(child.parent == parent) return true;

     return false;

}

Potential problem:

With timers, the listeners are always invoked, so when you roll over and out with out the tween completing or canceling (which invokes your other method) -> it continues on its execution path.

Inspiring
September 30, 2011

DIsplauObjectContainer has method contains() - you should use it. For example, if you need to remove an object with instance name myObject from the current scope the code can be:

if(this.contains(myObject) {

     removeChild(myObject);

}

Ned Murphy
Legend
September 30, 2011

You can test the same object you are trying to target to see if it is not null.

if(targeted_object != null)....