嘿,下面是我移动我的角色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;
}
}
如果我理解正确的话,您想要这样的内容:
if (player_mc._x > stage.width)
{
player_mc._x = stage.width
}
if (player_mc._x < 0) {
player_mc._x = 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宽度的一半,这样一半的宽度就不会消失。