简体   繁体   English

在AS3中加载嵌入的图像

[英]Loading an embedded image in AS3

OK, I feel dumb asking this, but I've spent the whole afternoon trying to figure it out, without success. 好吧,我觉得愚蠢的问这个,但我花了整整一个下午试图搞清楚,没有成功。

In AS3, I want to add an image to the Library (let's call it image.png) and instantiate it using just code. 在AS3中,我想将一个图像添加到库中(让我们称之为image.png)并仅使用代码对其进行实例化。

I succeeded in instantiating an external image this way 我成功地以这种方式实例化外部图像

var pLoad:Loader = new Loader();
pLoad.load(new URLRequest("image.png"));
addChild(pLoad);

But no matter what I tried, I can't do the same loading the image from the library. 但无论我尝试什么,我都无法从库中加载图像。 How is that done? 怎么做的?

Aside : I've seen the [embed] syntax but ideally I'd like to avoid hardcoded image names, that is, I want to choose an image, and thus generate the image name, programatically. 旁白:我已经看过[embed]语法,但理想情况下我想避免硬编码的图像名称,也就是说,我想选择一个图像,从而以编程方式生成图像名称。

Well, first of all you need to open the properties window of the library and check the "Export to Actionscript" checkbox, after that just put a nice reference to your object in the class textfields, for this example I'll call it MyImage, ok. 好吧,首先你需要打开库的属性窗口并选中“Export to Actionscript”复选框,之后只需在类文本域中对你的对象进行一个很好的引用,对于这个例子,我将它称为MyImage,好。 Now let's get the code. 现在让我们来获取代码。

var ImageClass:Class = getDefinitionByName("MyImage") as Class;
var image:Bitmap = new Bitmap(new ImageClass(575,430));
addChild(image);

That's it, works nice here. 就是这样,在这里工作得很好。

The Flash library is quite a mystical beeing, but if well used, it can be quite a nice way to setup your Flash workflows... I'll try to clarify all of this: Flash库非常神秘,但如果使用得当,它可以是设置Flash工作流程的一种很好的方式......我将尝试澄清所有这些:

Library symbols with the "Export for ActionScript" option are actually compiled as classes. 带有“Export for ActionScript”选项的库符号实际上编译为类。 If there is no class file with the same class name, Flash will create one on compilation with the same name you declare in the "Class" field. 如果没有具有相同类名的类文件,Flash将在编译中创建一个与您在“类”字段中声明的名称相同的名称。 That means, in your case, if the name of the class is "image.png" it will actually create a "png" class in the "image" package extending BitmapData (of course, it would be wiser to give it another classname, say proyect.library.MyImage)... this means you don't need getDefinitionByName, just instantiate it as you would with any other class: 这意味着,在你的情况下,如果类的名称是“image.png”,它实际上会在“image”包中创建一个“png”类,扩展BitmapData(当然,给它另一个类名更明智,比如proyect.library.MyImage)...这意味着你不需要getDefinitionByName,只需像实现任何其他类一样实例化它:

import image.png;
var bmd:BitmapData = new png(0,0); //the dimensions are irrelevant but necessary

Then you need a Bitmap instance to be able to add it to the displayList: 然后你需要一个Bitmap实例才能将它添加到displayList:

var bitmap:Bitmap = new Bitmap(bmd,"auto", true); //see the docs for the las two args
addChild(bitmap);
//Bitmap is a DisplayObject, so you can apply transformations to it as with any Sprite or MovieClip.

All of this applies to any Library Symbol (except Graphic)... let's say you "export for AS" a sound symbol as "project.library.MySound", then you can just do this: 所有这些都适用于任何库符号(图形除外)...让我们说“导出AS”声音符号为“project.library.MySound”,然后你可以这样做:

import proyect.library.MySound;
var sound:Sound = new MySound();
sound.start();

If you do have a class file with the same name as your library's symbol, Flash will try to use it (if it inherits the default base class). 如果您有一个与库的符号同名的类文件,Flash将尝试使用它(如果它继承了默认的基类)。 You will notice that all of these symbols have an editable Base Class field. 您会注意到所有这些符号都具有可编辑的基类字段。 You can also set a custom class in there, as long as it inherts the default base class... In bitmaps it's flash.display.BitmapData, sounds are flash.media.Sound, fonts are flash.text.Font, movieclips are flash.display.MovieClip, etc... In the case of MovieClips, as long as you don't have frames, you can also subclass from Sprite. 你也可以在那里设置一个自定义类,只要它是inherts默认基类...在位图中它是flash.display.BitmapData,声音是flash.media.Sound,字体是flash.text.Font,movieclips是flash .display.MovieClip等...在MovieClip的情况下,只要你没有帧,你也可以从Sprite继承。

All of this, while it may sound a bit mystical, if well applied, can result in quite a comfortable workflow for both designers and developers working with Flash. 所有这些,虽然听起来有点神秘,但如果应用得当,可以为使用Flash的设计人员和开发人员带来相当舒适的工作流程。 For instance, you can just setup a nice package with the whole UI definition, and make your designers use those base classes to assemble the graphics and animations. 例如,您可以使用整个UI定义设置一个漂亮的包,并让设计人员使用这些基类来组合图形和动画。

An improvement on ruyadorno's way, if you already know what the classname is, is just: 如果你已经知道类名是什么,那么对ruyadorno方式的改进就是:

var image:Bitmap = new Bitmap(new MyImage(0, 0));
addChild(image);

Bitmaps can be put into Sprites easily enough: 位图可以很容易地放入Sprite中:

var image:Bitmap = new Bitmap(new MyImage(0, 0));
var sprite:Sprite = new Sprite;
sprite.addChild(image);
addChild(sprite);

Then you can transform the sprite as normal. 然后你可以正常转换精灵。

nb The parameters to MyImage are width and height, but they are dummy for subclasses, you can pass anything. nb MyImage的参数是宽度和高度,但它们对于子类是虚拟的,你可以传递任何东西。

According to what I have found, this is nowhere as easy to do in AS3 as it was in previous versions. 根据我的发现,在AS3中,这与以前的版本一样容易。 One method that worked was to create a MovieClip to hold the image, then create an instance of that MovieClip and add it to the stage. 一种有效的方法是创建一个MovieClip来保存图像,然后创建该MovieClip的一个实例并将其添加到舞台上。 The other method was to create a class for each image you add, then instantiate and add that class to the stage (the steps for which are located here . 另一种方法是为您添加的每个图像创建一个类,然后实例化该类并将其添加到舞台(其中的步骤位于此处)

There must be better ways to do this. 必须有更好的方法来做到这一点。 I'll continue searching. 我会继续搜索。

Looks like this works : 看起来像这样:

var pDef:Class = getDefinitionByName("image.png") as Class;
var _image:BitmapData = new pDef(10, 10);

m_pSprite = new Sprite();           
m_pSprite.graphics.beginBitmapFill(_image);
m_pSprite.graphics.drawRect(0, 0, _image.width, _image.height);
m_pSprite.graphics.endFill();
addChild(m_pSprite)

Looks ugly and unnecesarily complex, though. 但是看起来很丑陋而且不必要地复杂。 Any other way? 还有其他方法吗?

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

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