AS3:addChild不会出现在舞台上

时间:2014-07-03 12:54:12

标签: actionscript-3 oop

我试图制作一个菜单,但我的影片剪辑并没有出现在舞台上。我用跟踪测试了它,并且当addChild(currentClip)执行时它确实开始运行。

它反对的面向编程,所以我加入了全班。 对不起评论,否则我迷路了:))

package 
{

    import flash.display.MovieClip;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.events.Event;

    public class IntroClip extends MovieClip
    {
        var currentClip:MovieClip;

        public static const MY_FINISHED_INTRO_CLIP_EVENT:String = "my_finished_intro_clip_event";

        // constructor code
        public function IntroClip()
        {
            startButton.addEventListener(MouseEvent.CLICK, openMainGame);
            howToButton.addEventListener(MouseEvent.CLICK, openHowTo);
            hiscoreButton.addEventListener(MouseEvent.CLICK, openHiscore);
        }

        //Open het spel
        public function openMainGame (event:MouseEvent):void
        {
            //Bij klikken IntroClip verwijderen en MainGame toevoegen
            dispatchEvent(new Event(MY_FINISHED_INTRO_CLIP_EVENT));
            currentClip = new MainClip();
            trace('addChild')
            addChild(currentClip);
            currentClip.x=160;
            currentClip.y=160;
        }

        //Open de opties
        public function openHowTo (event:MouseEvent):void
        {
            //Bij klikken IntroClip verwijderen en Options toevoegen
            dispatchEvent(new Event(MY_FINISHED_INTRO_CLIP_EVENT));
        }   

        //Open de hiscores
        public function openHiscore (event:MouseEvent):void
        {
            //Bij klikken IntroClip verwijderen en Hiscores toevoegen
            dispatchEvent(new Event(MY_FINISHED_INTRO_CLIP_EVENT));
        }  
    }
}

2 个答案:

答案 0 :(得分:1)

在您的主要类Kikkers,方法startMainGame中,您正在删除currentClip而不是将其重新添加到舞台。问题是你在IntroClip的openMainGame方法中添加MainClip,而不是在主类Kikkers中添加它。您应该在Kikkers中修改startMainGame,如下所示:

    public function startMainGame( event: Event): void
    {
        trace("Start Main Game");
        currentClip.removeEventListener(IntroClip.MY_FINISHED_INTRO_CLIP_EVENT, startMainGame);
        removeChild(currentClip);

        currentClip = new MainClip();
        currentClip.x = 160;
        currentClip.y = 160;
        addChild(currentClip);
    }

在IntroClip中的openMainGame方法中,您应该只保留调度该事件的第一行。

答案 1 :(得分:0)

您无法看到任何内容,因为MainClip没有任何图片。试试

public function MainClip() 
{
        this.graphics.beginFill(0xF244A7);
        this.graphics.drawCircle(20,20, 100);
        this.graphics.endFill();
}

当你把它添加到舞台上时,你应该能看到一些东西:)