Flash AS2如何阻止角色移动屏幕+移动&轮换代码

时间:2013-05-28 02:01:03

标签: flash actionscript-2

下面的嘿伙计是我的代码,用于移动我的角色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;
    }
    }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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的宽度的一半减去/加到边界上,这样它的一半就不会消失。