加载在AS3中嵌入图像 [英] Loading an embedded image in AS3

查看:122
本文介绍了加载在AS3中嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我觉得哑问这个,但我花了整个下午试图弄明白,都没有成功。

在AS3,我想将图像添加到库(姑且称之为image.png),并只用code实例吧。

我成功的实例化一个外部图像这样

  VAR PLOAD:装载机=新的Loader();
pLoad.load(新的URLRequest(image.png));
的addChild(PLOAD);
 

但无论怎样我试过了,我不能做同样的加载方式,从库中的形象。这是怎么一回事?

另外:我已经看到了[嵌入]语法,但最好我想避免硬codeD映像的名字,那就是,我要选择一个图像,从而生成图像的名称,编程<。 / P>

解决方案

看起来是这样工作的:

  VAR PDEF:类= getDefinitionByName(image.png)为类;
变种_image:的BitmapData =新PDEF(10,10);

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

看起来丑陋和unnecesarily复杂,虽然。任何其他办法吗?

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.

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.

解决方案

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?

这篇关于加载在AS3中嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆