从库Actionscript 3生成随机实例

时间:2013-11-11 15:57:08

标签: actionscript-3 flash random actionscript adobe

希望有人能帮助我在AS3。以下是每3秒生成1个球的代码。在库中有一个名为“coin”的动画片段。每隔3秒就会在舞台上输出。

问题:我想要实现的是创建8个随机动画片段并链接到“硬币”,所以当它被导出时它不是一直都是同一个球,所以这就是我所拥有的:

var ballsArray:Array = [ball00,ball01,ball02,ball03,ball04,ball05,ball06,ball07,ball08]; ---- ---图库中的movieclips

问题:如何让硬币随机读取这个数组所以它不会去“硬币”movieclip“而只是玩一个我希望它去玩随机球的球

非常感谢你

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,chosenNums.length);

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

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

chuckBall generates coins

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);

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 ++;

1 个答案:

答案 0 :(得分: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 ++;
}

我希望这会对你有所帮助:)。