addChild和removeCurrent子

时间:2017-09-27 15:41:14

标签: actionscript-3 removechild addchild

我有多个按钮可以加载多个影片剪辑。我的问题是,当一个人加载时,它不会消失,防止其他人在点击他们的按钮时加载。这是我的代码如下。我是否需要添加"如果"声明?任何帮助将不胜感激。感谢。

movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);

 function gotoMovie(event:Event):void {
    var moviescene:MovieClip = new movie();
    stage.addChild(moviescene);

}

function gotoWeb(event:Event):void {
    var webscene:MovieClip = new web();
    stage.addChild(webscene);

}

function gotoMail(event:Event):void {
    var contactscene:MovieClip = new contact();
    stage.addChild(contactscene);

}

1 个答案:

答案 0 :(得分:0)

我不完全确定你想要达到的目标,但我想这应该会有所帮助。

var currentScene:MovieClip; // pointer to scenes
movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);

// function that sets new scenes
function setScene(mc:MovieClip):void {
    if(currentScene) stage.removeChild(currentScene);// if a scene is already loaded, remove it
    currentScene = mc; // set currentScene to new scene
    stage.addChild(currentScene); // add it to stage
}

function gotoMovie(event:MouseEvent):void {
    setScene(new movie());
}

function gotoWeb(event:MouseEvent):void {
    setScene(new web());
}

function gotoMail(event:MouseEvent):void {
    setScene(new contact());
}