1

i want to remove my object out of viewcontainer when i hit my object hit works, removing out of array also, but i doesn't want to remove it from stage. because index in the array is null. ok but how do you remove it then?

thanks

private function enemySpawnen():void
{
var enemyShip:SpaceShip = new SpaceShip();

Enemy[0] = enemyShip;
Enemy[1] = enemyShip;
}

private function renderEnemy(e:Event):void
{
if(Enemy[0] != null){
viewContainer.addChild(Enemy[0]);

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

Enemy[0].x = 600;
Enemy[0].y = 400;
}
else{
viewContainer.removeChild(Enemy[0]);==> problem
}
}

if(spaceBar){
var kogel:Kogel = new Kogel();
viewContainer.addChild(kogel);

kogel.scaleX = 0.08;
kogel.scaleY = 0.08;

kogel.x = heli.x + (heli.width/2) -225;
kogel.y = heli.y + (heli.height/2) + 30;

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

try{
if(kogel.hitTestObject(Enemy[0])){
Enemy.splice(0,1);
}
}
catch(e:Error){

}
}
3
  • You else statment with your error will only ever run when the item is null, so it will always throw an error Commented Nov 9, 2012 at 17:54
  • You should removeChild on the line before you splice the array Commented Nov 9, 2012 at 17:55
  • Why are there two copies of this question? stackoverflow.com/questions/13314520/… Commented Nov 9, 2012 at 21:20

1 Answer 1

0

Your else statment that contains your error will only ever run when the item Enemy[0] is null, so it will always throw an error and is completely unnecessary (you might as well remove the whole else statement)

Enemy[0] is null because you've spliced it out your array. What you need to do is have the line that is throwing the error, before you splice the array.

if(kogel.hitTestObject(Enemy[0])){
    viewContainer.removeChild(Enemy[0])
    Enemy.splice(0,1);
}

Also keep in mind, now that you've spliced the array, the item that used to be Enemy[1] is now referenced by Enemy[0] as your array indicies have shifted.

Sign up to request clarification or add additional context in comments.

Comments

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.