如何随着时间的推移使健康衰退? (AS3)

时间:2013-10-26 11:21:29

标签: actionscript-3 flash actionscript

您好我是ActionScript 3.0的新手,我真的需要一些帮助:P 我想做一个计时(?)游戏。 基本上这种健康状况应该随机衰减,当健康状况达到1000时,播放它的人必须点击一个按钮。

当我点击按钮时,我已设法制作健康栏并使其下降,但我如何制作它以便健康栏随时间衰减?

最大生命值是10000,我希望每0.1秒生命值下降80~90(随机)?

到目前为止,这是我做过的代码

var maxHP:int = 10000;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
var randNum:Number=Math.floor(Math.random()*1000)+900;

function updateHealthBar():void
{
percentHP = currentHP / maxHP;
healthBar.barColor.scaleX = percentHP;
}

function attackHealth():void
{
for(currentHP = maxHP; currentHP > 0; currentHP-= 1)
{
    updateHealthBar();
}
}

attackButton.addEventListener(MouseEvent.CLICK, attackButtonClicked);

function attackButtonClicked(e:MouseEvent):void
{
currentHP -= 800;
if(currentHP <= 0)
{
    currentHP = 0;
    trace("You died! :P");
}
setInterval(attackHealth, 500);
updateHealthBar();

}

1 个答案:

答案 0 :(得分:0)

您可以使用timer

import flash.events.TimerEvent;
import flash.utils.Timer;

//it will trigger every 100ms and keep repeats indefinitely
 var t:Timer = new Timer(100);
 t.addEventListener(TimerEvent.TIMER, onTimer);


  private function onTimer(event:TimerEvent):void {
       var damage:int = 80 + int(Math.random()*11); //[80, 90]

       //do something with damage

  }