Action Script 3: Addchild within an already added child issue - actionscript-3

My issue is that i get an error when my function addSpotlight is called.
TypeError: Error #1010: A term is undefined and has no properties.
at BubbleBoy_fla::MainTimeline/addSpotlight()
at BubbleBoy_fla::MainTimeline/policeHeli()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
My code is this:
//Adding Helicopter
function addHelicopter(xLocation: int, yLocation: int, passSpeedX, passSpeedY): void {
var helicopter: Helicopter = new Helicopter(xLocation, yLocation, passSpeedX, passSpeedY);
back.addChild(helicopter);
heliList.push(helicopter);
playFly();
}
//Adding Spotlight
function addSpotlight(xSpotlightLocation: int, ySpotlightLocation: int, spotlightSpeedX): void {
var spotlight: Spotlight = new Spotlight(xSpotlightLocation, ySpotlightLocation, spotlightSpeedX);
back.helicopter.addChild(spotlight);
spotlightList.push(spotlight);
}
The reason i am doing this is because the helicopter can be destroyed and the spotlight gets removed at the same time, but if you don't destroy the helicopter and the spotlight hits you then you are caught.
I previously had my code just add the spotlight to the back container then i lined it up with the helicopter, but then this would only remove the first spotlight in the array.
if (heliList.length > 0) {
for (var h: int = 0; h < heliList.length; h++)
for (var l: int = 0; l < spotlightList.length; l++) {
if (heliList[h].hitTestPoint(boy.x + upBumpPoint.x, boy.y + upBumpPoint.y, true)) {
trace("SCORE POINTS")
score += 100;
playCrash();
playFly();
heliList[h].removeHeli();
spotlightList[l].removeSpotlight();
//policeTimer.start();
}
}
}
Any suggestions on these 2 issues??

In addSpotlight use:
trace('spotlight: ', spotlight);
trace('back: ', back);
trace('back.helicopter: ', back.helicopter);
trace('spotlightList: ', spotlightList);
Whatever is missing - it causes the issue :)

Related

How to call the events simultaneously in drag and drop event of multiple objects in as3

I have totally 15 Movieclips in one array and another 3 Movieclips in another array. My goal is to drag one of the movieclip in the first array and need to set to another position. After setting position next event should be called, but my problem is that another movieclip can also accessible during the first event called.
Below I have attached the events in As3 Code:
//Assigned movieclips in array
goat_Array =[goat1,goat2,goat3,goat4,goat5,goat6,goat7,goat8,goat9,goat10,goat11,goat12,goat13,goat14,goat15];
tiger_Array = ["",tiger1,tiger2,tiger3];
for (p = 0; p < goat_Array.length; p++)
{
goat_Array[p].addEventListener(MouseEvent.MOUSE_DOWN,goat_select);
goat_Array[p].addEventListener(MouseEvent.MOUSE_UP,goat_release);
}
public function goat_select(event:MouseEvent):void
{
/*var originalIndex = this.getChildIndex(current_goat);*/
for (goat_count = 1; goat_count <= 15; goat_count++)
{
if (event.target == this["goat" + goat_count])
{
this["goat" + goat_count].startDrag(true);
current_goat = event.target as MovieClip;
}
}
}
for (q = 0; q < tiger_Array.length; q++)
{
tiger_Array[q].addEventListener(MouseEvent.MOUSE_DOWN,tiger_select);
tiger_Array[q].addEventListener(MouseEvent.MOUSE_UP,tiger_release);
}
for (tiger_count = 1; tiger_count <= 3; tiger_count++)
{
if (event.target == this["tiger" + tiger_count])
{
this["tiger" + tiger_count].startDrag(true);
current_tiger = event.target as MovieClip;
}
}
How to check these two events should be called and what are the conditons need to be checked to trigger both these event?
Thanks in advance.
Delete this:
for (q = 0; q < tiger_Array.length; q++)
{
tiger_Array[q].addEventListener(MouseEvent.MOUSE_DOWN,tiger_select);
tiger_Array[q].addEventListener(MouseEvent.MOUSE_UP,tiger_release);
}
and insert it in goat_release function. In this way once the first goat is droped - tigers can be moved. But I'm still not sure what are you trying to accomplish...

Flash Action Script 3.0 error 1120

So I have come to a point where I have to ask a question, I've already went to 3 pages of google for hours now. I hope you guys can help me out.
I'm pretty sure i've defined var UsedSlot but it says error 1120.
i've already tried putting it on different lines, like before and after the timer. I'm just a newbie and I need this for my term project.
Here are the errors
Scene 1, Layer 'Player', Frame 1, Line 55 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 62 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 90 1120: Access of undefined property UsedSlot.
Scene 1, Layer 'Player', Frame 1, Line 91 1120: Access of undefined property UsedSlot.
stop();
import flash.display.MovieClip;
import flash.events.Event;
import fl.motion.MotionEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
//initialize objects
var PlayerItems:Array = [new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),
new Items_Player_Cheese(),];
//hard code test ***working***
/*PlayerItems[0].x = Guide_Test_Start.x;
PlayerItems[0].y = Guide_Test_Start.y;
stage.addChild(PlayerItems[0]);*/
///////////////////
var j:int;
var SlotUsed:Array = new Array(PlayerItems.length);
for(j = 0 ; j < SlotUsed.length; j++)
{
SlotUsed[j] = -1
trace(SlotUsed[j]);
}
for(j = 0; j < PlayerItems.length; j++)
{
PlayerItems[j].x = Guide_Test_Start.x;
PlayerItems[j].y = Guide_Test_Start.y;
}
var PlaceOccupied:Array = [false,false,false,false,false];
//check if a place is unoccupied
var myTimer:Timer = new Timer(3000);
myTimer.addEventListener(TimerEvent.TIMER, CheckVacancy);
myTimer.start();
//choose a starting location
var CVSync:int;
function CheckVacancy(e:TimerEvent)
{
for(CVSync = 0 ; CVSync < 5; CVSync++)
{
if(PlaceOccupied[CVSync] == false)
{
// Put an object
var ItemIndex = randomRange(0,PlayerItems.length - 1);
while(UsedSlot[ItemIndex] > -1)
ItemIndex = randomRange(0,PlayerItems.length - 1);
trace("Item["+ItemIndex+"] placed on ["+CVSync+"]");
PlayerItems[ItemIndex].x = StartGuide[CVSync].x;
PlayerItems[ItemIndex].y = StartGuide[CVSync].y;
UsedSlot[j] = ItemIndex;
PlaceOccupied[CVSync] = true;
trace("Vacant Slot Filled" + getTimer() + " ms");
break;
}
}
}
//Instantiate the objects
for(j = 0; j < PlayerItems.length; j++)
{
trace("Adding Player Items");
stage.addChild(PlayerItems[j]);
}
//Add Listener To determine A hit
for(j = 0; j < PlayerItems.length; j++)
{
PlayerItems[j].addEventListener(MouseEvent.MOUSE_UP, MUp);
}
function MUp(e:MouseEvent):void
{
for(j = 0; j < PlayerItems.length; j++)
{
if(PlayerItems[j].hitTestObject(MyItem[0]))
{
CVSync[UsedSlot[j]] = false;
UsedSlot[j] = -1;
PlayerItems[j].x = -100;
trace("Point by: " + MyItem[0].name.toString());
break;
}
}
}
/*cheese.addEventListener(MouseEvent.MOUSE_UP, MUp);
function MUp(e:MouseEvent):void{
if(cheese.hitTestObject(MyItem[0]))
{
trace("Cheese Eaten by: " + MyItem[0].name.toString());
cheese.x = Guide_Test_Start.x;
cheese.y = Guide_Test_Start.y;
}
}*/
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
I'm pretty sure i've defined var UsedSlot
No, you don't and that's the problem here.
The name UsedSlot appears 4 times in the snippet you posted, which are the lines that cause the 4 errors.
None of them defiens the variable.
You do however define another variable:
var SlotUsed:Array
in your code.
If this is unclear: these are two different variables. Just because you define a variable SlotUsed somewhere in your code doesn't mean you can access it via some other name, like UsedSlot.
This is case sensitive, which means SlotUsed and slotUsed are not considered to be the same.
As an advice, "being pretty sure" is usually worthless when it comes to finding problems like this. Instead, prove it by finding the declaration. If you cannot find it, it's probably not there. After all, the compiler cannot find it.
It's a common convention to start variable names with a small letter, which helps to distinguish them from names of classes (or interfaces...).
While it's your choice to code however you want, it's easier for others to read your code if you stick to common conventions and therefore simplifies the task of helping you out. =)

HitTest Function with two parameters not initializing

Hey guys so Basically what I'm trying to do is when my player collides with all 5 of the points MovieClips in an array and the Goal Movie Clip i want a text to appear saying "Perfect. But i cant quite accomplish this. I set up the function and it seems like it would but i think what might be wrong is the hitTest with all the Movie clips in the array.
Here is how i set it up:
This is in my loop Function:
private function gameLoop(e:Event):void
{
perfectTextFunction();
}
This is the function:
private function perfectTextFunction():void
{
if (player.hitTestObject(mcGoal_1 && points))
{
trace("perfect Text");
mcPerfect = new perfectText();
mcPerfect.x = (stage.stageWidth / 2);
mcPerfect.y = (stage.stageHeight/ 2);
stage.addChild(mcPerfect);
}
}
The Trace doesn't pick anything up.
Here is how the points are added to the stage if needed:
public function addPointsToStage():void
{
for (var i = 0; i < nPoints; i++)
{
trace(aPointsArray.length);
points = new mcGainPoints();
stage.addChild(points);
points.x = startPoint.x + (xSpacing * i);
points.y = startPoint.y - (ySpacing * i);
aPointsArray.push(points);
}
}
Please any help would be appreciated! THank you!
VESPER here is how I made the NESTED LOOP:
//If all points are hit then Perfect Hit Text
if (player.hitTestObject(mcGoal_1 || mcGoal_2))
{
var weHitAll:Boolean = true;
for (var s in aPointsArray)
{
weHitAll = weHitAll && player.hitTestObject(aPointsArray[s]);
if (!weHitAll)
break;
}
if (weHitAll)
{
trace("perfect Hit");
mcPerfect = new perfectText();
mcPerfect.x = (stage.stageWidth / 2);
mcPerfect.y = (stage.stageHeight/ 2) - 80;
stage.addChild(mcPerfect);
nScore += 25;
updateHighScore();
}
}
var weHitAll:Boolean=true;
for (var s in aPointsArray) {
weHitAll = weHitAll&&player.hitTestObject(aPointsArray[s]);
if (!weHitAll) break; // missed one, drop cycle
}
if (weHitAll) {
trace('Perfect hit!');
... // etc
}
Function hitTestObject accepts only a single object as parameter, in order to check against an array you need to iterate through it, checking against one object at a time (here it's in aPointsArray[s]).

How can I get rid of non-null hitTestObject error with already instantiated objects?

I am a beginner/intermediate AS3 "programmer" trying to complete a skeeball-like flash-based game for a university assessment, and I'm doing myself serious mental damage trying to get even basic object (ball) collisions working with scoring targets on my stage. I'm using a power bar type variable to determine the force of the ball roll which is translated into a tween to create a smooth movement up my "lane" and into the scoring area (this is overhead perspective). The targets are instances of movie clips inside a larger movie clip that is made up of all the components of the game table. Even though my game table and scoring components have already been instantiated when I release the ball, I am getting the typical non-null errors:
ballSpeed is 552
targetArray value is [object upperScoringAreaCTarget_mc]
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at SkeeBlast_7_fla::MainTimeline/ballTargetScore()
at SkeeBlast_7_fla::MainTimeline/rollBall()
at SkeeBlast_7_fla::MainTimeline/releaseBall()
Here is my ball release function:
function releaseBall(event:MouseEvent):void
{
rollBall();
gameElements.removeEventListener(MouseEvent.MOUSE_MOVE, moveBall);
}
function rollBall():void
{
ballSpeed = rollPower * 12;
trace("ballSpeed is " + ballSpeed);
ballFriction();
ballGravity();
//ball.y -= ballSpeed;
//var myBallTween:Tween = new Tween(ball,"y",Strong.easeOut,ball.y,ball.y - ballSpeed,3,true);
myBallTween = new Tween(ball,"y",Strong.easeOut,ball.y,ball.y - ballSpeed,3,true);
myBallTween.start();
ballTargetScore();
}
and my collision detection and scoring function:
//match targets to scoring values which should be tied to determineScore()
function ballTargetScore():void
{
var targetValue:String;
var targetArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaCTarget,
gameTable.upperScoringArea.upperScoringAreaLtTarget,
gameTable.upperScoringArea.upperScoringAreaRtTarget,
gameTable.middleScoringArea.middleScoringAreaTargetTop,
gameTable.middleScoringArea.middleScoringAreaTargetMiddle,
gameTable.middleScoringArea.middleScoringAreaTargetLower,
gameTable.lowerScoringArea.lowerScoringAreaTarget);
if (ball.hitTestObject(gameTable.tableDisplay))
{
myBallTween.stop();
}
else
{
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaCTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaC_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaLtTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaLt_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaRtTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaRt_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetTop))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaU_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetMiddle))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaM_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetLower))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaL_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.lowerScoringAreaTarget))
{
targetValue = gameTable.lowerScoringArea.lowerSA_text.text;
}
else
{
trace("no hit");
}
}
}
}
//gameElements.removeEventListener(Event.ENTER_FRAME, ballTargetScore);
determineScore(targetValue);
//return targetValue;
}
It's all still a bit messy as I tinker trying to find the right combination.
I need to first get a basic ball<->target or boundary collision working, but ultimately, I'd like to try to figure out how to control the collisions conditionally since many of the targets are aligned vertically like a skeeball table. Will I be able to measure the speed of the ball as it is tweening/rolling? Will I be able to apply gravity correctly so that the ball falls and if it catches one of the arcs beneath the scoring target, it will roll along the arc until it 'falls into the hole'? I think I will have issues with the bounding box of my objects on that one.
Thanks for any help,
Alan
Here's the response to Vesper with a cleaned up function (thanks) and new error.
function ballTargetScore():void
{
var targetValue:String;
var targetArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaCTarget,
gameTable.upperScoringArea.upperScoringAreaLtTarget,
gameTable.upperScoringArea.upperScoringAreaRtTarget,
gameTable.middleScoringArea.middleScoringAreaTargetTop,
gameTable.middleScoringArea.middleScoringAreaTargetMiddle,
gameTable.middleScoringArea.middleScoringAreaTargetLower,
gameTable.lowerScoringArea.lowerScoringAreaTarget);
var targetTextArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaC_text.text,
gameTable.upperScoringArea.upperScoringAreaLt_text.text,
gameTable.upperScoringArea.upperScoringAreaRt_text.text,
gameTable.middleScoringArea.middleScoringAreaU_text.text,
gameTable.middleScoringArea.middleScoringAreaM_text.text,
gameTable.middleScoringArea.middleScoringAreaL_text.text,
gameTable.lowerScoringArea.lowerSA_text.text);
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(targetArray[i]))
{
targetValue = targetTextArray[i];
trace('targetValue becomes',targetValue);
}
}
}
determineScore(targetValue);
}
and the error:
ballSpeed is 432
targetArray value is [object upperScoringAreaCTarget_mc]
targetArray value is [object upperScoringAreaLtTarget_mc]
targetArray value is [object upperScoringAreaRtTarget_mc]
targetArray value is [object middleScoringAreaTargetTop_mc]
targetArray value is [object middleScoringAreaTargetMiddle_mc]
targetArray value is [object middleScoringAreaTargetLower_mc]
targetArray value is [object lowerScoringAreaTarget_mc]
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at SkeeBlast_7_fla::MainTimeline/determineScore()
at SkeeBlast_7_fla::MainTimeline/ballTargetScore()
at SkeeBlast_7_fla::MainTimeline/rollBall()
at SkeeBlast_7_fla::MainTimeline/releaseBall()
Thanks for the assistance.
The rules, which I'm sure are necessary, are making me turn this into a really long question. :)
Anyway, I sort of figured out the error about the DisplayObject, but the program still isn't working correctly.
I fixed the error by adding my gameElements MC to my removeChild call in the determineScore function, which is below; however, the ball still isn't removed, even with the error gone, and the targetValue and the score never update (or show up in the trace from above).
function determineScore(scoreEvent:String):void
{
if ( scoreEvent == "D-O" || scoreEvent == "2XB" || scoreEvent == "?")
{
if (scoreEvent == "D-O")
{
ballCount += 1;
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
else if (scoreEvent == "2XB")
{
ballCount += 2;
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
else
{
determineScore(allRandomScore(allScoresArray));
}
}
else
{
scoreTotal += Number(scoreEvent);
ballScore = Number(scoreEvent);
gameTable.tableDisplay.Score_text.text = scoreTotal;
gameTable.tableDisplay.Score_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
}
Thanks for looking at this. I feel like I'm finally making a little bit of progress.
Ok, continuing along with yet another update.
gameElements is the primary empty MC that I add all the other MC, like the game table and the ball, to so that when I go back to the main menu, I can remove everything at once.
var gameElements:MovieClip = new MovieClip();
var gameTable:MovieClip = new table_mc();
var ball:MovieClip = new blastBall();
and from my drawGame function:
...
stage.addChildAt(gameElements, 1);
gameElements.addChild(gameTable);
initBall();
...
and initBall:
function initBall():void
{
//resize ball
ball.height = 18;
ball.width = 18;
//place ball on table in correct location
ball.x = gameTable.x;
ball.y = gameTable.height - 20;
gameElements.addChild(ball);
//reduce number of remaining balls;
ballCount -= 1;
//hide the mouse and connect its movement to ball
Mouse.hide();
gameElements.addEventListener(MouseEvent.MOUSE_MOVE, moveBall);
}
Hope there's no limit to this entry. :)
Here's the little addition I put in determineScore to pick up the "results" from ballTargetScore (or lack thereof, really):
if (scoreEvent == null)
{
trace("scoreEvent is null");
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0)
{
initBall();
}
else
{
drawEnd();
}
}
(haven't cleaned up anything else yet) Still trying to get that first collision to work. When I started catching the null value, initBall and drawEnd sort of started to work (ok, it still doesn't really do what I want but at least there was a response).
Well, you have an array of your targets, and the error you receive means that some of its elements are null. But, when you iterate through the array, you for some reason check against the entire set of targets, not checking if any of them are null. I don't understand why. Seeing that you need a text variable to be assigned if a hit is detected, I advise you making another array with corresponding indexes that will contain all the texts you want to assign.
var targetTextArray:Array=(gameTable.upperScoringArea.upperScoringAreaC_text.text,
gameTable.upperScoringArea.upperScoringAreaLt_text.text,
gameTable.upperScoringArea.upperScoringAreaRt_text.text,
gameTable.middleScoringArea.middleScoringAreaU_text.text,
gameTable.middleScoringArea.middleScoringAreaM_text.text,
gameTable.middleScoringArea.middleScoringAreaL_text.text,
gameTable.lowerScoringArea.lowerSA_text.text);
Then, when you iterate through targetArray, you no longer need to check what was that target in order to retrieve the correct text. You do:
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(targetArray[i])) {
targetValue=targetTextArray[i];
trace('targetValue becomes',targetValue);
}
}
}
Update: Okay, let's clean up your determineScore() function. It looks like you are correctly adding and removing your ball, but it's possible you do something of this twice. Removing the ball twice without adding it back will net you this error. Currently I can't get if gameElements.removeChild(ball); gets called twice within that function.
function determineScore(scoreEvent:String):void
{
var reBall:Boolean=false; // should we make another ball roll, given the score
var reDisplay:Boolean=false; // should we set new text
if ((scoreEvent==null)||(scoreEvent=="")) return; // we haven't scored
if ( scoreEvent == "D-O" || scoreEvent == "2XB" || scoreEvent == "?")
{
if (scoreEvent == "D-O")
{
ballCount += 1;
reBall=true;
reDisplay=true;
}
else if (scoreEvent == "2XB")
{
ballCount += 2;
reBall=true;
reDisplay=true;
}
else
{
determineScore(allRandomScore(allScoresArray));
// hey, what are we doing here? We seemingly receive a "?" as an input,
// and we roll a random value out of a set given elsewhere. Okay
return; // as good measure, because we don't have to do anything more
}
}
else
{ // if we're here, we assume we have a numeric score passed inside, right?
scoreTotal += Number(scoreEvent);
ballScore = Number(scoreEvent);
reBall=true;
reDisplay=true;
}
// Now look, we always have to launch a new ball or display end, and
// we always have to redraw score textfield. But look, you are always calling
// determineScore with some value, while you should do it only if you hit something!
// So I added starting clause of "if null or empty string".
if (reDisplay) {
// we have to update text
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
}
if (reBall) {
// we have to remove ball and launch another, if we have any left
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
}

Removing array-objects from DisplayList

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 :)
Try casting each "Friend" as a Display Object:
var totalDel:int = deleted.length;
for(var i:int = 0; i < totalDel ;i++) {
var toDelete:DisplayObject = deleted[i] as DisplayObject;
removeChild(toDelete);
trace(totalDel + "Dele from deleted");
}
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.