hitTest, nested if/then

Still working on my game. I’m having a little trouble with my collision test. I got everything to work, but I found that if I hit an alien more than once it keeps racking up points and sends the alien explosion animation back to frame one. I tried putting a variable into the alien movie clip to determine if the alien had already been hit or not. Then I put the following commands into my laser shot movie clip:

  1. for (i=1; i<=_root.numEnemy; i++){
  2. if (this.hitTest( _root["enemy"+i])){
    
  3.      this.shotSpeed=0;
    
  4.      this.play();
    
  5.       if(_root["enemy"+i].enemyDead="no"){ 
    
  6.              _root.score+=100;
    
  7.           _root["enemy"+i].gotoAndPlay( 13 );
    
  8.              _root["enemy"+i].enemyDead="yes";
    
  9. 	      }
    
  10.  }
    

12.}

If I hit an enemy, the laser stops and blows up, like you’d expect (lines 4 and 5). However, the alien doesn’t explode anymore. It’s not getting past the “if” statement on line 6.

I have a line in the alien movie clip for enemyDead=“no”.

Very strange indeed. You say it doesn’t get past the 6th line, when it automatically should. To test the value of enemyDead, you have to put ==

if(_root["enemy"+i].enemyDead=="no")

Otherwise it takes the value 0 and is evaluated as true.

Last thing, don’t use strings like no and yes, but rather 0 and 1 or true and false. Cleaner =)

pom :asian:

thanks, that seemed to help, but now I have a different problem. Every once in awhile I’ll get an alien that is invincible. When I hit him I’ll get 100 points, so it’s getting past the if statement. But then it won’t die. Seems to be about one in 10. Here’s the code.

for (i=1; i<=_root.numEnemy; i++){

 if (this.hitTest( _root["enemy"+i])){
	this.shotSpeed=0;
	this.play();
	if (_root["enemy"+i].enemyDead==false){
	_root.score+=100;
	_root["enemy"+i]. enemyDead=true;
	_root["enemy"+i].gotoAndPlay(13);
	
   }
  
  }
  
}

It’s like it won’t get down to the gotoAndPlay (13) line, but I know it must be because I have a sound effect on frame 13 of the alien movie clip and it plays the first time I hit one of these invincible guys.

Maybe I should just leave it be, makes the game more interesting. But it bugs me to not know what’s going on.

Also, off topic, I have a problem with the game slowing down permenently if I hold down the fire button and let loose a lot of bullets. I’m removing the bullet movie clips when they get to a certain point, so I don’t think there’s a problem with too many clips on the stage at one time.