PDA

View Full Version : [FMX] functions



tgelston
April 29th, 2003, 01:18 PM
On the first frame (and currently the only frame) of my main timeline I have the following code

_root.plane.onEnterFrame = function() {
_y += gravity;
if (Key.isDown(Key.LEFT)) {
_x -= 1;
_rotation = -5;
} else {
_rotation = 0;
}
if (Key.isDown(Key.RIGHT)) {
_x += 5;
_rotation = 3;
} else {
_x += 2;
}
};


This works fine until I place more stuff on the stage and everything starts to follow the function. I have tried it with and without the _root. Now if I put a this before each property it works fine. why is that? if this code is being place on the plane?

I am still new to writting my code this way - any help in undesrstanding would be appreciated.

THANKS,
Tobias

lostinbeta
April 29th, 2003, 01:43 PM
By calling _root.plane.onEnterFrame you are assign all actions in that function to the movie clip with the instance name "plane" (no quotes) on the _root timeline. So you would need to use "this" instead of _root.

If you want to use the same actions on multiple clips then you will have to define the actions in a function or a prototype that can be called by each individual clip. If you want to do that, check out these tutorials....

About Prototypes:
http://www.kirupa.com/developer/actionscript/tricks/prototypes.asp

Handling Variables across functions/prototypes:
http://www.kirupa.com/developer/actionscript/spring2.htm

pom
April 30th, 2003, 07:48 AM
Yep, basically, properties (and more generally variables), when put just like that in functions, refer to the current timeline. In your case _root. Putting this. in front of the properties make them relative to object calling the function, in your case plane.