Skip to main content
Known Participant
February 5, 2015
Question

If statement & specific array element

  • February 5, 2015
  • 1 reply
  • 465 views

So for a simple game I need to remove 1 life/heart when the player collides with the wall.

The first heart works, but the second only should be invisible after the first is already gone, and so on.

How do I state that? I now tried it with  if-statements in a row, but that's probably not the way to do it.

And how do I equate with alpha and an array element? Or is there a more straight forward way?

I'd really appreciate your help! I'm still a beginner.

var wallArray:Array = new Array(wall1,wall2,wall3,wall4, wall5,sidewall1, sidewall2, sidewall3, sidewall4);

var heartArray:Array = new Array (heart1, heart2, heart3);

function collisionDetectPlayer(){

  for(var i:uint = 0; i < wallArray.length;i++){

  var wallTemp:MovieClip = wallArray;

  if(player.hitTestObject(wallTemp)){

  movePlayerBack();

  heartArray[2].alpha = 0;

  if ((heartArray[2]) < (alpha = 1)){

  heartArray[1].alpha = 0;

  if ((heartArray[1]) < (alpha = 1)){

  heartArray[0].alpha = 0;

  }

  }

  }

  }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 5, 2015

you should use a variable to track the number of lives lost (or remaining) and use that variable in your array.

Known Participant
February 5, 2015

how do you mean? do you have an example?

kglad
Community Expert
Community Expert
February 5, 2015

var lives:int=10;

function collisionDetectPlayer(){

  for(var i:uint = 0; i < wallArray.length;i++){

  var wallTemp:MovieClip = wallArray;

  if(player.hitTestObject(wallTemp)){

lives--;

if(lives<=0){

gameOverF();

}

  movePlayerBack();

heartArray[lives-1].alpha=0;  // or heartArray[heartArray.length-lives+1].alpha=0

  }

  }

}

p.s. i just noticed your if-statements have a few errors.  so, in the future you should use something like

if(heartArray[whatever].alpha<.5){

//do whatever

}