The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumTrying to make a turret face the mouse [AS3]

6 6055
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

I'm working on my first flash game, and I'm having trouble making a turret face the mouse in AS3.

The class file for the turret:

package
{
import flash.display.MovieClip;
import flash.events.Event;

public class Turret extends MovieClip
{
public function Turret()
{
x = 320;
y = 320;
addEventListener(Event.ENTER_FRAME, updateRotation)
}
public function updateRotation(evt:Event)
{
rotation = Math.atan2((mouseY - 320), (mouseX - 320)) * 180 / Math.PI;

}
}
}

It currently rotates like:
This

Any help would be greatly appreciated.

  • 6 Replies
AirDecade
offline
AirDecade
23 posts
Shepherd

if (stage!=null)
{
rotation = Math.atan2((stage.mouseY - 320), (stage.mouseX - 320)) * 180 / Math.PI;
}

AirDecade
offline
AirDecade
23 posts
Shepherd

or better:

rotation = Math.atan2(mouseY, mouseX) * 180 / Math.PI;

master565
offline
master565
4,104 posts
Nomad

rotation = Math.atan2((mouseY - 320), (mouseX - 320)) * 180 / Math.PI;


Try Turret.rotation = Math.atan2((mouseY - 320), (mouseX - 320)) * 180 / Math.PI;

And also


x = 320;
y = 320;


Replace 320 with a var such as
var turretX:number = 320;
var turretY:number = 320
x = turretX
y = turretY

And replace anywhere else you have "320".
gaboloth
offline
gaboloth
1,612 posts
Peasant

Use stage.mouseX instead of mouseX, and stage.mouseY instead of mouseY.

xDuked
offline
xDuked
29 posts
Nomad

Use stage.mouseX instead of mouseX, and stage.mouseY instead of mouseY.


But try Turret Rotation too. Keep X/Y to probably 360...
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

Yay! Thanks so much guys.

Using Turret.rotation throws up lots of errors about "Access to undefined property rotation through a class" or something like that.


It works if I do stage.mouseX and just rotation.

Showing 1-6 of 6