【问题标题】:AS3: addChild doesn't appear on stageAS3:addChild 没有出现在舞台上
【发布时间】:2014-07-03 12:54:12
【问题描述】:

我正在尝试制作菜单,但我的影片剪辑没有出现在舞台上。我用跟踪对其进行了测试,当 'addChild(currentClip) 被执行时它确实开始运行。

它是面向对象的编程,所以我添加了我的整个课程。 对不起cmets,否则我会迷路:)

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));
        }  
    }
}

【问题讨论】:

  • 应该可以。你是从 Flash 导出的吗?或者它是一个单独的类,比如公共类 MainClip 扩展了 MovieClip?也许你没有添加任何图形?如果跟踪有效,那么它可能已添加到阶段。您可以通过运行 this.getChildIndex(currentClip) 来检查它。如果没有抛出异常,则添加。
  • 这是一个单独的类,我会更新我的代码,以便您可以看到我导入的内容。它正在运行,只是它没有出现在舞台上。
  • 好的,你能发布 MainClip() 代码还是太大了?
  • MainClip 代码目前为空。
  • 这就是你看不到它的原因——没有什么可显示的。如果您创建一个新的 MainClip() 并执行 this.graphics.beginFill(0xff0000);this.graphics.drawRect(0,0,100,100); this.graphics.endFill() 你会在舞台上看到。

标签: actionscript-3 oop


【解决方案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 方法中,您应该只保留调度事件的第一行。

【讨论】:

    【解决方案2】:

    您看不到任何内容,因为 MainClip 没有任何图形。试试

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2014-06-23
      • 2014-05-12
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多