0

the problem is when i shoot down my enemy he keeps re-spawning. probably something i overlooked.

i can shoot it, it gets removed from stage but gets rerenderd because it still exist in my array.

thanks

private function enemySpawnen():void
    {           
        for(var i:uint = 0; i < hoeveelheidEnemy;i++){
            var enemyShip:SpaceShip = new SpaceShip();
            Enemy[i] = enemyShip;
        }
    }

private function renderEnemy(e:Event):void
    {           
        for(var i:uint = 0; i < hoeveelheidEnemy;i++){              
            if(Enemy[i] != null){
                viewContainer.addChild(Enemy[i]);

                Enemy[i].scaleX = 0.5;
                Enemy[i].scaleY = 0.5;

                Enemy[i].x = 500;
                Enemy[i].y = 400 - i*100;
        }
        }
    }

function shoot(e:Event):void
            {
                kogel.x +=10;

                try{
                    for(var i:uint = 0; i < hoeveelheidEnemy;i++){
                        if(kogel.hitTestObject(Enemy[i])){

                            Enemy.splice(i,0);==>problem when i set it to 1 or i then nothing happens
                            viewContainer.removeChild(Enemy[i]);
                            //trace("hit");
                        }
                    }
                }
                catch(e:Error){

                }
            }

My source code can be found here: http://dl.dropbox.com/u/50815831/Nieuwe%20map.zip

6
  • you need to splice AFTER you removeChild. once you splice i no longer indicates the item you want. Commented Nov 9, 2012 at 19:22
  • what is hoeveelheidEnemy? your for loop should be using Enemy.length instead of hoeveelheidEnemy Commented Nov 9, 2012 at 19:23
  • yes but i still got some problems with i,1 if i destroy something on index 1 it removes also index2 Commented Nov 9, 2012 at 19:28
  • hoeveelheidEnemy = numbers enemy = 5 Commented Nov 9, 2012 at 19:29
  • you still have problems even after swapping the placement of your splice and removeChild lines? Commented Nov 9, 2012 at 19:48

1 Answer 1

1

You need to remove the child BEFORE you splice the array. You also need to iterate backwards so your indices don't change mid loop.

Try changing your for loop to the following:

for(var i:uint = Enemy.length-1; i >= 0;i--){
    if(kogel.hitTestObject(Enemy[i])){
        viewContainer.removeChild(Enemy[i]);
        Enemy.splice(i,1); //do this after the above line, otherwise you're actually removing doing removeChild on the next item in the array
        kogel.removeEventListener(Event.ENTER_FRAME, shoot); //stop the frame handler
        break; //abandon the rest of this loop
    }
}

If Kogel is a bullet and you want it to only affect one enemy, then you need to break out of the loop once it destroys something.

As a tip for good practice. you should encapsulate the shoot function/enter frame handler into your Kogel class and just pass in the enemy array.

Something else I've noticed, your have your renderEnemy function run every frame, which is completely uneccessary, and causing undesired positioning. Delete that function all together and put the code into your enemySpawned() method:

private function enemySpawnen():void
    {
        for(var i:uint = 0; i < hoeveelheidEnemy;i++){
            var enemyShip:SpaceShip = new SpaceShip();
            Enemy[i] = enemyShip;
            viewContainer.addChild(Enemy[i]);

                Enemy[i].scaleX = 0.5;
                Enemy[i].scaleY = 0.5;

                Enemy[i].x = 500;
                Enemy[i].y = 400 - i*100;
        }
    }
Sign up to request clarification or add additional context in comments.

8 Comments

I can tell you that you HAVE to use splice(i,1)...that second argument says "remove 1 element starting at index i". splice(i,0) is "remove 0 elements starting at index i").
it still removes all the above indexes index 1 will also remove index2
should something bin changed? i replaced it, is see a performance increase but my original problem have you a solution for that?
is kogel a bullet? if so see my updated answer again. I don't understand your last comment.
sry it didn't change the awnser untill now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.