开发者

removing a child in actionscript 3

I'm trying to make my game restart but I have a few issues. I'm making a asteroids ga开发者_运维问答me (basic). I have the following classes :

GameClass, Ship, Enemy.

Enemy and Ship is created within GameClass. GameClass is created within the DocumentClass(this being the main class).

When the game starts, I start the game e.g.

var something:GameClass = new GameClass(); 
addChild(something); 

The game plays as it should. When I try to remove the objects, nothing happens .. I do it like this:

something = null or tried removeChild(something);

neither work. Why? What am I doing wrong?


It's a little tricky to tell exactly what's going on from your question without posting more code. But, doing removeChild(something) seems to be on the right track. I'm guessing that when you're calling removeChild(something) "something" is out of scope. Maybe try making "something" a private variable within your DocumentClass


In action script, deletes are only deleting refences. So i suggest you delete the entire stage (or movieClip) holding the content to update, and your gameClass(), and recreate it.


My guess is that you are accidentally creating multiple instances of the GameClass. Have you tried iterating through all of the children of the DocumentClass?

try this from the DocumentClass:

for( var i:int = 0; i < numChildren; i++ )
{
    trace( getChildAt( i ) ); 
    // or removeChild( getChildAt( i ) ) to simply get rid of them.
}

If you're using CS3-5, I have personally noticed some very bizarre behavior which has been caused by "automatically declare stage instances". You might want to see what happens if you turn that off.


pull the var out so you have access to it throughout the document class.

like this.

package src
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class DocumentClass extends Sprite
    {
        // You will have access to this var any place in this class.
        private var game:GameClass;

        public function DocumentClass()
        {
            addEventListener(Event.ADDED_TO_STAGE, initDocumentClass);
        }

        private function initDocumentClass(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, initDocumentClass);

            game = new GameClass();
            addChild(game);


            // Now whenever you want to reset your game you can do a few things
            // the easiest would be
            removeChild(game);
            game = new GameClass();
            addChild(game);
        }
    }
}

This will also give you easy access via your menu system etc.

Any questions or if I completely missed your point. Just ask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜