Skip to main content
January 8, 2014
Answered

Lifesystem in as3

  • January 8, 2014
  • 1 reply
  • 313 views

Hello,

I'm trying to make a game and within that game i'd like a lifesystem. The player has 3 lifes (shown as 3 hearts in the game). This is what I have so far:

var lifes:int = 3;

function checkForHits() {

if(Player.hitTestObject(Zombie)) {
Heart3.x=1100;
lifes --;
Player.x=200,Player.y=600;

}

if (lifes == 0) {

gotoAndStop("gameover");

}

It works, after 3 deaths I progress to the gameover screen. My problem is: how do I show that graphically? The first heart (called heart3) dissapears as I want it to, but I have no clue how to do the same with the second and third heart. Any suggestions?

This topic has been closed for replies.
Correct answer moccamaximum

you can reference objects with this construction

var num:int = 5;

this["object"+String(num)].visible = false;

//this will make any displayObject with the instance name object5 invisible on th stage;

in your case:

if(Player.hitTestObject(Zombie)) {
this["Heart"+String(lifes)].x=1100;
lifes --;

}

this is an easy way to tie your UI to the actual gaming data

1 reply

moccamaximumCorrect answer
Inspiring
January 8, 2014

you can reference objects with this construction

var num:int = 5;

this["object"+String(num)].visible = false;

//this will make any displayObject with the instance name object5 invisible on th stage;

in your case:

if(Player.hitTestObject(Zombie)) {
this["Heart"+String(lifes)].x=1100;
lifes --;

}

this is an easy way to tie your UI to the actual gaming data