0

大家好,下面是我移动角色 player_mc 的代码,它可以很好地在屏幕上移动,但我无法阻止他离开屏幕。据我了解,如果 playermc = x> 舞台宽度减少 5 增加 5 等,并且与 y 相同,但为什么这对我不起作用?你们能告诉我我在这里做错了什么吗?您可以看到标记为不作为注释工作的代码。谢谢,如果您想查看完整代码,这里是我问的另一个问题的链接: https ://stackoverflow.com/questions/16764305/educational-simulation-actionscript-2

   function rotatePlayer()
    {
        //calculate player_mc rotation, based on player position & mouse position 
        player_mc._rotation = Math.atan2(_ymouse - player_mc._y, _xmouse - player_mc._x) * radians2;

        // not working code: stage collision.
    if (player_mc._x > stage.width)
    {
        player_mc._x+50
    }
    if (Key.isDown(Key.RIGHT))
    {
    player_mc._x += 5; 
    }
    else if (Key.isDown(Key.LEFT))
    {
    player_mc._x -= 5;
    }
    else if (Key.isDown(Key.UP))
    {
    player_mc._y -= 5;
    }
    else if (Key.isDown(Key.DOWN))
    {
    player_mc._y += 5;
    }
    }
4

2 回答 2

0

此外,在 AS2 中,您需要将“Stage”大写,因此需要读取类似

if (player_mc._x > Stage.width) {
    player_mc._x= Stage.width;
}
else if (player_mc._x < 0) {
    player_mc._x = 0;
}

if (player_mc._y > Stage.height) {
    player_mc._y= Stage.height;
}
else if (player_mc._y < 0) {
    player_mc._y = 0;
}

将此代码放在 Key.isDown 部分下方,而不是上方,否则在添加另一个增量之前纠正过冲会导致奇怪的反弹效果。最后,您可能希望将播放器 mc 的一半宽度减去/添加到边界,以便它的一半不会消失。

于 2013-05-28T02:27:53.827 回答
0

如果我理解正确,你想要这样的东西:

if (player_mc._x > stage.width)
        {
            player_mc._x = stage.width
        }
if (player_mc._x < 0) {
    player_mc._x = 0;
}
于 2013-05-28T02:20:54.460 回答