-1

嗨,希望有人可以在 AS3 中帮助我。下面是每 3 秒生成 1 个球的代码。库中有一个名为“硬币”的影片剪辑,每 3 秒在舞台上导出一次。

问题:我想要实现的是创建 8 个以上的随机影片剪辑并将其链接到“硬币”,因此当它被导出时,它始终不是同一个球,所以这就是我所拥有的:

var ballsArray:Array = [ball00,ball01,ball02,ball03,ball04,ball05,ball06,ball07,ball08];---- --- 库中的影片剪辑

问题:我如何让硬币随机读取这个数组,这样它就不会进入“硬币”电影剪辑“只玩一个我希望它去玩随机球的球

非常感谢你

creates timer that runs every 3 seconds.  Number of times it runs 
is determined by the length of chosenNums array (which is the number
of numbers chosen)

var timer:Timer = new Timer(3000, selectedNums.length);

timer is listening for itself to trigger, calls chuckBall()

timer.addEventListener(TimerEvent.TIMER,chuckBall);timer.addEventListener(TimerEvent.TIMER_COMPLETE, ballChuckingComplete);

chuckBall generates coins

函数chuckBall(事件:TimerEvent):无效{

  generates new Coin - Coin is the name of MovieClip in Library
  remember to export for Actionscript
var c1:Coin = new Coin();


   coin is placed at appropriate x and y coords
c1.x = kNum[chosenNums[chosenNumsIndex]].x;
c1.y = kNum[chosenNums[chosenNumsIndex]].y;



     addChild tells main timeline to display coin once it's generated.
this.addChild(c1);

trace(chosenNums[chosenNumsIndex])

     pull appropriate movieclip out of array and play to darken.
mcArray[chosenNums[chosenNumsIndex] - 1].play();

    fill in bottom numbers

      increments chosenNumsIndex so the next time chuckBall runs
      it pulls the next element of the chosenNums array.
      chosenNumsIndex ++;
4

1 回答 1

0



您可以保留它们的类,而不是将球的实例保留在数组中,然后您可以将新创建​​的类实例添加到硬币实例中。

举个例子:

// keep the classes of your balls. 
// In the library, the balls must be exported for actionScript with class names Ball00, Ball01, Ball02...
var ballsArray:Array = [Ball00, Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, Ball07, Ball08];

// create your timer
var timer:Timer = new Timer( 3000, chosenNums.length );

// add your listeners
timer.addEventListener(TimerEvent.TIMER, chuckBall); 
timer.addEventListener(TimerEvent.TIMER_COMPLETE, ballChuckingComplete);

// generate balls
function chuckBall(event:TimerEvent):void 
{
    //generates new Coin - Coin is the name of MovieClip in Library remember to export for Actionscript
    var c1:Coin = new Coin();

    //coin is placed at appropriate x and y coords
    c1.x = kNum[chosenNums[chosenNumsIndex]].x;
    c1.y = kNum[chosenNums[chosenNumsIndex]].y;

    //addChild tells main timeline to display coin once it's generated.
    this.addChild(c1);

    // instanciate your ball
    var ball:MovieClip = new (mcArray[chosenNums[chosenNumsIndex] - 1] as Class)() as MovieClip;

    // add your ball to your coin if you want 
    c1.addChild( ball );

    // play the ball to darken.
    ball.play();

    //increments chosenNumsIndex so the next time chuckBall runs it pulls the next element of the chosenNums array.
    chosenNumsIndex ++;
}

我希望这能帮到您 :)

于 2013-11-12T06:06:56.517 回答