0

ランダムなオブジェクトのセットを作成して、ループでステージを落下させたいと考えています。

これまでのところ、ランダムな x 座標に落ちるテスト オブジェクトを作成しました。オブジェクトの複数のインスタンスが継続的に落下するように、落下関数をループする方法を理解するのに苦労しています。

var randomX:Number = Math.random() * 800;

test_mc.x = randomX;
test_mc.y = 0;

var speed:Number = 10;

test_mc.addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void
{
 e.target.y += speed; 

 if(e.target.y >= 480)
 {
      test_mc.removeEventListener(Event.ENTER_FRAME, moveDown);
 }
}
4

2 に答える 2

2

私の降雪効果コードを参照してください。

雪の開始位置はすべてランダムで、実際の降雪状況とほぼ同じ効果です。あなたが走ればあなたは驚かれることでしょう。

雪は私のカスタムムービークリップです(白い円の形、幅15、高さ15)

これが私のデモです:SnowEffect

これが私のソースです:SnowEffect Down

this.addEventListener( Event.ENTER_FRAME, onEnter );

function onEnter( e: Event ):void {

    var s: Snow = new Snow();
    s.x=550*Math.random();
    s.y=-20;
    s.width=s.height=1+9*Math.random();// 1 ~ 9

    s.xSpeed=-2+4*Math.random();// -2 ~ 2
    s.ySpeed=1+4*Math.random();// 1 ~ 5

    s.at = -0.001 -0.001*Math.random();
    s.vt = 0;
    this.addChild( s );

    s.addEventListener( Event.ENTER_FRAME, onSnowEnter );
}

function onSnowEnter( e: Event ):void {
    var s:Snow=e.currentTarget as Snow;
    s.x+=s.xSpeed;
    s.y+=s.ySpeed;


    if (s.y>=480) {
        s.addEventListener( Event.ENTER_FRAME, onMeltingEnter );
    }
}

function onMeltingEnter( e: Event ): void {
    var s:Snow=e.currentTarget as Snow;
    this.addChild( s );
    s.removeEventListener( Event.ENTER_FRAME, onSnowEnter );
    s.vt += s.at;
    s.alpha += s.vt;
    if ( s.alpha <=0){
        s.removeEventListener( Event.ENTER_FRAME, onMeltingEnter );
        this.removeChild( s );
    }

}
于 2012-08-09T17:55:09.743 に答える
1

一連のオブジェクトを作成し、それらを配列に追加してから、配列をループします。

var numOfObjects:int = 10;
var fallingObjectArray:Array = [];
var speed:Number = 10;

// Add 10 falling objects to the display and to the array
for(var i:int = 0; i < numOfObjects; i++) {
   var fallingObject:Sprite = new Sprite();
   fallingObject.graphics.beginFill(0xFF0000);
   fallingObject.graphics.drawCircle(0, 0, 15);
   fallingObject.graphics.endFill();
   addChild(fallingObject);
   fallingObject.x = Math.random() * stage.stageWidth;
   fallingObjectArray.push(fallingObject);
}

addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void
{    
   // Go through all the objects in the array and move them down
   for each(var fallingObject in fallingObjectArray) {
      fallingObject.y += speed;
      // If the object is past the screen height, remove it from display and array
      if(fallingObject.y+fallingObject.height >= stage.stageHeight) {
         removeChild(fallingObject);
         fallingObjectArray.splice(fallingObjectArray.indexOf(fallingObject), 1);
      }
   }
   // Once all objects have fallen off the screen, remove the listener
   if(fallingObjectArray.length <= 0) {
      removeEventListener(Event.ENTER_FRAME, moveDown);
   }
}

上記のコードは赤い円を使用しています-代わりに、持っている画像を使用する必要があります(赤い円が好きでない限り...)。

于 2012-08-09T17:54:38.783 に答える