I'm using for loop to display 3lifes on stage using:
/////////////////////////////////////////////////////
for(var i=0;i<3;i++) {
var Life:life = new life();
Life.x = 100*i+650;
Life.y = 20;
addChild(Life);
}
///////////////////////////////////////////////////
and I have already create a hitTestObject on the character, once the character comes in contact with a movieclip, removeChild(Life); is triggered. The first child removed successfully but when it comes to the second one, an error appear:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at _4_fla::MainTimeline/hitBoy()[_4_fla.MainTimeline::frame1:52]
at _4_fla::MainTimeline/dropEnemy()[_4_fla.MainTimeline::frame1:41]
What should I do to solve this? and how do I trigger an event after no more life is left?
I would use something like this:
import flash.events.Event;
var lives:Array = [];
for(var i:int = 0; i < 3; i++) {
addLife();
}
addEventListener("dropDead", dropDeadHandler);
function addLife():void {
var Life:life = new life();
Life.x = 100 * i + 650;
Life.y = 20;
addChild(Life);
lives.push(Life);
}
// this function triggers when I'm supposed to lose life
function removeLife():void {
var Life:life = lives.pop() as life;
removeChild(Life);
if(lives.length == 0) {
dispatchEvent(new Event("dropDead"));
}
}
function dropDeadHandler(event:Event):void {
trace("You have no more lives.");
}
these are the code i'm using
createEnemy() triggered every 3 second and ****/mc is created and move downward, the if statement is to test if the boy touches the ****/mc to and removeChild is trigger to removeChild(Life); everytime, it seem to work for removing the first live but not the second and third one....how come?
(sorry for the legibility of the code but I'm used to code in hierarchical, since it is easier for me to link how the code actually links to one another....)
////////////////////////////////////////////////////////
gameSpeed=3000;//3sec create one ****
createEnemyId=setInterval(createEnemy,gameSpeed);
function createEnemy():void{
var otherShit:**** =new ****();//assign random **** on stage
otherShit.y=50; //rubbish create off stage
otherShit.x=Math.random()*stage.stageWidth;
otherShit.addEventListener(Event.ENTER_FRAME,dropEnemy); //link to function "dropenemy", enter frame to make enemy move downards
addChild(otherShit);
//trace(otherShit+" created enemy");
trace("ceate 1 enemy everytime");
}
function dropEnemy(e:Event):void{
var mc:****=****(e.target);//linking otherShit to mc aka accessing othershit within another function
//trace(mc);//since it is enter frame fuction mc is continuousely move down and object **** being traced
mc.y+=10;
if(mc.hitTestObject(boy)){
hitBoy(mc);
trace("ouch");
}else{
missBoy(mc);
trace("you miss");
}
}
function hitBoy(mc:****):void{
mc.removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
removeChild(Life);
//error occur here??
}
function missBoy(mc:****):void{
if(mc.y>=700){
mc.removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
updateScore(+1);
}
}
I have few question for your solution, as I'm still quite a novice...
1. Why do you use import flash.events.Event;??
2. Your Solution actually works, it removes my second and third lifes without and error, so the tricks is to make individual addchild into an array?
3. I try running :
removeLife();
removeLife();
removeLife();
for third times but You have no more lives. doesn't seem to triggered in the output window?
4. when I'm running removeLife() for fourth time, an error occurs?
TypeError: Error #2007: Parameter child must be non-null.
1. Why do you use import flash.events.Event;??
You wanted to dispatch an event.. So I dispatched new Event(); if you use come class, you should import it, otherwise you get a compile error.
2. Your Solution actually works, it removes my second and third lifes without and error, so the tricks is to make individual addchild into an array?
The idea is to store your Life references in an Array, so you can target as many Lives as you want. You used only one variable - Life, which can point only to one object.
3. I try running :
removeLife();
removeLife();
removeLife();for third times but
You have no more lives. doesn't seem to triggered in the output window?
Did you addEventListener("dropDead"...) ? Use trace method to print out lives.length:
4. when I'm running removeLife() for fourth time, an error occurs?
TypeError: Error #2007: Parameter child must be non-null.
Sorry, didn't make it bulletproof. You should stop the game when you have 0 lives anyway, so this shouldn't happen. But try this instead:
// this function triggers when I'm supposed to lose life
function removeLife():void {
if(lives.length == 0) {
trace("What are you doing here? You should be dead already!");
return;
}
var Life:life = lives.pop() as life;
removeChild(Life);
trace("You have still " + lives.length + " lives.");
if(lives.length == 0) {
trace("Baam! You're dead. Dispatching event.. 'hope somebody is listening.");
dispatchEvent(new Event("dropDead"));
}
}
North America
Europe, Middle East and Africa
Asia Pacific