开发者

AS3 : How do I give my DisplayObjects custom properties?

I am very new to AS3, and I'm confused about how things that would have been simple in AS2 are so complex and tricky now. For example, I want to create a bunch TextField objects that, for some reason, rise up every frame (as in : object.y-=1; ). However, I also need each TextField to reference the TextField that was created just before it. I tried creating a variable that would hold such a reference : ThisTextField.Ref=LastTextField; but - this returns an "Access of possibly undefined property..." error. It seems I can only have custom properties on mere Objects ! This is annoying because an Object doesn't seem to accept event listeners (remember, I need them to do something every frame).

Is there a way to simply set custom properties on my TextFields, without having to use custom packages ? Or is there a way to use event listeners on Objects ? I've read something about strict mode which could allow setting properties on other objects - what are the risks of turning it off ?

(this is my first time here, so I'm开发者_开发百科 sorry if I sound confusing, or confused !)


you can also use casting to save time, by converting your extended objects to their base class, assigning the property, and then switching them back. For instance, everything extends Object so:

var myWhat:Object = myThing as Object; //where myThing is whatever your orginal object was; myWhat.customProp = "yellow";

var myNewThing:WhatEverMyObjectWasOrignially = myWhat as WhatEverMyObjectWasOrignially; trace(myNewthing.customProp);

this has been very handy for me...


They really are neither complex nor tricky, they now just happen to follow logical OO rules.

That said...

You cannot create properties on the fly with every object type in ActionScript 3, only objects that are declared as dynamic (for example: Object and Array). If you need this kind of control, you should create a subclass and extend TextField or come up with some other means of reference to prev/next such as a bi-directional linked list.


Instead of setting properties on an Object instance you should make use of Data Structures such as the Dictionary Class or an Array. In your example above where you wanted to reference the "previous" TextField, an Array could be used to maintain a reference to all of them (and then be used to iterate (loop) through them):

// Create the array which will be populated with the TextFields we want
// to move.
var textFields : Array = [ myTextField1, myTextField2 .... etc ];

// Loop through each TextField in the array and modify it's y property.
for each (var thisTextField : TextField in textFields) {
    thisTextField.y -= 1;
}

As for using EventListeners, you will want to add an EventListner to an object which implements IEventDispatcher; all MovieClips, Sprites, etc implement IEventDispatcher and are therefore capable of dispatching Events, if you are working in the Flash IDE, you could code:

// Listen out for the ENTER_FRAME event which will be dispatched by this MovieClip
addEventListener(Event.ENTER_FRAME, onEnterFrame);

// This function will be called each time it is dispatched.
function onEnterFrame(event : Event) : void {
    trace("onEnterFrame!");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜