PDA

View Full Version : [AS3] health system


Lav
12-28-2008, 01:10 PM
hello,

I'm trying to make a board game in flash and i want to use a sort of health system in my game, for example, if a player gets to a certain square, he will lose 1 hp but if he uses his hp-kit, everything will be repaired.

i first declared a globar var hp and made it 3, I then created 3 red squares next to eachother in a function

for(var i:int = 0; i<hp; i++)
{
this.graphics.beginFill(0xFF0000);
this.graphics.drawRect((kaderAvatar.x - 50)+i*15, 15, 15, 15);
this.graphics.endFill();
}


it works this far, but when i try to check what kind of square the player is on, and if it's the one where i need to reduce hp i simply use hp--; (the code to check what square its on works, i checked it with trace so no problem there)


now i'm a bit stuck where to go from here to make it actually work. I tried to make a different function that returns hp but i can't seem to make it work, anyone can help me?
And sorry if my english isn't that good

934texas
12-28-2008, 02:34 PM
Do you really want the number of squares to match the hitpoints? Anyway, what is your question? How to return your hitpoints?

public function getHP():Number {
return hp;
}

Lav
12-28-2008, 03:03 PM
if my activePlayer (my game has 2 players) hits a certain square, for example square14, then i want 1 (of the 3) hpbox to disappear so i thought to do it with just hp--;

this is how i check how i check if it's square 14 and that code works fine with other things

if(activePlayer.goalPosition == 14)
{
hp--;
trace(hp);
}


when i trace it it tells me that hp = 2 so the value of hp does change but i need to repaint with the new hp value so it should only paint 2 boxes.
But i don't know how to repaint it, i tried recalling the function and using Event.ENTER_FRAME.
pardon me if this seems like a trivial question, i'm still very new to AS3

Bunney lord
12-28-2008, 10:13 PM
I don't know AS3, but I might be able to help you. In the code that draws the squares, add:
this.graphics.clear();
before your drawing commands.

Lav
01-02-2009, 01:13 PM
thanks for the suggestion, it didn't really work but the deadline of the project has already passed so i did it without the health system. I'm still curious how i could have solved it tho, still quite a mystery to me :p

rrh
01-02-2009, 04:27 PM
If, instead of a global variable you made a health class and made a static reference to an instance of that class, then in this health class you could make public getters and setters for the hp value:

protected var _hp:int;

public function get hp():int {
return _hp;
}

public function set hp(newVal:int):void {
if (newVal!=_hp) {
_hp=newVal;
graphics.clear();
for(var i:int = 0; i<hp; i++)
{
...draw square
}
}
}