Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-12-2012, 03:48 PM   #1
parsihaa
Registered User
 
Join Date: Apr 2012
Posts: 8
Smile less speed because of a lot of child

hello.
I have an movieClip form an array named object1 and another movieClip from another array named object2 and if object1 hit object2, both will remove.
my code is like this:
ActionScript Code:
var object1:Array = new Array; var object2:Array = new Array; for (var f:uint = 0; f < object1.length; f++) {  for (var i:uint = 0; i < object2.length; i++)  {   if (object1[f].hitTestObject(object2[i]))   {    if (stage.contains(object1[f]))    {     removeChild(object1[f]);    }    if (stage.contains(object2[i]))    {     removeChild(object2[i]);    }   }  } }
but my problem is when the two child from 2 arrays (object1 & object2) removeChild from stage they will be invisible not remove and functions for them won't be stop. so when the children increase to 100 or more stage speed slow down (because of huge number of children is stage). If anyone knows a solution for this please notice me.
parsihaa is offline   Reply With Quote
Old 04-12-2012, 03:57 PM   #2
Biodiscus
Immma firing Ninnnnjaaa
 
Join Date: Aug 2011
Posts: 95
Default

throw a object2[i].removeEventListener(....); in it. BTW if you want to use alot of objects on stage, but it lags flash. Try to use Stage3D (Molehill)
Biodiscus is offline   Reply With Quote
Old 04-12-2012, 04:18 PM   #3
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

the contains method is very slow and expensive. Never use it especially in a loop.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 04-12-2012, 04:22 PM   #4
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,679
Send a message via Skype™ to henke37
Default

Start by never using the contains method, it is never the condition that you really care about.

As for the issue, you should indeed start removing stuff from the arrays.
ActionScript Code:
function removeFromVector(v,e):void { var pos:int=v.indexOf(e); if(p<0) throw new ArgumentError("E was not in v!"); v.splice(p,1); }
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 04-12-2012, 04:29 PM   #5
parsihaa
Registered User
 
Join Date: Apr 2012
Posts: 8
Default

thanks for reply. but I can't understand the code you gave me. if you can give me an example with my arrays name and hitTestObject, It will really help me to find the way to use your code.
parsihaa is offline   Reply With Quote
Old 04-13-2012, 05:08 AM   #6
parsihaa
Registered User
 
Join Date: Apr 2012
Posts: 8
Default

Any suggestion how to remove specific child in arrays instead of removeChild???
parsihaa is offline   Reply With Quote
Old 04-13-2012, 01:18 PM   #7
dmb85
Senior Member
 
Join Date: Jan 2012
Posts: 590
Post

The code henke posted is what you want (except the variable "p" needs to be "pos"). You find the index of the item in the array, then use the splice method on the array to remove that item.

To use it, you'd write:
ActionScript Code:
try{       removeFromVector(yourArrayName, theItemToRemove); } catch(err:ArgumentError){       //put any code that should happen here if the item is not found in the array }

Last edited by dmb85; 04-13-2012 at 01:21 PM.
dmb85 is offline   Reply With Quote
Old 04-15-2012, 10:41 AM   #8
parsihaa
Registered User
 
Join Date: Apr 2012
Posts: 8
Default

I used the code you said but It didn't work because I don't know how to use it yet. so if someone can notice my mistake in the code I wrote here, It will really help me with removing child in array (my array name is enemys1 and I want remove child when child.live is less than 0)
ActionScript Code:
for (var e1:uint = 0; e1 < enemys1.length; e1++) {  if (enemys1[e1].live <= 0)  {   try   {    removeFromVector(enemys1, e);   }   catch (err:ArgumentError)   {    trace("E was not in v!");   }   function removeFromVector(enemys1,e):void   {    var pos:int = enemys1.indexOf(e);    if (pos < 0)    {     throw new ArgumentError("E was not in v!");    }    enemys1.splice(pos,1);   }  } }
parsihaa is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:48 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.