Skyliquid
Skyliquid

Reputation: 392

How to add a Timer in AS3

I was wondering how to add an ingame Timer in AS3, I made a health bar for enemy mobs and once mob is attacked, health bar is visible. I need to add piece of code which if Enemy was not attacked for 5 Seconds, healthBar.visible = false; but I have no idea how to deal with time in AS3

Healthbar

any ideas ?

Upvotes: 0

Views: 418

Answers (1)

bitmapdata.com
bitmapdata.com

Reputation: 9600

My suggestions are as follows:

Assumed MouseClick is Ninja attacked hero. If you using a this code you need to create Class or fit type. I just written skeleton code.

Download Source

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

stage.addEventListener(MouseEvent.CLICK, onAttacked);

var isAttacked:Boolean;
var timer:Timer = new Timer(100,50);
var alphaCount:Number = 1.0;
timer.addEventListener(TimerEvent.TIMER, onTick);
function onAttacked(e:MouseEvent):void
{
    isAttacked = true;
    timer.start();
}

function onTick(e:TimerEvent):void
{
    alphaCount = 1.0 - ( 2 * timer.currentCount ) / 100;
    if(!isAttacked)
    {
        goFadeStatusBar();
    }
    else
    {
        restoreStatusBar();
        alphaCount = 1.0;
        timer.reset();
        timer.start();
        isAttacked = false;
    }
}

function goFade():void
{
    bar.alpha = alphaCount;
}

function restore():void
{
    bar.alpha = 1;
}

Upvotes: 1

Related Questions