ActionScript 2 墙壁碰撞

ActionScript 2 Wall Collision

我正在制作我的 Flash 游戏,但是...碰撞是我的大问题。我尝试了每个网站,但没有任何效果。

播放器的代码是这样的:

    onClipEvent(enterFrame){
        if(Key.isDown(Key.RIGHT)) {
            this._x+=3
        }
            if(Key.isDown(Key.LEFT)) {
            this._x-=3
        }
            if(Key.isDown(Key.UP)) {
            this._y-=3
        }
            if(Key.isDown(Key.DOWN)) {
            this._y+=3
        }

    }

Collision:

    if(cityhallLeftWall.hitTest(Player._x+Player._width/2, Player._y, true)){
    Player._x -=0
    }
    if(cityhallRightWall.hitTest(Player._x-Player._width/2, Player._y, true)){
    Player._x +=0
    }
    if(cityhallTopWall.hitTest(Player._x, Player._y+Player._height/2, true)){
    Player._y +=0
    }
    if(cityhallBottomWall.hitTest(Player._x, Player._y-Player._height/2, true)){
    Player._y -=0
    }

播放器的动画片段名为"Player"。 建筑物的动画片段名为 "cityhall"。 所以,例如,当影片剪辑播放器接触影片剪辑市政厅时,y 和 x 速度变为 0 或类似的值。 实在是找不到解决办法,所以我决定在这里寻求帮助。

谢谢:)

你应该让 += 成为与玩家速度相等的排斥力,即如果您的玩家以每帧 5 像素的 x 速度撞墙,则墙壁应在相反方向为玩家增加 5 像素的速度,这样您的净移动为零。

我已经有一段时间没有在 AS2 中做任何事情了,但我会尝试一下。

首先,为玩家的速度设置一个变量。它可能看起来像这样:

var speedX:Number = 0;

现在,在您的 enterFrame 函数中添加:

Player._x += speedX; //

接下来,在您的每个 hitTest 方法中,执行以下操作:

if(cityhallLeftWall.hitTest(Player._x+Player._width/2, Player._y, true)){
    speedX = 0;
}

您需要对 y 方向执行同样的操作。我相信你可以从这里弄明白。干杯!

首先是代码,因为大多数人没有它就没有阅读的动力(在我的测试中,它被放置在电影剪辑播放器中,或者对我来说只是电影剪辑 P)

    onClipEvent(enterFrame){
    if(Key.isDown(Key.RIGHT)) {
        this._x+=3
    }
        if(Key.isDown(Key.LEFT)) {
        this._x-=3
    }
        if(Key.isDown(Key.UP)) {
        this._y-=3
    }
        if(Key.isDown(Key.DOWN)) {
        this._y+=3
    }
if(_root.cityhallLeftWall.hitTest(this._x-this._width/2, this._y, true)){
this._x +=3;
}
if(_root.cityhallRightWall.hitTest(this._x+this._width/2, this._y, true)){
this._x -=3;
}
if(_root.cityhallTopWall.hitTest(this._x, this._y-this._height/2, true)){
this._y +=3;
}
if(_root.cityhallBottomWall.hitTest(this._x, this._y+this._height/2, true)){
this._y -=3;
}
}

我测试了这段代码并解决了你的问题。这将很难用语言理解,所以请耐心等待(不要将声誉指向 post 图片)。

1.When testing for the right wall hit test you didn't use Player._x+Player._width/2 but Player._x-Player._width/2 this当您测试角色的左边缘与右边缘的墙时不起作用,因此当他被推出时玩家将 'inside' 在墙上。

2.You 对左边的墙犯了同样的错误,测试右边的边缘。

3.you 不像其他答案提到的那样从玩家中减去速度但是从玩家当前位置中减去 0 这没有任何作用,你必须减去玩家移动的数量在这种情况下,每个方向(上、下、左、右)3 个。

4.Your 碰撞检查线不在输入帧括号中,因此只会在 swf 启动时检查一次,而不是每一帧。

5.This 不是一个错误,但提示使用 while 循环,就像当前的 "if" 你会注意到玩家是否走到墙边并设法以某种方式进入内部并且直到按下方向键他可以留在墙内但是这不可能发生。

while 循环代码:

     onClipEvent(enterFrame){
     if(Key.isDown(Key.RIGHT)) {
        this._x+=3
     }
        if(Key.isDown(Key.LEFT)) {
        this._x-=3
    }
        if(Key.isDown(Key.UP)) {
        this._y-=3
    }
        if(Key.isDown(Key.DOWN)) {
        this._y+=3
    }

while(_root.cityhallLeftWall.hitTest(this._x-this._width/2, this._y, true)){
this._x +=3;
}
while(_root.cityhallRightWall.hitTest(this._x+this._width/2, this._y, true)){
this._x -=3;
}
while(_root.cityhallTopWall.hitTest(this._x, this._y-this._height/2, true)){
this._y +=3;
}
while(_root.cityhallBottomWall.hitTest(this._x, this._y+this._height/2, true)){
this._y -=3;
}
}

希望这有效,祝你好运。

问题出在玩家的坐标上。它们不是全球性的。

在你的碰撞函数中试试这个:

// store player's new local position in an object
var playerGlobalPosition = {
    x:Player._x,
    y:Player._y
};

// translate local position to global
Player.localToGlobal(playerGlobalPosition);

然后针对这些全局坐标进行测试:

if(cityhallLeftWall.hitTest(playerGlobalPosition.x + (Player._width/2), playerGlobalPosition.y, true)){
    Player._x -= 3; // as mentioned before, replace those 0 values with 3
}

解释:

在 hitTest 方法中,x 和 y 坐标在全局坐标中定义 space。因此,如果我们的坐标(Player._x 和 Player._y)是局部坐标,我们应该在应用该方法之前将它们转换为全局坐标 space。 More here and here