I have the following error: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at lamagame_fla::MainTimeline/bulletEnterFrame()[lamagame_fla.MainTimeline::frame2:116]
In the following code: Main Timeline
var j:int;
addEventListener(Event.ENTER_FRAME, enemyhit);
function enemyhit(event:Event):void {
for (i = enemy_array.length -1; i >= 0; i--){
for(var j = 0; j < bulletarray.length; j++) {
if(enemy_array[i].hitTestObject(bulletarray[j])) {
removeChild(enemy_array[i]);
removeChild(bulletarray[j]);
enemy_array.splice(j, 1);
bulletarray.splice(i, 1);
j--;
break;
}
}
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)
var pressedState:int = 0;
var myTimer:Timer = new Timer(10);
myTimer.addEventListener(TimerEvent.TIMER, onTimerTick);
function onDown(e:MouseEvent)
{
myTimer.start();
switch(e.currentTarget)
{
case stage:
pressedState = 1;
break;
}
}
function onUp(e:MouseEvent)
{
myTimer.stop();
pressedState = 0
}
function onTimerTick(e:TimerEvent)
{
switch(pressedState)
{
case 1:
// Create a new bullet
var b:Bullet = new Bullet();
// Set his position to the tank position
if(llama.scaleX==1) {
b.x = llama.x+20;
b.y = llama.y-50;
}else{
if(llama.scaleX==-1) {
b.x = llama.x-25;
b.y = llama.y-50;
}else{
b.x = llama.x+20;
b.y = llama.y-50;
}
}
// Save the randian angle between the mouse and the tank
// This angle will set the direction of the bullet
b.angleRadian = Math.atan2(mouseY - llama.y,mouseX - llama.x);
// Add an enter frame event on each bullet
b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
// Add this display object on the display list
addChild(b);
bulletarray.push(b);
break;
}
return b;
}
import flash.display.MovieClip;
import flash.geom.Point;
import flash.events.MouseEvent;
// Code by Benoit Freslon.
// Tutorials, Flash games:
// http://www.benoitfreslon.com
// This object will always look at the mouse cursor
llama.head.addEventListener(Event.ENTER_FRAME, llamaenterframe);
// This function will be launched every frame (25 times by seconds);
function llamaenterframe(pEvt) {
// pEvt.currentTarget: myTank
var b = pEvt.currentTarget;
// Get the radian angle between the tank and the cursor
// You can also replace mouseX and mouseY by another coordinates
// Convert the radian angle in dedree angle
var angleDegree = b.angleRadian * 90 / Math.PI;
// Set the orientation
b.rotation = angleDegree;
// Display angle of rotation in degree
vcam.txtAngle.text = Math.round(angleDegree) + "°";
}
// Velocity of each bullet
var speed = 8;
function bulletEnterFrame(pEvent) {
// Get the current object (Bullet)
var b = pEvent.currentTarget;
// Move this bullet on each frames
// On X axis use the cosinus angle
b.x += Math.cos(b.angleRadian) * speed;
// On Y axis use the sinus angle
b.y += Math.sin(b.angleRadian) * speed;
// Orient the bullet to the direction
b.rotation = b.angleRadian * 180 / Math.PI;
// You have to remove each created bullet
// So after every moves you must check bullet position
// If the bullet is out of the screen
if (!b.hitTestObject(vcam)) {
// Remove it from the display list
removeChild(b);
bulletarray.splice(bulletarray.indexOf(j), 1);
// /!\ AND REOMOVE HIS EVENT LISTER
b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
}
}
// Set his position to the tank position
var maxHP:int = 400;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
updateHealthBar();
function updateHealthBar():void
{
percentHP = currentHP / maxHP;
llama.healthBar.barColor.scaleX = percentHP;
}
// properties in class ----------
var enemy_array:Array = new Array;
// dynamically place MovieClip instances on the stage ----------
// inside Constructor function of class
var enemy_number:int=100;//number of circles on the stage
for(var i=0; i<enemy_number; i++) {
enemy_array[i] = new enemy(); //linkage in the library
enemy_array[i].x = MovieClip(root).background1tiles.x + Math.floor( Math.random()* MovieClip(root).background1tiles.width) ;
enemy_array[i].y = MovieClip(root).background1tiles.y + Math.floor( Math.random()* MovieClip(root).background1tiles.height) ;
enemy_array[i].addEventListener(Event.ENTER_FRAME, function (event:Event):void {
if(event.currentTarget.hitTestObject(llama)) {
currentHP -= 1;
if(currentHP <= 0) //if the player died
{
currentHP = 0; //set his HP to zero, (just in case)
vcam.gotoAndStop(2); //add any extra death-code here
}
updateHealthBar(); //update the healthBar
}
});
addChild(enemy_array[i]);
enemy_array[i].addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_4);
function fl_EnterFrameHandler_4(event:Event):void
{
if(event.currentTarget.hitTestObject(vcam)) {
if(event.currentTarget.x < Math.floor(llama.x)) {
event.currentTarget.x+=event.currentTarget.randomspeed;
}else{
event.currentTarget.x-=event.currentTarget.randomspeed;
}
if(event.currentTarget.y < Math.floor(llama.y)) {
event.currentTarget.y+=event.currentTarget.randomspeed;
}else{
event.currentTarget.y-=event.currentTarget.randomspeed;
}
}
}
}
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_6 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, basics);
function basics(event:Event):void {
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
if(llama.x > 3005.6) {
llama.x = 3005.6;
}
if(llama.x < -2369) {
llama.x = -2369;
}
if(llama.y > 0) {
llama.y=0;
}
vcam.x=llama.x;
vcam.y=llama.y;
vcam.coordinates.text="Llama's X is "+llama.x+". And Llama's Y is "+llama.y;
// End your custom code
}
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, dothelistener);
function dothelistener(event:Event) {
if(vcam.currentFrame==2) {
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_11);
llama.removeEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
}
}
llama.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
llama.y-=5;
}
if (downPressed)
{
llama.y+=5;
}
if (leftPressed)
{
llama.scaleX=-1;
llama.x-=5;
}
if (rightPressed)
{
llama.scaleX=1;
llama.x+=5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
// Velocity of each llama
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
vcam.magnify.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_10);
function fl_MouseClickHandler_10(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
vcam.width-=25;
vcam.height-=20;
// End your custom code
}
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
vcam.magnifyun.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_11);
function fl_MouseClickHandler_11(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
vcam.width+=25;
vcam.height+=20;
// End your custom code
}
var bulletarray:Array = new Array;
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_10 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_10);
function fl_EnterFrameHandler_10(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
if(vcam.currentFrame == 2) {
llama.removeEventListener(Event.ENTER_FRAME, llamaenterframe);
}
// End your custom code
}
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_11 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_11);
function fl_EnterFrameHandler_11(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
vcam.count.text = bulletarray.length;
// End your custom code
}
I tried if(parent) but then only some go through the hittest (only some bullets remove and only sometimes the enemy hp goes down) The enemy's hp is configured through an integer(currentHP2) Any help is appreciated, and thanks.
[EDIT]
After I do if(bulletarray[j].parent) then only some of the bullets work
j = array.lengthis defined once when the loop is created, it's not redefined after every tick of the loop. so, islengthis 5 when the loop is created, and after 2 ticks you remove two elements (making the array's lenght now 3) , asibecomes 3 or 4, your reference to those indexs in the array are null. - gthmb