flash - ActionScript 3 Looping -


i started using looping in as3 , have learn. here question. below loop puts 5 balls 1 on top of other on stage. far good. however, i'd create situation when clicking on button, bottom ball removed, , each ball take place of ball below 1 one, continue each click until balls gone. . have created situation add/remove child , thought more efficient looping. don't know how access balls since don't have instance name or class name can reference too.

var ball: gball4m; var i: number;  (i = 0; < 5; i++) {    ball = new gball4m();   ball.x = 331.30;   ball.y = 25 + * 17   addchild(ball); } function release2ball2(event: mouseevent): void { 

this effect want https://youtu.be/b4glolw8qva

as mentioned in answer of @daniel-messer, can use array store balls, , second part of question, when removing last ball , moving other ones, can use array.pop() remove last element of array, , can use array.map() move other balls :

function release2ball2(event:mouseevent): void {     if(balls.length > 0){         ball = balls.pop();             // remove , last element of array         ball.parent.removechild(ball);  // remove element displayobjectcontainer          function move_ball(item:ball, index:int, array:array):void {             item.y += 17;         }         // move rest of elements         balls.map(move_ball, this);     } } 

edit :

take on code working, added numbers understand how balls moving :

enter image description here

your full code can :

var balls:array = [],     ball:gball4m;  (var i:int = 0; < 5; i++) {     ball = new gball4m();     ball.x = 331.30;     ball.y = 25 + * 17;     balls.push(ball);     addchild(ball); }  btn.addeventlistener(mouseevent.click, release2ball2); function release2ball2(event:mouseevent):void {     if (balls.length > 0) {         ball = balls.pop();         ball.parent.removechild(ball);         function move_ball(item:gball4m, index:int, array:array):void {             item.y +=  17;         }         balls.map(move_ball, this);     } } 

edit 2:

to kind of animation, can use timer :

var balls:array = [],     ball:gball4m;  (var i:int = 0; < 5; i++) {     ball = new gball4m();     ball.x = 30;     ball.y = 25 + * 17;     balls.push(ball);     addchild(ball); }  btn.addeventlistener(mouseevent.click, release2ball2); function release2ball2(event:mouseevent):void {     if (balls.length > 0) {         ball = balls.pop();         ball.parent.removechild(ball);         if(balls.length > 0){             timer.start();         }     } }  var timer:timer = new timer(150);     timer.addeventlistener(timerevent.timer, function(e:timerevent){         if(balls.length >= timer.currentcount){             balls[balls.length - timer.currentcount].y += 17;         } else {             timer.reset();         }     }) 

which give :

enter image description here

hope can help.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -