View Full Version : HP heal after set time not working (setInterval();)
Naezo
July 12th, 2010, 01:55 AM
You guy's have saved me once not to long ago, I could use that again here :D
So, my problem is I have set up a script for a HP bar, and now I want to set up a script that will replenish the character's HP after a set amount of time. (IE. 2 seconds.) I was reading about setInterval(); and clearInterval(); and it seemed to be the best thign to use, but I can't get it to work.
here is my code
CurrHP = 10;
HP = 50;
clearInterval(autoheal);
var autoheal = setInterval(function () {
if (CurrHP<HP) {
CurrHP += 5;
}
}, 2000);
trace(CurrHP);any help would be great!
Thanks in advance,
-Naezo
skadoo
July 12th, 2010, 02:18 AM
Well, I'm too dumb to know how to use that ._. BUT this should work until someone else shows up.
Set up a variable like... healTimer. if your movie is 12 frames per second, and you want two seconds, set it to 24
healTimer = 24;
onEnterFrame = function(){
if (currentHP >= maxHP){
currentHP = maxHP;
healTimer = 24;
} else{
healTimer--;
}
if (healTimer <= 0){
currentHP++;
}
}
That sets a 2 second timer to heal, makes you heal when it hits zero if your hitPoints are under the max, and resets the timer once you're fully healed. You can use something like this to heal 1HP every second, too. Just pointing you in the right (well, a work-adound's) direction
four4swords
July 12th, 2010, 02:52 AM
That looks like AS2.. So.. here's the AS3 equivalent! :P
var CurrHP:int = 10;
var HP:int = 50;
var healTimer:Timer = new Timer(2000,0);
healTimer.addEventListener(TimerEvent.TIMER,autohe al);
healTimer.start()
function autoheal (e:TimerEvent):void{
if (CurrHP<HP) {
CurrHP += 5;
}
trace(CurrHP);
}
Sorry.. I don't remember how to code in AS2 anymore..
Wanna consider moving over to AS3? Coding is more logical!
Naezo
July 12th, 2010, 02:59 AM
@skadoo: Thank you for that script, but it isn't exactly what I'm looking for, sorry. Thanks though!
@four4swords: I have and I'm not exactly sure. I have already started coding in AS2 and have a lot of experience. But, I will look into it. Thanks for your reply though!
EDIT: Actually, I have begun a port over of my game to AS3, so thank you four4swords!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.