Removing child is just... confusing me

howdy peeps,

been a while since ive asked anything. been busy :wink:

but something that has been confusing me lately is removeChild and using it rightly. but all i have is an enemy that is being spawned, goes from one side of the screen to the other, and when it reaches a point, should delete/remove itself. but on my many MANY attempts, its just not working for some strange reason. ill post code below to show

this is from my PlayState.hx

private function setup():Void {
		setupPlayer();
		setupEnemy();
		setupEvent();
	}

	private function setupEnemy():Void {
		enemy = new Enemy();
		enemy.y = Math.random()* stage.stageHeight;
		addChild(enemy);
		if (enemy.x == 700) {
			removeChild(enemy);
			trace('phpbkt');
		}
	}

i would have thought this would have worked. and it does to some degree, because the enemy is moving from left to right, but when it reaches anything, it is not deleting. its bothering the tits off me, i cant figure it out, which is weird

cheers if anyone can point out my silliness :wink:

EDIT
now putting it at <= to 700 works [but that means its deleting right away], but anything like == or >= just doesnt and thats whats confusing me

If you are moving “enemy” constantly you have to check it’s position constantly as well. I don’t know how the rest of your code looks like but something like this would work:

function enterFrame(e:Event)
{
      enemy.x += 10;
      if (enemy.x >= 700) 
      {
            removeChild(enemy);
      }
}

cool spot .ill give that a bash on.

what i have is a Walker.hx [which is just sending a constant signal to enemy.x], which is then linked to my Enemy.hx
this is the Enemy.hx

package char;

import openfl.display.Sprite;
import openfl.events.Event;

import act.Walker;

class Enemy extends Sprite {
	private var walker:Walker;
	public function new() {
		super();
		walker = new Walker();
		this.addEventListener(Event.ENTER_FRAME, update);
	}

	public function update(event:Event):Void {
		draw();
		walker.step();
	}

	public function draw():Void {
		this.graphics.clear();
		this.graphics.beginFill(0x00FFBC);
		this.graphics.drawRect(walker.x, 0, 32, 32);
		this.graphics.endFill();
	}
}

thanks again

Maybe something like this?

In a higher class

addEventListener (Event.ENTER_FRAME, this_onEnterFrame);

....

private function this_onEnterFrame (event:Event):Void {
    
    var i = 0;
    var enemy;
    
    while (i < enemies.length) {
        
        enemy = enemies[i];
        enemy.update ();
        
        if (enemy.x > stage.stageWidth) {
            
            removeChild (enemy);
            enemies.remove (enemy);
            
        } else {

            i++;
         
        }
    
}

There are ways this could be more elegant (accounting for other reasons that an enemy should be removed, doing a remove animation instead of just removing from the display list, etc) but as a basic concept, this might help?

It is ideal to keep your Event.ENTER_FRAME listeners to a minimum for performance, as well as control over your application. By putting a single listener at a higher-level, it gives you control over whether you will translate the frame into an update, or ignore it (such as if you implement a pause button)

Then as you update the enemies, you can have logic that determines if they should be removed. Usually smarter to put this logic at a higher level (that’s thinking about the game area size) then to make each enemy think about this.

:slight_smile:

1 Like

hmmm. cool. ill bookmark this and have a look-see.

many thanks :wink: