Action Script 3 - ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller


Action Script 3 - ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller



I'm following this tutorial for Action Script 3 on http://markbennison.com/actionscript/as3-space-shooter/2-coding-projectiles/



I'm on part 2 Coding projectiles I don't know why its saying Error all the time when I press play



"ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller."



Here's the exact code im trying to work through to shoot bullets when spacebar is pressed, there are more but idk how to fix the Argument Error.



function addBullet(startX, startY): void {


//declare an object instance from the bullet Class
var b: Bullet = new Bullet();

//set the new bullet's x-y coordinates
b.x = startX;
b.y = startY;

//add the new bullet to the stage
stage.addChild(b);

//store the object in an array
bullets_arr.push(b);



}



function moveBullet(): void {


//loop through all instances of the bullet

//loop from '0' to 'number_of_bullets'
for (var i: int = 0; i < bullets_arr.length; i++) {
//move the bullet
bullets_arr[i].x += bulletSpeed;

//if the bullet flies off the screen, remove it
if (bullets_arr[i].x > stage.stageWidth) {
//remove the bullet from the stage
stage.removeChild(bullets_arr[i]);

//remove the bullet from the array
bullets_arr.removeAt(i);
}
}



}



Can someone give me tips to change anything or?




1 Answer
1



The error means that when this line runs:


stage.removeChild(bullets_arr[i]);



The item referenced by bullets_arr[i] is not actually on the stage. Likely because it has already been removed from the stage.


bullets_arr[i]



While this may not be the exact cause of your error, one big issue here is removing items from an array that you are currently iterating through.



When you do: stage.removeChild(bullets_arr[i]);, you are changing the size of the bullets_arr array.


stage.removeChild(bullets_arr[i]);


bullets_arr



On the first iteration, i is 0. Your first bullet is bullets_arr[0], your second bullet is bullets_arr[1] your third is bullets_arr[2] etc. If on the first loop, you end up removing the item from the array, it means the indices have shifted, so now your second bullet is bullets_arr[0], but on the next loop i is incremented to 1, so now you've actually skipped over the second bullet and are checking the third item, which after removing the first bullet now sits at index 1.


i


0


bullets_arr[0]


bullets_arr[1]


bullets_arr[2]


bullets_arr[0]


i


1


1



In your moveBullet function, change the loop so it iterates backwards, this way if you remove an item, it doesn't shift the indices that you have yet to iterate over.


moveBullet


for (var i: int = bullets_arr.length - 1; i >= 0; i--) {






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV