Destroy an enemy without destroying all of them


(Andressa Rebeca Pinheiro) #1

Hi guys,

I am creating a game based on MVC with flash punk. I add 5 enemies per time in the world but when I want to destroy one enemy by a collision with the bullet, all the enemies are destroyed at the same time.

This is my code in the Enemy class:

	override public function update():void{
		
		trace("My obstacle was added");
					
				this.x += speed;
				this.y += 0.4;
				/*var power : Power = Power(this.collideTypes([Power.power_type], this.x, this.y));
			
				if(power != null){
					
					power.destroy(); */
		   if ((this.x > FP.screen.width) || (collide("power", this.x, this.y))){ // remove se pegar em alguma coisa
			
			this.world.remove(this);
			
			}			

I would like that just one per time was destroyed.

How do I do that?


(Bora Kasap) #2

you may try collide("power", this.x, this.y) != null sometimes works, actually @jacobalbano was explained something about that but i don’t remember right now


(Andressa Rebeca Pinheiro) #3

Hi AlobarNon,

Thank you for reading. I tried to do this, but it didn’t work. :confused:


(Jacob Albano) #4

That shouldn’t make a difference in Flash. It’s my personal preference because I use a few other languages that don’t allow null to be checked as a boolean.


(Andressa Rebeca Pinheiro) #5

I don’t know what it is happening…

How I create enemies in my world function update :

if(enemy_count <= 5){   
			this.add(new Enemy(FP.rand(FP.screen.width-100), FP.rand(FP.screen.height)));
			enemy_count ++;
			}

in enemy function update I have :

 if ( (collide("power", this.x, this.y))!= null){ 
				
				this.world.remove(this);
				
				}	

Enemy constructor :

	public function Enemy(a:Number, b:Number) {
		
		this.type = enemy_type;
		this.graphic = new Image(EXPLO);
		this.setHitbox(95,48);
		graphic.x = a;
		graphic.y = b;
	}

:frowning:


(Bora Kasap) #6

Could you put your code without “comments” inside it? Also “power.destroy();” function too?


(Ultima2876) #7

Are you using the a and b arguments to set the x/y position of the entity? The code above looks like it is only setting the x/y of the graphic, not the entity itself. This means that despite your enemy graphics being drawn at the correct positions, the entities still all exist at 0, 0, meaning they will all collide at the same time (collide uses the entity position, not the graphic position).

You might want something like this instead:

public function Enemy(a:Number, b:Number) {
		// constructor code
		//super( FP.rand(FP.screen.width/2)+ 70 , FP.screen.height +10, graphic , mask);
		this.type = enemy_type;
		this.graphic = new Image(EXPLO);
		this.setHitbox(95,48);
		this.x = a;
		this.y = b;
	}

(Andressa Rebeca Pinheiro) #8

Hi guys. Thank you for your help.

Ultima2876 . It was exactly what you said :

the x/y of the graphic, not the entity itself. This means that despite your enemy graphics being drawn at the correct positions, the entities still all exist at 0, 0, meaning they will all collide at the same time (collide uses the entity position, not the graphic position).

I changed for

this.x = a; this.y = b; }

in all my entities .

Thank you. :smile:


(Willard Torres) #9

The first language that came to my mind was Haxe


(Jacob Albano) #10

That’s the one! I also do a lot of work in C#.


(Ultima2876) #11

C# is wonderful. I’m actually going to be working with Unity this year and I’m looking forward to working with C# again.