开发者

actionscript 3 flash removing a child - bring it back?

i just started learning flash. Anyways, I have a movieclip that is removed when开发者_C百科 i quit the game from the menu e.g. removeChild(character). When i press "start" on the menu, i would like the character to re-appear. How can i add him again ONLY if he's been deleted?


You can use addChild again to add him. To test if he is added/deleted, you can test if his stage property is true.

if (!character.stage) 
{
    addChild(character);
}


Maybe you just want to change the visibility of the child or temporarily exclude it from the layout?


Check the parent property of the character to see if it is attached to anything. Note however that it is perfectly fine to use addChild on a display object that is already added.


when removing save it in a var

var mySavedCharacter:MovieClip = characater
removeChild(character)

When adding it back just add the saved var

addChild(mySavedCharacter)


Also remember objects are passed by reference so a change to character will also change mySavedCharacter.


You first need to make sure that you have a reference to the MovieClip you want to re-add to the stage. If you are creating this MovieClip in a function and not saving off a reference, you will have to do a "dirty" look-up to get a reference. Here is an example of how to create a reference for later use:

var myMovieClip : MovieClip; // make sure you have this outside of a function

function createMyMovieClip() : void
{
    myMovieClip = new MovieClip();
    addMyMovieClip();
}

Now that you have that reference to the MovieClip you can do something like this:

function addMyMovieClip() : void
{
    stage.addChild( myMovieClip );
}

function removeMyMovieClip : void
{
    stage.removeChild( myMovieClip );
}


if (!yourContainer.contains(character)) yourContainer.addChild(character);

This code assumes "yourContainer" is a valid DisplayObjectContainer reference, and that "character" is a non-null reference to a DisplayObject.

Also, make sure that you're saving a reference to your character MovieClip/Sprite other than just adding to the display list, so that it is not GC'd.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜