AS 3 Action Script - PullRequest
       7

AS 3 Action Script

1 голос
/ 22 марта 2019

Я пытаюсь заставить мой 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);
}

image

1 Ответ

1 голос
/ 22 марта 2019

Вы можете добавить довольно простой защитный код в 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...