2016-10-20 64 views
0

我正在爲iPad前後移動一個iPad的項目。當它碰到邊界時,它必須旋轉一點,然後向另一個方向回滾,並讓後面的小孩也開始移動並遵循隨機路徑。 (我在做從頭項目。見代碼)。as3在iPad或平板電腦上來回移動物體

ball_mc.addEventListener(Event.ENTER_FRAME, moveBall); 

function moveBall(e:Event):void { 

ball_mc.rotation += 1; 
if (ball_mc.x < (stage.stageWidth - 100)) { 
    //trace('move forward'); 
    ball_mc.x += 2; 

} else { 
    // while(ball_mc.x > 100)? 
    // trace('move backward'); 
    // how does it roll back? 
     ball_mc.x += -2; 
    } 
} 

}

enter image description here

回答

0

您需要保存的方向或速度的值的變量。如果球通過左側邊界,則此變量應更改爲2,如果球通過右側邊界,則該變量更改爲-2。像這樣:

var ballSpeed:Number = 0; // this should go where you declare global variables such as at the beginning of your main class before the constructor 



ball_mc.addEventListener(Event.ENTER_FRAME, moveBall); 

function moveBall(e:Event):void { 

ball_mc.rotation += ballSpeed; 
if (ball_mc.x < (stage.stageWidth - 100)) { 
    ballSpeed = 2; 
} else if (ball_mc.x > stage.stageWidth){ 
    ballSpeed = -2; 
} 
相關問題