ActionScript 3 循环

ActionScript 3 Looping

我最近才开始在 AS3 中使用 Looping,我还有很多东西要学。这是问题。下面是一个将 5 个球放在舞台上的循环。到目前为止,一切都很好。但是,我想创建一种情况,当点击一个按钮时,底部的球被移除,然后每个球一个接一个地取代它下面的球,每次点击都继续这个,直到所有的球都消失了。 .我用 add/remove child 创建了这种情况,我认为循环可以更有效。我只是不知道如何访问这些球,因为我也没有可以引用的实例名称或 class 名称。

var ball: gBall4M;
var i: Number;

for (i = 0; i < 5; i++) {

  ball = new gBall4M();
  ball.x = 331.30;
  ball.y = 25 + i * 17
  addChild(ball);
}
function release2Ball2(event: MouseEvent): void {

这是我想要得到的效果https://youtu.be/B4GLolw8QVA

只需将实例保存在一个数组或类似的数组中,您以后可以使用。

我希望您使用的是代码文件,而不是直接在 Flash 编辑器的框架内编写代码。否则你可能会遇到问题。

类似于此的东西应该可以工作

package {
  class Main {
    var balls:Array = [];

    function createBalls() {
      var ball: gBall4M;
      var i: Number;

      for (i = 0; i < 5; i++) {

        ball = new gBall4M();
        ball.x = 331.30;
        ball.y = 25 + i * 17;
        balls.push(ball); //Save them to array
        addChild(ball);
      }
    }

    function release2Ball2(event: MouseEvent): void {
      var clickedBall:gBall4M = event.currentTarget as gBall4M; //this might be wrong depending on what you are listening for, and what type of object gBall4M is...
      for(var i=0; i<balls.length; ++i) {
        if(balls[i] == clickedBall) {
          balls[i].splice(i, 1); //remove instance from array
          removeChild(clickedBall); //remove instance from display list
          break;
        }
      }
    }
  }
}

如@daniel-messer 的回答所述,您可以使用 Array to store your balls, and for the second part of your question, when removing the last ball and moving the other ones, you can use array.pop() to remove the last element of the array, and then you can use array.map() 来移动其他球:

function release2Ball2(event:MouseEvent): void
{
    if(balls.length > 0){
        ball = balls.pop();             // remove and get the last element of the array
        ball.parent.removeChild(ball);  // remove that element from the DisplayObjectContainer 
        function move_ball(item:Ball, index:int, array:Array):void {
            item.y += 17;
        }
        // move the rest of elements
        balls.map(move_ball, this);
    }
}

编辑:

看看代码的工作原理,我添加了数字以了解球是如何移动的:

你的完整代码可以是这样的:

var balls:Array = [],
    ball:gBall4M;

for (var i:int = 0; i < 5; i++) {
    ball = new gBall4M();
    ball.x = 331.30;
    ball.y = 25 + i * 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);
    }
}

编辑 2:

要制作那种动画,您可以像这样使用 Timer

var balls:Array = [],
    ball:gBall4M;

for (var i:int = 0; i < 5; i++) {
    ball = new gBall4M();
    ball.x = 30;
    ball.y = 25 + i * 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();
        }
    })

这会给你这样的东西:

希望能帮到你。

好的,我明白了。在第 1 帧使用此代码

function release2Ball2(event: MouseEvent): void {
  if (ballA.length > 0) {
  ball = ballA.pop();
  removeChild(ball);
  ball = null
  gotoAndPlay(2);

  }
}
stop();

在第 10 帧使用此代码:

for (i = 0; i < ballA.length; i++) {
  ballA[i].y += 17;
stop();

那就行了。

非常感谢您的帮助