2017-04-25 89 views
1

嘿,我目前正在使用Adobe Flash Professional CS6创建游戏。我有一个角色,实例名称为“外星人”。 到目前为止,我只能编写我的游戏,以便外星人不能离开舞台的顶部或左侧。我无法弄清楚如何编写代码,以使外星人不能脱离舞台的底部或右侧。我的编码如下:如何在Adobe Flash Professional CS6中停止移动对象(通过箭头键移动)动作脚本3.0

if((alien.x) < (alien.width/2)){ 
     alien.x += 10; 
} 
if((alien.y) < (alien.width/2)){ 
     alien.y += 10; 
} 

谢谢你的时间。

回答

2

使用stage.stageWidthstage.stageHeight值来确定舞台区域的大小。这不是强制性的使用矩形,但我喜欢它是多么的自我解释。

import flash.geom.Rectangle; 

// new Rectangle(left, top, width, height) 
var aBounds:Rectangle = new Rectangle(
    alien.width/2, 
    alien.height/2, 
    stage.stageWidth - alien.width, 
    stage.stageHeight - alien.height 
); 

if (alien.y < aBounds.top) alien.y = aBounds.top; 
if (alien.x < aBounds.left) alien.x = aBounds.left; 
if (alien.x > aBounds.right) alien.x = aBounds.right; 
if (alien.y > aBounds.bottom) alien.y = aBounds.bottom;