2017-04-25 92 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; 
相關問題