[英]AS3 Dynamically change image of object
How does one change image of already created object 如何更改已创建对象的图像
Let's say I have an empty class named Icon and on runtime I want to change its image to something and add it on stage. 假设我有一个名为Icon的空类,并且在运行时我想将其图像更改为某种图像并在舞台上添加它。 The image can be from the library as another object or just a bitmap.
该图像可以来自库中的另一个对象,也可以只是位图。
var icon1 = new Icon(myIcon);
...
public class Icon extends MovieClip {
public function Icon(icon:Class) {
//change image
addChild(this);
}
EDIT: in java I used to do it like this: 编辑:在Java中,我曾经这样做:
new Icon(myIcon);
...
public class Icon {
public Image image;
public String icon;
public Icon(String icon) {
ImageIcon ii = new ImageIcon(this.getClass().getResource(icon + ".png"));
image = ii.getImage();
this.icon = icon;
}
and on paint method I just type icon1.image
to show on screen and also can change image if needed by accessing this variable. 在绘画方法上,我只需键入
icon1.image
即可在屏幕上显示,也可以根据需要通过访问此变量来更改图像。
Is this possible to do it something like this on as3? 在as3上可以这样做吗?
Let's say I have an empty class named Icon and on runtime I want to change its image to something
假设我有一个名为Icon的空类,并且在运行时我想将其图像更改为某种
Before code make sure you prepared by following these example steps... 在编写代码之前,请确保您已按照以下示例步骤进行了准备...
1) Open the Library (ctrl+L) and right click to choose "create new symbol" of MovieClip type and give it the name Icon then look at the Advanced tab further below... 1)打开媒体库 (ctrl + L),右键单击以选择MovieClip类型的“创建新符号”,并为其命名为Icon,然后在下面的“ 高级”选项卡中查看...
2) Tick the box called "Export For ActionScript" and that should auto-fill a box called Class with text saying Icon . 2)勾选名为“为ActionScript导出”的框,然后应使用名为Icon的文本自动填充一个名为Class的框。 Click OK and you are ready.
单击确定,您已经准备好。
3) Add one or two images to Library and right-click to choose "properties" and again like step (2) in the linkage section you tick the "Export for AS" option and give a name you prefer to type later via code. 3)向库中添加一两个图像,然后单击鼠标右键以选择“属性”,然后像在链接部分中的步骤(2)一样,选中“导出为AS”选项,并提供一个您希望稍后通过代码键入的名称。 We'll assume you chose names like Pic1 and Pic2 .
我们假设你选择喜欢PIC1和PIC2名。 Notice as well that the "base Class" for these bitmaps is bitmapData ?
还要注意,这些位图的“基类”是bitmapData ? That's the data you change to update your image by code.
这就是您更改以通过代码更新图像的数据。
So to update dynamically by code... Create a bitmap and update its pixel data with any bitmapData from Library. 因此,要通过代码动态更新...创建位图,并使用库中的任何bitmapData更新其像素数据。 Something like:
就像是:
//# in your imports...
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.getQualifiedClassName;
import flash.utils.*;
....
//# in vars declarations...
public var my_icon : Icon = new Icon ();
public var my_BMP : Bitmap;
public var str_Type : String = ""; //later holds library object type for checks
....
//# later in your code
stage.addChild(my_icon); //looks invisible since empty
my_BMP = new Bitmap; //create new instance
my_icon.addChild ( my_BMP ); //add the empty bitmap into Icon
change_Icon ( Pic1 ); //parameter can be Library's Bitmap or Movieclip
change_Icon
function... change_Icon
函数... If you just kept it bitmaps (PNG files?) you could use a simple function like this... 如果只保留位图(PNG文件?),则可以使用像这样的简单函数...
public function change_Icon ( in_Param : Class ) : void
{
my_BMP.bitmapData = new in_Param as BitmapData; //replace pixels of existing bitmap container
}
But since you say it could be anything, sometimes a Bitmap or sometimes a MovieClip, well in that case you have to check the input and handle accordingly. 但是由于您说的可能是任何东西,有时是位图或有时是MovieClip,所以在这种情况下,您必须检查输入并进行相应的处理。
public function change_Icon ( in_Param : * ) : void
{
//# Check the input type and handle it
str_Type = String ( getDefinitionByName( String( getQualifiedSuperclassName(in_Param) ) ) );
if ( str_Type == "[class BitmapData]" )
{
trace ( "this object is a Bitmap..." );
my_BMP.bitmapData = new in_Param as BitmapData; //replace pixels of existing bitmap container
}
if ( str_Type == "[class MovieClip]" )
{
trace ( "this object is a Movieclip..." );
var temp_MC = new in_Param();
var temp_BMD : BitmapData = new BitmapData( temp_MC.width, temp_MC.height );
my_BMP.bitmapData = temp_BMD; //assign data to this bitmap
temp_BMD.draw (temp_MC); //update by drawing
}
}
After adding your icons (image files) to your library, converting them to MovieClips and setting their AS linkage like this, for example : 将图标(图像文件)添加到媒体库后,将其转换为MovieClips并设置它们的AS链接,例如:
You can use, for example, a Sprite
object to load your icon inside your Icon
class instance where your Icon
class can be like this, for example : 例如,您可以使用
Sprite
对象将图标加载到Icon
类实例中,其中Icon
类可以是这样,例如:
package {
import flash.display.Sprite;
public class Icon extends Sprite
{
private var _icon:Sprite;
public function Icon(IconClass:Class): void
{
_icon = new IconClass();
addChild(_icon);
}
public function changeIcon(IconClass:Class): void
{
removeChild(_icon);
_icon = new IconClass();
addChild(_icon);
}
}
}
Then in your main code, you can use that class : 然后在您的主代码中,可以使用该类:
import Icon;
var icon:Icon = new Icon(PHPIcon);
addChild(icon);
then to change the icon, you've just to call the changeIcon()
function : 然后要更改图标,只需调用
changeIcon()
函数:
icon.changeIcon(JSIcon).
Edit : 编辑:
If you don't want to call addChild()
to add your Icon
class's instance to it's parent container, the constructor of your Icon
class can be like this for example : 如果您不想调用
addChild()
将Icon
类的实例添加到其父容器,则Icon
类的构造函数可以像这样:
public function Icon(container:DisplayObjectContainer, IconClass:Class): void
{
container.addChild(this);
_icon = new IconClass();
addChild(_icon);
}
then for its instance 然后就其实例
var icon:Icon = new Icon(this, PHPIcon);
Hope that can help. 希望能对您有所帮助。
Thanks finally now I have a base class to work with on future projects, I also use list
so all I have to do is call new Icon(myIcon, x, y, this);
终于谢谢了,现在我有了一个可以在将来的项目中使用的基类,我还使用了
list
所以我要做的就是调用new Icon(myIcon, x, y, this);
package {
import flash.display.Sprite;
import flash.display.DisplayObjectContainer;
public class Icon extends Sprite {
public static var list:Array = new Array();
private var icon:Sprite;
private var n:int;
public function Icon(IconClass:Class, x:int, y:int, container:DisplayObjectContainer):void {
container.addChild(this);
icon = new IconClass();
n = list.push(this);
addChild(icon);
this.x = x; this.y = y;
}
public function changeIcon(IconClass:Class):void {
removeChild(icon);
icon = new IconClass();
addChild(icon);
}
public function removeIcon():void {
this.parent.removeChild(this);
list.splice(n-1,1);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.