Движение на основе плиток на ActionScript 2.0 (Adobe Fla sh CS6) - PullRequest
0 голосов
/ 05 марта 2020

Таким образом, у меня возникла проблема, когда я не знаю, как сделать возможным движение на основе фрагмента кода в 4 направлениях (NSWE) для сценария действия 2.0.

У меня есть этот код, но это динамическое c движение , что заставляет полукокса двигаться во всех 8 направлениях (NW, NE, SW, SE N, S, W, E). Цель состоит в том, чтобы ограничить движения на основе плиток и только в 4 направлениях (NSEW)

 onClipEvent(enterFrame)
{
    speed=5;
    if(Key.isDown(Key.RIGHT))
    {
        this.gotoAndStop(4);
        this._x+=speed;
    }
    if(Key.isDown(Key.LEFT))
    {
        this.gotoAndStop(3);
        this._x-=speed;
    }
    if(Key.isDown(Key.UP))
    {
        this.gotoAndStop(1);
        this._y-=speed;
    }
    if(Key.isDown(Key.DOWN))
    {
        this.gotoAndStop(2);
        this._y+=speed;
    }
}

1 Ответ

0 голосов
/ 08 марта 2020

Самый простой и простой способ - переместить эту вещь вдоль X -оси ИЛИ вдоль Y -оси, только по одной за раз, не одновременно .

onClipEvent(enterFrame)
{
    speed = 5;

    dx = 0;
    dy = 0;

    // Figure out the complete picture of keyboard input.

    if (Key.isDown(Key.RIGHT))
    {
        dx += speed;
    }

    if (Key.isDown(Key.LEFT))
    {
        dx -= speed;
    }

    if (Key.isDown(Key.UP))
    {
        dy -= speed;
    }

    if (Key.isDown(Key.DOWN))
    {
        dy += speed;
    }

    if (dx != 0)
    {
        // Move along X-axis if LEFT or RIGHT pressed.
        // Ignore if it is none or both of them.

        this._x += dx;

        if (dx > 0)
        {
            this.gotoAndStop(4);
        }
        else
        {
            this.gotoAndStop(3);
        }
    }
    else if (dy != 0)
    {
        // Ignore if X-axis motion is already applied.
        // Move along Y-axis if UP or DOWN pressed.
        // Ignore if it is none or both of them.

        this._y += dy;

        if (dy > 0)
        {
            this.gotoAndStop(2);
        }
        else
        {
            this.gotoAndStop(1);
        }
    }
}
...