View Full Version : [AS3] Rotate to Face movieclip
EightySeven
10-03-2008, 05:35 PM
I kinda jumbled this question in with a thread where i basicly talked to myself to work through part of my problem. I know how to make an object rotate to face the mouse, but the code does not work for movieclips and I know the reason why.
Heres my code
var dx:int = MovieClip(parent).player.x - turret_01.x;
var dy:int = MovieClip(parent).player.y - turret_01.y;
var cursorAngle:Number = Math.atan2(dy, dx);
var cursorDegree:Number = 360*(cursorAngle/(2*Math.PI));
turret_01.scaleX = -1;//My MovieClip was inverted this corrects the way in which it faces
turret_01.rotation = cursorDegree;
I wish for turret_01 movieclip to rotate to face my player
Draxus
10-03-2008, 09:54 PM
Are the turret and the player in the same inside the same movieclip/sprite/whatever? If not, you need to convert their coorindates to the same scope, using localToGlobal() or globalToLocal().
The code looks fine otherwise, though you could save some calculation time by doing this instead of what you have:
var cursorDegree:Number = cursorAngle * 57.32
EightySeven
10-04-2008, 02:21 AM
the player movieclip is on the stage
the turret is a movieclip inside a movieclip so Boss01.turret_01
and yes the turret does rotate to face the player in my code, but to no degree of accuracy.
I'm looking at the localToGlobal and globalToLocal in the flash docs and i'm not really understanding what exactly its saying. I thought 0,0 on a movieclip was the location on the stage
so a movieclip centered to 0.0 means that on the stage if its at 100,100 then the 0.0 point of the movieclip on the stage is 100,100...not sure if u understand that, maybe an example of how one might use these functions if you have a moment to make an example.
Thanks a bunch
EDIT: After grabbing a glass of milk I realized what u were saying.
In my player movieClip the image in centered on 0.0
In my Boss movieclip the turret is somewhere around the 60,60 mark. does this have an effect on anything?
Draxus
10-04-2008, 09:59 PM
Yeah you've got the basic idea down. The location of the player graphic inside the player mc doesn't matter since you're not testing the graphic, but the entire mc. The location of the turret inside boss is what's messing you up.
When you compare the player's position (who is on the stage) with the turret position... you need to know where the turret is ON THE STAGE, not where it is inside boss1, since you're comparing it's location to something on the stage. To do this, you'd do something like
var turretLoc:Point = new Point(turret_01.x, turret_01.y)
turretLoc = boss1.localToGlobal(turretLoc)
then use this loc instead of the turret's loc:
var dx:int = player.x - turretLoc.x;
var dy:int = playey.y - turretLoc.y;
var cursorAngle:Number = Math.atan2(dy, dx);
var cursorDegree:Number = cursorAngle * 57.32
turret_01.rotation = cursorDegree;
EightySeven
10-05-2008, 02:28 AM
Awesome thanks.
Its good to know I almost had it :) and I'm not losing my mind. It works flawlessly now, and tomorrow when I have time I'm hoping to finish this project off. Its my first flash project so far I've learned lots, the next one will be even better.
Its good to know where at Flash Jedi out there to help Flash Padwans like myself.
Draxus
10-05-2008, 03:18 AM
No problem, glad to help.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.