本文介绍了在影片剪辑或雪碧存储可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何数据添加到动态创建的MovieClip /精灵,使得数据可以在以后的事件配位于该MovieClip被访问/雪碧?

How can you add data to a dynamically created MovieClip/Sprite so that the data can be accessed later on an event coordinating to that MovieClip/Sprite?

例如code:


for(var i:int; i < xml.children(); i++){
    var button:MovieClip = new MovieClip();
    button.graphics.beginFill(0x000000);
    button.graphics.drawCircle(100 + 20 * i, 200, 10);
    button.graphics.endFill();
    button.addEventListener(MouseEvent.MOUSE_UP, doSomething);
    button.name = "item_"+i;
    button.storedData.itemNumber = i;
}

function doSomething(e:Event):void
{
    trace(e.target.storedData.itemNumber);
}

在此先感谢。

Thanks in advance.

推荐答案

幸运的是,在AS3中,MovieClip类定义为动态类(只有影片剪辑,精灵不是)。在已被定义为动态的一类,可以通过标准的变量赋值语句添加一个新的动态实例,该类的任何实例。

Luckily for you, in AS3, the MovieClip class is defined as a dynamic class (and only movie clips are, sprites not). In a class that has been defined as dynamic, you can add a new dynamic instance to any instance of that class via a standard variable assignment statement.

var myInstance:DynamicClass= new DynamicClass();
myInstance.foo = "hello World"; // this won't cause any compile time errors

trace(myInstance.foo ); //returns hello World

EA-SY ^ _ ^

EA-SY ^_^

现在让我们来创建动态的几个影片剪辑,然后更改其中的一个属性。

Now let's create dynamically several MovieClips and then change a property of one of them.

AS2语法:

for(var i:Number = 0; i < 10; i++){
    _root.createEmptyMovieClip("button" + i, _root.getNextHighestDepth());
}

然后,你可以直接打电话给你的影片剪辑:

Then you could call your movie clip directly :

button3._x = 100;
button3._y = 300;

或动态通过使用这样的:

or dynamically by using this :

this["button" + i]._x = 100;
this["button" + i]._y = 300;

在AS3,这是完全不同的(而且会有很多方法可以做到这一点)。

In AS3, it is quite different (and there would be many ways to do it).

AS3语法:

var button:Array = new Array();

for (var i:Number = 0; i < 10; i++) {
    var _mc:MovieClip = new MovieClip();
    addChild(_mc); // in AS3 when you create a MovieClip, it remains in memory and won't be seen on stage until you call addChild(_mc)

    button[i] = _mc;
}

然后你可以有一些乐趣,你的电影剪辑,动态:

Then you can have some fun with your movie clips, dynamically :

button[2].graphics.beginFill(0x000000);
button[2].graphics.drawCircle(100, 200, 10);
button[2].graphics.endFill();

这篇关于在影片剪辑或雪碧存储可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:11