Sunday, May 13, 2012

[Action Script] Error 2025 when calling removeChild() May,2012

Hi Guys,
I am working on a tower defense game. I found some code online and modified it, but even before that it generated the error: Quote: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. when calling removeChild(roadHolder);. My code is below:
Code: stop();

noMoney.visible=false;

//setting vars to step in for turns and special blocks
var S:String = 'START';
var F:String = 'FINISH';
var U:String = 'UP';
var R:String = 'RIGHT';
var D:String = 'DOWN';
var L:String = 'LEFT';

var startDir:String;//the direction the enemies go when they enter
var finDir:String;//the direction the enemies go when they exit
var startCoord:int;//the coordinates of the beginning of the road
var lvlArray:Array = new Array();//this array will hold the formatting of the roads

lvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,
                        0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
                        0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,
                        S,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,
                        0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
                        0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,
                        0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        ];

//the names of these variables explain what they do
var currentLvl:int = 1;
var gameOver:Boolean = false;

var currentEnemy:int = 0;//the current enemy that we're creating from the array
var enemyTime:int = 0;//how many frames have elapsed since the last enemy was created
var enemyLimit:int = 12;//how many frames are allowed before another enemy is created
var enemyArray:Array = new Array();//this array will tell the function when to create an enemy
var enemiesLeft:int;//how many enemies are left on the field
//for enemyArray, use numbers: 1, 2, 3, 4, 5, 50, 100, 250, 500
enemyArray = [//defining the array
                        [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created
                        [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],//another row means another level
                        [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
                        [100],
                        [5,4,5,4,5,4,5,3,3,3,50,50,5,4,3,1,1,1,4,5,5,50,100,100,5,4,4,4,100],
                        [250,250],
                        [500],
                        [500, 500]
                          ];

var money:int=100;//how much money the player has to spend on turrets
var lives:int=5;//how many lives the player has

var rangeCircle:Shape = new Shape();
rangeCircle.graphics.beginFill(0x006600,.5);
rangeCircle.graphics.drawCircle(12.5,12.5,100);
rangeCircle.graphics.endFill();

function startGame():void{//we'll run this function every time a new levelbegins
        if(gameOver==false){
                for(var i:int=0;i<enemyArray[currentLvl-1].length;i++){
                        if(enemyArray[currentLvl-1][i] != 0){
                                enemiesLeft ++;
                        }
                }
        }
}

var roadHolder:Sprite = new Sprite();//create an object that will hold all parts of the road
addChild(roadHolder);//add it to the stage
var obj = roadHolder;
function makeRoad():void{
        var row:int = 0;//the current row we're working on
        var block;//this will act as the block that we're placing down
        for(var i:int=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
                if(lvlArray[i] == 0){//if the current index is set to 0
                        block = new EmptyBlock();//create a gray empty block
                        block.graphics.beginFill(0x6E0000);
                        block.graphics.drawRect(0,0,25,25);
                        block.graphics.endFill();
                        addChild(block);
                        //and set the coordinates to be relative to the place in the array
                        block.x= (i-row*22)*25;
                        block.y = row*25;
                } else if(lvlArray[i] == 1){//if there is supposed to be a row
                        //just add a box that will be a darker color and won't have any actions
                        block = new Shape();
                        block.graphics.beginFill(0x300000);
                        block.graphics.drawRect(0,0,25,25);
                        block.graphics.endFill();               
                        block.x= (i-row*22)*25;
                        block.y = row*25;       
                        roadHolder.addChild(block);//add it to the roadHolder
                } else if(lvlArray[i] is String){//if it's a string, meaning a special block
                        //then create a special block
                        block = new DirectBlock(lvlArray[i],(i-row*22)*25,row*25);
                        addChild(block);
                }
                for(var c:int = 1;c<=16;c++){
                        if(i == c*22-1){
                                //if 22 columns have gone by, then we move onto the next row
                                row++;
                        }
                }
        }
}

function makeTurret(xValue:int,yValue:int):void{//this will need to be told the x and y values
        var turret:Turret = new Turret();//creating a variable to hold the Turret
        //changing the coordinates
        turret.x = xValue+12.5;
        turret.y = yValue+12.5;
        addChild(turret);//add it to the stage
}

addEventListener(Event.ENTER_FRAME, eFrame);//adding an eFrame function
function eFrame(e:Event):void{
        //if there aren't any levels left
        if(currentLvl > enemyArray.length){
                gameOver=true;//set the game to be over
               
                var j = setTimeout(callback2, 1000);
                removeChild(roadHolder);//remove the pieces of road
                function callback2():void {
                        //reset all the stats
                currentLvl = 1;
                currentEnemy = 0;
                enemyTime = 0;
                enemyLimit = 12;
                enemiesLeft = 0;
               
                removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener
                gotoAndStop('win');//go to the win frame
                };
        }
        if(lives<=0){//if the user runs out of lives
                gameOver=true;//set the game to be over
               
                var k = setTimeout(callback1, 1000);
                removeChild(roadHolder);//remove the pieces of road
                function callback1():void {
                        //reset all the stats
                currentLvl = 1;
                currentEnemy = 0;
                enemyTime = 0;
                enemyLimit = 12;
                enemiesLeft = 0;
               
                removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener
                gotoAndStop('lose');//go to the lose frame
                };
               
        }
        makeEnemies();//we'll just make some enemies
        if(enemiesLeft==0 && currentLvl!=8){//if there are no more enemies left
                currentLvl ++;//continue to the next level
                currentEnemy = 0;//reset the amount of enemies there are
                startGame();//restart the game
        }
       
        //Updating the text fields
        txtLevel.text = 'Level '+currentLvl;
        txtMoney.text = '$'+money;
        txtLives.text = 'Lives: '+lives;
        txtEnemiesLeft.text = 'Enemies Left:  '+enemiesLeft;
}

function makeEnemies():void{//this function will add enemies to the field
        if(enemyTime < enemyLimit){//if it isn't time to make them yet
                enemyTime ++;//then keep on waiting
        } else {//otherwise
                var theCode:int = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
                if(theCode != 0){//if it isn't an empty space
                        var newEnemy:Enemy = new Enemy(theCode);//then create a new enemy and pass in the code
                        enemyHolder.addChild(newEnemy);//and add it to the enemyholder
                }
                currentEnemy ++;//move on to the next enemy
                enemyTime = 0;//and reset the time
        }
}

//run these functions at the start
makeRoad();
var enemyHolder:Sprite = new Sprite();
addChild(enemyHolder);
startGame();Thanks in advance.
Error 2025 when calling removeChild()

Related Post



0 comments: