View Full Version : tracking X and Y coordinates of movieclips
Molybdenum
08-18-2009, 02:14 AM
Ok, dumb question, but I'm new to this.
I have a movie clip object, created within CS3 (v9). It's just a box that's motion tweened to move from left to right. I placed it on the stage and gave the clip the name "box1". Now I add this code to the first frame of the stage:
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame(Event)
{
trace(box1.x);
}
I play it, and the box moves across the screen, and the trace screen says:
357.45000000000005
357.45000000000005
357.45000000000005
357.45000000000005
357.45000000000005
forever. This seems to be the X position of where I dropped the box on the stage originally. But I want the position of the box at each frame. How do I reference that number?
Thanks in advance for helping a newbie!
senocular
08-18-2009, 03:31 AM
What exactly is the tween on? If you created a tween that existed in your library, then took that and added it to the main timeline with the name box1, then the object on the main timeline isn't moving, the tween inside of it is. Is that what's happening?
Molybdenum
08-18-2009, 04:55 AM
What exactly is the tween on? If you created a tween that existed in your library, then took that and added it to the main timeline with the name box1, then the object on the main timeline isn't moving, the tween inside of it is. Is that what's happening?
Yeah, I think that's right. I created a MovieClip object in the library, and tweened inside of that. Then I dropped that MovieClip onto the stage and called it box1.
So I guess the position of the movie clip isn't changing, which is what .x and .y refer to? How do I refer to where the object in the movie clip is at any given frame? Or am I going about this wrong, and I shouldn't be using a MovieClip in the first place?
senocular
08-18-2009, 03:03 PM
Using MovieClips is correct - they're the interactive symbol, well those and obviouslt Button symbols (but not graphics).
x and y locations indicate where in the current coordinate space, the 0,0 coordinate of another movie clip in that space is located. For the case of your box1, as a movie clip, it represents a self-contained timeline with its own coordinate space - the space in which the actual tween was made. Since the 0,0 point (which in Flash is represented by a small cross hair, normally positioned in conjunction with a white circle) within that space does not move within the main timeline space, the x and y of box1 remain the same - where it was placed on the main timeline when it was dragged there from the library.
If you want the actual tween position, then you would need to make sure that the object being tweened is a MovieClip instance, and that it has an instance name that you can refer to. Be careful, though, since instance names relate to keyframes (for CS3- keyframes or "classic" keyframes in CS4) so you may need to make sure that your tween has the same instance name throughout the full tween duration. Either that or name your object before tweening.
With your current setup, if the tweened object has a name, you would then reference the location through something like box1.tweenInstanceName.x; where tweenInstanceName would be the instance name you gave your tweened object.
Molybdenum
08-19-2009, 04:45 AM
Aha! Thanks senocular, that worked. Once I changed it to a movie-clip-inside-a-movie-clip, I was able to refer to the tweened instance's X and Y coordinates. I noticed they're relative to the position I dropped the movieclip in, though, so I had to add them together to get their absolute position. Thanks again!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.