AS 3 动作脚本

AS 3 Action Script

我试图让我的 EntityArray 在它到达屏幕宽度和高度时停止移动,如果有人能帮助我的话,它会很棒,然后将它的位置重置为我的大学项目的中心

private function keyDownHandler(evt:KeyboardEvent)
{
    // Detect 'A' key for UP movement
    if(evt.keyCode == 65) 
    {
        trace("A")
        //Move player left (using key 'A')
       EntityArray[0].x = (EntityArray[0].x)-10;
    }

    //creating the Frog
    var newfrog = new frog();
    newfrog.x = 320;
    newfrog.y = 220;
    EntityArray.push(newfrog);
}

您可以在 keyDownHandler:

中放置一些非常简单的保护代码
//Add the if statement after this line in your current function:  
EntityArray[0].x = (EntityArray[0].x)-10;

//Guard code
if (EntityArray[0].x <= 0) {
    EntityArray[0].x = 320;
} else if (EntityArray[0].x > Stage.width - EntityArray[0].width) {
    //Note:  You need to subtract the frog's width from the stage to avoid having the frog be mostly off the screen before resetting the position.
    EntityArray[0].x = 320;
}