hey so im attemping to make a castle Defense type of game in which you fire arrows at on-coming enemies and kill them. The issue i am running into is removing the Enemy and the Arrow that have collided. So far i have a timer that spawns the enemies and puts it into an array, i also have a stage event listener that adds an arrow and moves it towards the Mouse coordinates, and then adds the arrow to an array. The issue i am having is trying to make a timer that constantly checks if any given arrow from the array has made contact with any given enemy in the enemy array, and then removeChild()ing both of them. Below is my sub-par code for the spawning of the enemies and putting them in an array
var enemies:Array=[]; //Array to store the spawned enemies
var enemyTimerArray:Array=[]; //Array for enemy Timers
var spawnTimer:Timer=new Timer(4000-(spawnTimerIncrement),100); //Timer to Spawn the units
spawnTimer.addEventListener(TimerEvent.TIMER,spawnUnit);
function spawnUnit(event:TimerEvent):void
{
var basicI:basic=new basic(); //Creating a new basic Infantry every 4 seconds - increment
enemies.push(basicI); //Adds new spawn to enemies array
basicI.y=445-randomSpawnCoord(130); //Random start position of enemy
basicI.x=625; //Start position of enemy
addChild(basicI); //Adding enemy
var enemyTimer:Timer=new Timer(20833,1);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyAttack);
enemyTimerArray.push(enemyTimer);
enemyTimer.start();
}
This is the function that addChild()s the arrow and puts it in an array:
var arrowArray:Array=new Array;
var arrowTimerArray:Array=[];
var j:int=0;
stage.addEventListener(MouseEvent.CLICK,fireArrow) //Adding an on Mouse click event listener
function fireArrow(event:MouseEvent):void //fires arrow every time mouse is clicked
{
if(reloadDone==true) //Checking if reload is done
{
removeChild(arrowPlace);
var arrow1:Arrow=new Arrow(); //Defining arrow object
arrow1.x=190.9; //Should spawn arrow at Archer’s Coords
arrow1.y=332.65;
arrow1.SpeedX=mouseX-190.9;
arrow1.SpeedY=(-mouseY)+332.65;
arrow1.slope=arrow1.SpeedY/arrow1.SpeedX;
arrow1.intercept=(-332.65-(arrow1.slope190.9));
var arrowXTween:Tween=new Tween(arrow1,‘x’,null,190.9,2000,4,true);
var arrowYTween:Tween=new Tween(arrow1, ‘y’,null,332.65,((-arrow1.slope2000)-arrow1.intercept),2,true);
arrowArray.push(arrow1);
addChild(arrow1);
startReloading();
var arrowTimer:Timer=new Timer(6000,1);
arrowTimer.addEventListener(TimerEvent.TIMER, removeArrow);
arrowTimerArray.push(arrowTimer);
arrowTimer.start();
}
}
Below is the code for attempting to remove both Children when they collide:
var hitTestChecker:Timer=new Timer(10);
hitTestChecker.addEventListener(TimerEvent.TIMER, checkHit);
function checkHit(event:TimerEvent):void
{
for(var q:int=0;q<arrowArray.length;q++)
{
for(var r:int=0;r<enemies.length;r++)
{
if(arrowArray[q].hitTestObject(enemies[r]))
{
removeChild(arrowArray[q]);
removeChild(enemies[r]);
enemyTimerArray[r].stop();
arrowTimerArray[q].stop();
}
}
}
}
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
This is the Output that occurs every time the objects collide.
I apologize in advance if my wall of text is formatted incorrectly and if my code is far below the level of these forums :/. Any input on what i could do to not have this error occur would be greatly appreciated.
Danza.