user1792711
user1792711

Reputation:

Error #2025: The supplied DisplayObject must be a child of the caller - Game Looping when after gotoAndStop

So ok Im somewhat new to flash im more of a java man myself but anyway I keep getting this error and all the solutions ive been find either don't work or they break the games collision

Here's the Code

stop();

import flash.utils.Timer;
import flash.utils.getDefinitionByName;
import flash.events.Event;
import flash.events.TimerEvent;

var playerobj:player;
var nextObject:Timer;
var objects:Array = new Array();
var score:int = 0;
const speed:Number = 9.0;
playerobj = new player();
playerobj.y = 650;
addChild(playerobj);


setNextObject();
addEventListener(Event.ENTER_FRAME, moveObjects);


function setNextObject()
{

    nextObject = new Timer(1000+Math.random()*1000,1);
    nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
    nextObject.start();

}

function newObject(e:Event)
{
    var newObject:AI;
    newObject = new AI();
    newObject.x = Math.random() * 480;
    addChild(newObject);
    objects.push(newObject);

    setNextObject();

}

function moveObjects(e:Event)
{
    for (var i:int=objects.length-1; i>=0; i--)
    {
        objects[i].y +=  speed;
        if (objects[i].y > 800)
        {

            removeChild(objects[i]);
            score = score + 10000;
            objects.splice(i,1);

        }
        if (objects[i].hitTestObject(playerobj))
        {

            cleanUp();

        }
    }

    playerobj.x = mouseX;



}

function cleanUp():void
{
    while (this.numChildren > 0)
            {

                removeChildAt(0);



            }

    nextObject.stop();
    gotoAndStop(4);
    stop();
} 

It must be somehow related to this problem but whenever gotoAndStop is called the game seems to loop around back into the frame not really sure why, Thanks for your help

Upvotes: 0

Views: 44

Answers (2)

Cristina Georgescu
Cristina Georgescu

Reputation: 446

In the cleanup function, right after declaring it, you should remove the ENTER_FRAME event listener. Also, I would stop the timer before removing childs and I would just remove the objects you added to stage dynamically. And the stop() in the cleanup function is redundant.

function cleanUp():void {

  removeEventListener(Event.ENTER_FRAME, moveObjects);
  nextObject.stop();

  for(var i:uint = 0; i < objects.length; i++){
     removeChild(objects[i]);
  }
  removeChild(playerObj)

  gotoAndStop(4);    
} 

Also, it's better to keep minimal code in the timeline and move as much as you can in classes.

Upvotes: 2

Power
Power

Reputation: 113

the error is in the cleanUp()

function moveObjects(e:Event)
{
    for (var i:int=objects.length-1; i>=0; i--)
    {
        objects[i].y +=  speed;
        if (objects[i].y > 800)
        {

            removeChild(objects[i]);
            score = score + 10000;
            objects.splice(i,1);

        }
        if (objects[i].hitTestObject(playerobj))
        {

            cleanUp();

        }
    }

    //playerobj no more exists if cleanUp() is called, move this line above cleanUp();

    //playerobj.x = mouseX;

    //or inside cleanUp() put that line

    //removeEventListener(Event.ENTER_FRAME, moveObjects);


}

Upvotes: 0

Related Questions