【问题标题】:AS3 add an image to an "extended" movie clipAS3 将图像添加到“扩展”影片剪辑
【发布时间】:2011-12-13 21:41:14
【问题描述】:

在 AS3 中 - 我使用通用按钮处理程序来处理影片剪辑对象上的单击事件。在过去的 4 小时里,我一直在尝试将图像添加到此影片剪辑(请参阅 * * *)对象。

代码(我剪切并粘贴了一点,但这一切都编译没有任何错误)

btPlay  =   new mcButtonPlay(this,"ClickMe",GameImage); //  GameImage is an BitmapData object

public class mcButtonPlay extends navigationButtonHandler {
    public function mcButtonPlay(Parent:MovieClip,Text:String,GameImage:BitmapData)  {
        super(Text);
        if (GameImage != null) {                
            var ImageBitMap:Bitmap  = new Bitmap(GameImage);
            this.addChild(ImageBitMap); // * * * This doesn’t show
            Parent.addChild(ImageBitMap);   // Works just to test the image
        }                       
    }
} 

public class navigationButtonHandler extends MovieClip {
    public function navigationButtonHandler(Text:String)  {
        ChangeButtonTargetText(Text);
        Parent.addChild(this);
    }
}

【问题讨论】:

  • 不确定您遇到的问题,但您可能应该在opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions 中查看关于变量和类名大写的 AS3 编码约定。
  • 我不认为这会起作用,因为你不能将参数传递给 mcButtonPlay 而它扩展了一个扩展movieclip的类......不会给你一个错误??
  • 类名总是大写,变量名不大写。这些都是强有力的约定。作为风格问题,仅对事件处理程序(即函数)使用后缀“处理程序”。如果要表示一个影片剪辑,请添加后缀 _mc。
  • Adam / Rendall:我很欣赏你的 cmets 风格,并将在未来考虑这一点 Eric:不,这不会引发错误。它工作正常,但我相信问题与“扩展”和“超级”代码有关,因为很明显 - 只需将孩子添加到电影剪辑中就可以正常工作!

标签: actionscript-3


【解决方案1】:
public class navigationButtonHandler extends MovieClip {
    public function navigationButtonHandler(Text:String)  {
        ChangeButtonTargetText(Text);
        Parent.addChild(this); //<--------????
    }
}

Parent 在从问题复制的上述代码中来自哪里?似乎navigationButtonHandler 类永远不会被添加到舞台,因为它没有得到Parent?因此,扩展类也永远不会添加到舞台上,这就是为什么如果您将其添加到您的 mcButtonPlay 类中,您的图像永远不会显示的原因。
您的扩展构造函数确实传递了 Parent 参数,但基类没有。这对我来说似乎很奇怪,不应该编译。还是你在幕后做一些静态的事情?

然后开始研究 cmets 中提到的大写风格。如果您遵循通用约定,那么阅读和查找错误确实容易得多!

【讨论】:

    【解决方案2】:
            this.addChild(ImageBitMap); // * * * This doesn’t show
            Parent.addChild(ImageBitMap);   // Works just to test the image
    

    使用此代码,ImageBitMap 将从“this”中删除并放置在“Parent”中,因此您将永远不会在“this”中看到它。

    删除第二行并告诉我它是否仍然有效。

    编辑:

    您是将 btPlay 添加到舞台还是显示层次结构中?

    例如。

    btPlay  =   new mcButtonPlay(this,"ClickMe",GameImage); 
    this.addChild(btPlay);
    

    【讨论】:

    • 我在有和没有第二行的情况下都试过了,没有区别。
    【解决方案3】:

    设法通过交换超级并添加子逻辑来修复它:

    public class mcButtonPlay extends navigationButtonHandler {
        public function mcButtonPlay(Parent:MovieClip,Text:String,GameImage:BitmapData)  {
            if (GameImage != null) {                
                var ImageBitMap:Bitmap  = new Bitmap(GameImage);
                this.addChild(ImageBitMap); // * * * This doesn’t show
                Parent.addChild(ImageBitMap);   // Works just to test the image
            }                       
            super(Text);
        }
    }
    

    不知道为什么!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多