简体   繁体   中英

AS3 Flex Embed Image to Sprite

What is the difference between:

[Embed(source = "../assets/graphic.png")]
const GRAPHIC:Class;
var graphic:Bitmap = new GRAPHIC();
addChild(graphic);

And:

[Embed(source = "../assets/graphic.png")]
const GRAPHIC:Class;
addChild(new GRAPHIC());

And which one of these should I use and why?

The first is a variable pointer to the instantiated copy of the GRAPHIC class. The second is an implicit declaration.

You would use the pointer if you needed to perform further operations on the object. For example...

graphic.name = "myGraphic";
graphic.alpha = 0.5;
someFunction(graphic);

Setting properties, and passing it as an argument to other functions are good cases for a pointer. If you don't need to do this, you can use the implicit declaration. You can do this in other places when it makes sense. For example...

var settings:Object = {
    "x":20,
    "alpha":0.5
}
setProperties(foo, settings);

// Instead, you can do it in one line, with an implicit declaration.
setProperties(foo, {"x":20, "alpha":0.5});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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