简体   繁体   English

动作脚本:如何将图像/图标设置为DisplayObject?

[英]Actionscript: How to set an Image/Icon to a DisplayObject?

Now there is a Constructor: MyMarker(icon:DisplayObject=null) which accepts any DisplayObject, if I create a new instance of MyMarker(just new MyMarker()),it will be dsplayed with a default Icon,so how to set a Image/Icon into this Constructor? 现在有一个构造函数: MyMarker(icon:DisplayObject = null)可以接受任何DisplayObject,如果我创建MyMarker的新实例(只是new MyMarker()),它将使用默认的Icon进行播放,因此如何设置图像/图标进入这个构造函数? Any ideas are much appreciated! 任何想法都非常感谢!

It's kind of hard to tell from your question but, if you want to set a default icon if NO DisplayObject is passed, just check if icon is null in the MyMarker constructor, eg: 从您的问题中很难分辨出来,但是,如果要设置默认图标(如果未传递DisplayObject的话),只需检查MyMarker构造函数中的icon是否为null,例如:

...
public function MyMarker(icon:DisplayObject=null) {
    if (icon==null) {
    // set default icon here...

    }

    }
...

As for the inheritance issue, so long as the object passed inherits from DisplayObject, you should be fine. 至于继承问题,只要传递的对象是从DisplayObject继承,就可以了。 You may run into trouble if you try to invoke methods on the object that don't appear in DisplayObject. 如果尝试对未出现在DisplayObject中的对象调用方法,则可能会遇到麻烦。

You should create an appropriate DisplayObject in advance and then pass the reference to this object in the constructor: 您应该事先创建一个合适的DisplayObject,然后在构造函数中将引用传递给该对象:

var myLittleCustomIcon:Sprite = new Sprite();
myLittleCustomIcon.graphics.beginFill(0x00FF00, 1);
myLittleCustomIcon.graphics.drawRect(0, 0, 100, 50);
myLittleCustomIcon.graphics.endFill();
var myCustomMaker:MyMarker = new MyMarker(myLittleCustomIcon);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM