I've been learning AS3 for a class project for just over a few months, most of what I know is by looking at how others problems were solved on here.
To the point.
I've got a class called Generate so I import that to my main timeline, in that class it creates a child and adds it.
Now I can't figure out how to remove it, it always says it's null: "TypeError: Error #2007: Parameter child must be non-null."
package {
import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;
public class Generate extends MovieClip{
public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();
public function Generate(parent:Object){
if (monsterID == 1){
var monsOne:MonsterOne = new MonsterOne();
monsOne.name = "monsterOne";
parent.addChild(monsOne);
monsOne.x = 100;
monsOne.y = 200;
trace("spawn1");
}
This is how it's generated. There is a little more code for monsTwo, three and four. Same code different names. That's all that is in the file.
I've tried all sorts to remove the code but always with an error and no actual removal of the child.
stage.removeChild(monsOne);
monsOne.parent.removeChild(monsOne);
removeChild(monsOne);
And who knows how many others.
Am I missing something or just doing it completely wrong.
Thank you
--EDIT--
if(o.monHP <= 0) {
turnTimer.stop();
turnTimer.removeEventListener(TimerEvent.TIMER, CounterA);
var monsOne:MovieClip = getChildByName('monsterOne') as MovieClip;
var monsTwo:MovieClip = getChildByName('monsterTwo') as MovieClip;
var monsThree:MovieClip = getChildByName('monsterThree') as MovieClip;
var monsFour:MovieClip = getChildByName('monsterFour') as MovieClip;
parent.removeChild(monsOne);
parent.removeChild(monsTwo);
parent.removeChild(monsThree);
parent.removeChild(monsFour);
gotoAndStop('win');
}
How I'm trying to remove the child.
package {
import MonsterOne;
import MonsterTwo;
import MonsterThree;
import MonsterFour;
import flash.display.*;
public class Generate extends MovieClip{
public static var monsterID = String(monsterID);
monsterID = Math.ceil(Math.random() * 4).toString();
public function Generate(parent:Object){
if (monsterID == 1){
var monsOne:MonsterOne = new MonsterOne();
monsOne.name = "monsterOne";
parent.addChild(monsOne);
monsOne.x = 100;
monsOne.y = 200;
trace("spawn1");
}
if (monsterID == 2){
var monsTwo:MonsterTwo = new MonsterTwo();
monsTwo.name = "monsterTwo";
parent.addChild(monsTwo);
monsTwo.x = 100;
monsTwo.y = 200;
trace("spawn2");
}
if (monsterID == 3){
var monsThree:MonsterThree = new MonsterThree();
monsThree.name = "monsterThree";
parent.addChild(monsThree);
monsThree.x = 100;
monsThree.y = 200;
trace("spawn3");
}
if (monsterID == 4){
var monsFour:MonsterFour = new MonsterFour();
monsFour.name = "monsterFour";
parent.addChild(monsFour);
monsFour.x = 100;
monsFour.y = 200;
trace("spawn4");
}
}
}
} Full Generate File