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 {

	private function setupEnemy():Void {
		enemy = new Enemy();
		enemy.y = Math.random()* stage.stageHeight;
		if (enemy.x == 700) {

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:

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) 

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 act.Walker;

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

	public function update(event:Event):Void {

	public function draw():Void {;;, 0, 32, 32);;

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 {


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.


1 Like

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

many thanks :wink: