0

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

我的播放器在播放器中的移动代码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;;
    }

}

这是输入帧,vx并且vyint变量,当没有按下键时为 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;

}
4

3 回答 3

1

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

if(player.hitTestObject(object)){
        _vx = 0;
        _vy = 0;
}
于 2016-01-02T06:53:55.490 回答
1

不需要很多条件,看一下这个例子:

_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 );

    }

}

当您按下或释放A、或键时W,相应的 _keys属性会从变为。DSObjecttruefalse

现在,hitTest部分。MaybehitTestPointhitestObject方法更好,因为最后一个方法只检查两个对象的边界是否接触。而该hitTestPoint方法检查是否point触摸Object.

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

mazeplayer不能触摸的对象:

玩家的 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;

            }

        }

    }

}

下载示例

于 2016-01-02T18:31:33.060 回答
0
if(player.hitTestObject(object)){
     // stop player
    player.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}

或者你可以反过来做。把它放在你的播放器的enterframe中。

于 2016-01-02T06:38:28.950 回答