2014-01-09 70 views
0

我正在編程一個tamagotci風格的遊戲,並且我需要每秒鐘健康一次。當您提供目標時,我已經有一個得分編號已更改。如果有人能夠幫我編輯我目前使用的代碼,我會非常感激,每秒鐘減少1次健康。這是我的代碼,如果有人需要看到它;使健康每秒減少

 health=100 
     txtform.font = "Arial"; 
     txtform.color = 0x000000; 
     txtform.size = 24; 
     txtform.bold = true; 
     healthdisplay.defaultTextFormat=txtform; 
     addChild(healthdisplay); 
     healthdisplay.text=("Health: " + health) 
     healthdisplay.x=75 
     healthdisplay.y=-20 
     healthdisplay.autoSize=TextFieldAutoSize.CENTER; 

    } 

    public function IncreaseHealth(points:int){ 
     health+=points 
     healthdisplay.text=("Health: " + health) 
     healthdisplay.defaultTextFormat=txtform; 
    } 

回答

0

創建一個函數DecreaseHealth(),您每秒鐘都會調用一次。喜歡的東西:

var timer:Timer = new Timer(1000); // 1 second = 1000 milliseconds. 
timer.addEventListener(TimerEvent.TIMER, DecreaseHealth); 

timer.start(); 

DecreaseHealth()具有以下特徵:

function DecreaseHealth(event:TimerEvent):void 
{ 
    health -= 1; 
    // Do printing or whatever else here. 
} 
+0

感謝您的答覆。這可能對我完全沒有意義,但在輸入代碼後,我得到編譯器錯誤「通過靜態類型類的引用訪問可能未定義的屬性TIMER」。 – user3179788

+0

@ user3179788現在嘗試代碼示例,我敢打賭,它抱怨,因爲我有'Timer.TIMER'而不是'TimerEvent.TIMER'。 – Keeler

+0

現在它符合,謝謝!我已經將它添加到了我的阿凡達功能,以嘗試降低Health的價值,但沒有運氣0。我得到的輸出爲ArgumentError:錯誤#1063:參數計數不匹配頭像/ DecreaseHealth()。預計0,得到1. \t at flash.utils :: Timer/_timerDispatch() \t at flash.utils :: Timer/tick()任何想法? – user3179788