AS3 - 如何在使用 hitTestObject 触摸对象时停止播放器?

AS3 - How to stop player when touching object using hitTestObject?

我想知道如何在触摸物体时停止播放器。我在这里设置了动作,但我找不到让我的播放器在接触物体时正确停止的方法。

我的播放器在播放器中的移动代码Class:

这是按下按键时的事件侦听器。 Boolean 值在按下时变为真。

public function onKeyDown(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.D)
    {
        isRight = true;
    }
    if (event.keyCode == Keyboard.A)
    {
        isLeft = true;
    }
    if (event.keyCode == Keyboard.W)
    {
        isUp = true;
    }
    if (event.keyCode == Keyboard.S)
    {
        isDown = true;
    }
}

这是未按下按键时的事件侦听器。 Boolean 值在未按下时变为假。

private function onKeyUp(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.D)
    {
        isRight = false;
    }

    if (event.keyCode == Keyboard.A)
    {
        isLeft = false;
    }

    if (event.keyCode == Keyboard.W)
    {
        isUp = false;
    }

    if (event.keyCode == Keyboard.S)
    {
        isDown = false;;
    }

}

这是输入框,vxvyint变量,不按键时为0,按键时改变值。 vxvy 也会在每一帧添加到玩家的 xy 但当它们为 0 时,玩家将不会移动。

private function onEnterFrame(event:Event):void
{
    _vx = 0;
    _vy = 0;

    if (isRight)
    {
        _vx = 5;
    }

    if (isLeft)
    {
        _vx = -5;
    }

    if (isUp)
    {
        _vy = -5;
    }

    if (isDown)
    {
        _vy = 5;
    }

    x += _vx;
    y += _vy;

}
if(player.hitTestObject(object)){
     // stop player
    player.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}

或者你可以反过来做。 将其放入播放器的输入框内。

添加另一个条件并在 onEnterFrame 中检查关键状态的条件后将增量器 _vx 或 _vy(或两者)设置为 0。

if(player.hitTestObject(object)){
        _vx = 0;
        _vy = 0;
}

你不需要有很多条件,看看这个例子:

_keys = new Object();

_keys[Keyboard.D] = false;
_keys[Keyboard.A] = false;
_keys[Keyboard.W] = false;
_keys[Keyboard.S] = false;  

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyInteraction);
this.stage.addEventListener(KeyboardEvent.KEY_UP, keyInteraction);

//---Key events
function keyInteraction(evt:KeyboardEvent){

    if( _keys.hasOwnProperty(evt.keyCode) ){

        _keys[evt.keyCode] = Boolean( evt.type == KeyboardEvent.KEY_DOWN );

    }

}

当您按下或松开 AWDS 键,相应的 _keys Object 属性从 true 更改为 false

现在,hitTest 部分。也许 hitTestPoint 方法比 hitestObject 方法更好,因为最后一个方法只检查两个对象的边界是否接触。 hitTestPoint 方法检查 point 是否触及 Object.

的任何像素

当您每五个像素移动一次播放器时,有必要将播放器恢复到不接触物体的正确位置(我为此制作了一个 while 循环)。取决于您的游戏设计,您需要更改代码,但这可以指导您达到目的:

The maze is the object that the player can't touch:

玩家的getPoint方法

public function getPoint(corner:uint){

    var point:Point = this.parent.localToGlobal( new Point( this.x, this.y ) );

    if(corner == 1 || corner == 2) point.x += _w;
    if(corner == 2 || corner == 3) point.y += _h;

    return point;

}

HitTest 代码(游戏刻度)

//---Move the player
private function gameTick(evt:Event):void{

    var hit:Boolean = false;

    //---Sum the vars horizontal
    if( _keys[Keyboard.D] ) _player.x += 5;
    if( _keys[Keyboard.A] ) _player.x -= 5;

    //---Check if the point is inside the scene in the horizontal
    if(_player.x < 0) _player.x = 0;
    if(_player.x > this.stage.stageWidth - _player.width) _player.x = this.stage.stageWidth - _player.width;            

    var TL:Point = _player.getPoint(0);
    var TR:Point = _player.getPoint(1);
    var BR:Point = _player.getPoint(2);
    var BL:Point = _player.getPoint(3);

    //---Hittest in the horizontal
    if(
        _maze.hitTestPoint(TL.x, TL.y, true)
        ||
        _maze.hitTestPoint(TR.x, TR.y, true)
        ||
        _maze.hitTestPoint(BR.x, BR.y, true)
        ||
        _maze.hitTestPoint(BL.x, BL.y, true)
    ){

        var sumX:int = 0;

        if( _keys[Keyboard.D] ) sumX = -1;
        if( _keys[Keyboard.A] ) sumX = 1;

        hit = true;

        while(hit){

            TL.x += sumX;
            TR.x += sumX;
            BR.x += sumX;
            BL.x += sumX;

            if(
                !_maze.hitTestPoint(TL.x, TL.y, true)
                &&
                !_maze.hitTestPoint(TR.x, TR.y, true)
                &&
                !_maze.hitTestPoint(BR.x, BR.y, true)
                &&
                !_maze.hitTestPoint(BL.x, BL.y, true)
            ){

                hit = false;

                _player.x = TL.x;

            }

        }

    }

    //---Sum the vars vertical
    if( _keys[Keyboard.S] ) _player.y += 5;
    if( _keys[Keyboard.W] ) _player.y -= 5;

    //---Check if the point is inside the scene in the vertical
    if(_player.y < 0) _player.y = 0;
    if(_player.y > this.stage.stageHeight - _player.height) _player.y = this.stage.stageHeight - _player.height;            

    TL = _player.getPoint(0);
    TR = _player.getPoint(1);
    BR = _player.getPoint(2);
    BL = _player.getPoint(3);

    if(
        _maze.hitTestPoint(TL.x, TL.y, true)
        ||
        _maze.hitTestPoint(TR.x, TR.y, true)
        ||
        _maze.hitTestPoint(BR.x, BR.y, true)
        ||
        _maze.hitTestPoint(BL.x, BL.y, true)
    ){

        var sumY:int = 0;

        if( _keys[Keyboard.S] ) sumY = -1;
        if( _keys[Keyboard.W] ) sumY = 1;

        hit = true;

        while(hit){

            TL.y += sumY;
            TR.y += sumY;
            BR.y += sumY;
            BL.y += sumY;

            if(
                !_maze.hitTestPoint(TL.x, TL.y, true)
                &&
                !_maze.hitTestPoint(TR.x, TR.y, true)
                &&
                !_maze.hitTestPoint(BR.x, BR.y, true)
                &&
                !_maze.hitTestPoint(BL.x, BL.y, true)
            ){

                hit = false;

                _player.y = TL.y;

            }

        }

    }

}

Download the example