views:

34

answers:

2

Hello, 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 !)

A: 

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.

Tegeril
Well damn, so I really need to write a package for that ? (if that's what "creating a subclass" means ?) Can I add the ability to get EventListeners to Objects through packages, too ?
Orteil
No, a package can be a collection of classes (i.e.: flash.display is a package containing Sprite, Shape, etc) whereas a class is Sprite. A subclass would be your own custom object that extends a different class.
Tegeril
Ah, alright ! Thank you. I'll look into that, then.
Orteil
You're welcome and good luck. I think once you get comfortable with AS3, you'll begin to see its strengths, even just in organization. +1 for you.
Tegeril
A: 

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!");
}
JonnyReeves