Highlighted

How do I delete a movieclip in an array from the stage

Community Beginner ,
Jul 21, 2019

Copy link to clipboard

Copied

I have an array that holds obstacles as they spawn and move across the screen, once they leave the screen I want to delete it and spawn a new random obstacle. I was looking this up earlier and what I found was to use splice. But this only seems to remove it from the array and its still lingering off screen, since players would never see this I didn't initially care but the game starts slowing down if the player gets far because of it.

for(var i in obstacles){

     obstacles.x += 3;

     if(obstacles.x>=800){

          //delete obstacles

          obstacles.visible = false;

          obstacles.splice(i, 1);

          roll01();//rng new obstacle

     }

}

depending on what random number is generated roll01() adds an obstacle to the array like this:

var ob:Obstacle01 = new Obstacle01();

ob.x = 0-stage.stageWidth;

ob.y = 0;

addChild(ob);

obstacles.push(ob);

//collectible items and their location

createGood(9,4);

createGood(19,5);

createGood(14,9);

createGood(4,15);

The collectible items are in a separate array  but are done pretty much the same way. What do I need to do to remove the out of view obstacles and collectibles completely from the stage so my game can run smoothly?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

you need to loop through your obstacles array from end to start when removing items or else you'll skip items:

for(var i=obstacles.length-1;i>=0;i--){

  1. obstacles.x += 3
  2.      if(obstacles.x>=800){ 
  3. removeFromStageF(obstacles);
  4.           obstacles.splice(i, 1); 

}

function removeFromStageF(mc:MovieClip):void{

if(mc.stage){  // error prevention

mc.parent.removeChild(mc);

}

}

p.s. it's not clear what your collectibles array is.

TOPICS
ActionScript

Views

120

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How do I delete a movieclip in an array from the stage

Community Beginner ,
Jul 21, 2019

Copy link to clipboard

Copied

I have an array that holds obstacles as they spawn and move across the screen, once they leave the screen I want to delete it and spawn a new random obstacle. I was looking this up earlier and what I found was to use splice. But this only seems to remove it from the array and its still lingering off screen, since players would never see this I didn't initially care but the game starts slowing down if the player gets far because of it.

for(var i in obstacles){

     obstacles.x += 3;

     if(obstacles.x>=800){

          //delete obstacles

          obstacles.visible = false;

          obstacles.splice(i, 1);

          roll01();//rng new obstacle

     }

}

depending on what random number is generated roll01() adds an obstacle to the array like this:

var ob:Obstacle01 = new Obstacle01();

ob.x = 0-stage.stageWidth;

ob.y = 0;

addChild(ob);

obstacles.push(ob);

//collectible items and their location

createGood(9,4);

createGood(19,5);

createGood(14,9);

createGood(4,15);

The collectible items are in a separate array  but are done pretty much the same way. What do I need to do to remove the out of view obstacles and collectibles completely from the stage so my game can run smoothly?

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

you need to loop through your obstacles array from end to start when removing items or else you'll skip items:

for(var i=obstacles.length-1;i>=0;i--){

  1. obstacles.x += 3
  2.      if(obstacles.x>=800){ 
  3. removeFromStageF(obstacles);
  4.           obstacles.splice(i, 1); 

}

function removeFromStageF(mc:MovieClip):void{

if(mc.stage){  // error prevention

mc.parent.removeChild(mc);

}

}

p.s. it's not clear what your collectibles array is.

TOPICS
ActionScript

Views

121

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 21, 2019 0
Adobe Community Professional ,
Jul 21, 2019

Copy link to clipboard

Copied

Hi.

You need to remove the object from the display list as well.

So it would be something like this:

obstacles.parent.removeChild(obstacles);

obstacles.splice(i, 1);

Regards,

JC

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 21, 2019 1
Adobe Community Professional ,
Jul 21, 2019

Copy link to clipboard

Copied

you need to loop through your obstacles array from end to start when removing items or else you'll skip items:

for(var i=obstacles.length-1;i>=0;i--){

  1. obstacles.x += 3
  2.      if(obstacles.x>=800){ 
  3. removeFromStageF(obstacles);
  4.           obstacles.splice(i, 1); 

}

function removeFromStageF(mc:MovieClip):void{

if(mc.stage){  // error prevention

mc.parent.removeChild(mc);

}

}

p.s. it's not clear what your collectibles array is.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 21, 2019 1
Community Beginner ,
Jul 21, 2019

Copy link to clipboard

Copied

Thank you! The collectibles array is movieclips that when the player touches they get points and then it disappears, its handled the same way as obstacles just with some different if conditions, what you suggested works for the collectibles array too, thank you for the help!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 21, 2019 0
kglad LATEST
Adobe Community Professional ,
Jul 21, 2019

Copy link to clipboard

Copied

you’re welcome.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 21, 2019 0