I'm working on a game for the iPhone using flash, and since memory is crucial i want to clean up displayObjects not needed. All the objects i need to delete is MovieClips taken from some array to another using splice(). Here is the code.
public function onTick(e:TimerEvent):void
{ randomNr = Math.random();
if ( randomNr > 0.9 )
{ var newFriend:Friend = new Friend( randomX, -15 ); newFriend.cacheAsBitmap = true; army.push(newFriend); addChild(newFriend); }
for (var i:int = 0; i < army.length;i++) { army[i].y += 3;
if (avatar.hitTestObject(army[i]))
{
mood = false;
TweenLite.to(army[i], .3, {x:308, y:458, scaleX:.7, scaleY:.7, ease:Expo.easeOut, onComplete:fadeFace, onCompleteParams:[army[i],mood]});
deleted.push(army.splice(i,1));
}
} }
private function cleanUp(e:MouseEvent):void
{ var totalDel:int = deleted.length; for(var i:int = 0; i < totalDel ;i++) { removeChild(deleted[i]); } trace(totalDel + " Dele from deleted"); }
My problem is that i get an error when trying to use the CleanUp function. I can trace all objects in the array and they show as [object Friend], but when trying to remove then from the displayList i get this Error: Error #1034: Type Coercion failed: cannot convert []@2c11309 to flash.display.DisplayObject.
Might be the wrong method im using!? Need some guidance please :)
A friend coder ended up handing me the perfect solution:
private function cleanUp(arr:Array):void
{
var toDelete:DisplayObject;
var totalDel:int = 0;
while(arr.length >0)
{
toDelete = arr[0];
toDelete.parent.removeChild(toDelete);
arr.shift();
totalDel++
}
//trace(totalDel + "deleted from array " + arr.length + " left");
}
This way all objects gets deleted without any collapsing the array, which is exactly what i needed... Hope this will help someone withe the same problem.