actionscript 3 - Make all children do something...? -
i'm wondering if there easy way make children simultaneously something. in particular, want on stage shake , forth earthquake. it's bit hard single out on stage , add in proper code because @ given time don't know amount of children on stage.
also, i'm not sure if makes difference, when addchild something, don't go out of way add stage...like few buttons i'll this.addchild(mybutton)
. don't know if there's way access has been addchilded though may not have added them directly stage? i'm pretty sure numchildren
returns value of number of objects on screen, wrong...
i thinking of doing
inside loop
if (numchildren >= 10 && nukeshaketimer.currentcount == 0) { nukeshaketimer.start(); } if (nukeshaketimer.currentcount > 0) { nukeshake(); }
nukeshake
public function nukeshake():void { numberchildren = numchildren; trace(numberchildren); while (numberchildren > 0) { var tempobject:object = getchildat(numberchildren) if (nukeshaketimer.currentcount % 2 == 1) { tempobject.x += 10; } if (nukeshaketimer.currentcount % 2 == 0) { tempobject.x -= 10; } numberchildren -= 1; } if (nukeshaketimer.currentcount > 30) { nukeshaketimer.reset(); } }
when try though, runtime error line var tempobject:object = getchildat(numberchildren)
, error reads rangeerror: error #2006: supplied index out of bounds.
also feel may possible operation faster without using while loop, maybe not. please, thanks!
if want address every child on stage you'll need loop through them. cherniv says, changing getchildat
call use numberchildren-1
should resolve error.
it shouldn't matter whether or not directly on stage or not. looping through stage children whatever container added objects to. moving container move children too. (though you'll have more work if need move children independently).
but...
in specific case, looks want shake on screen together, in same direction @ same rate. in case, add objects single container sprite on stage. then, when want shake effect, need move 1 container object , moves together.
Comments
Post a Comment