Frage

I'm working on a side scroller, and for the enemy I'm making a turret. I'm trying to make the turret aim at the player but I cant seem to get it right. Below is a rough sketch of what I want to achieve:

enter image description here

I want the barrel (dark blue), to aim/rotate to its pointing at the player.

I have uploaded a YouTube video of my scene:

http://www.youtube.com/watch?v=eeP47VoX9uA&feature=youtu.be

This is what I have so far (loop):

function enterFrameHandler(e : Event) : void{
   _turretBarrel.rotation = Math.atan2(enTarget.x, enTarget.y) * 180/Math.PI;
}

What this does is only rotate the barrel when I jump, and the barrel isn't even aiming at the player, also the barrel doesn't change rotation when I walk on the other side of the turret.

My enTarget.x is always central to the stage and the scene (including the turret) moves around the player left and right (x)... Only the enTarget.y moves (jump/high platform).

I'm slightly new to Flash and ActionScript. If anyone could help me out, or point me in the right direction then that would be great.

Thanks

War es hilfreich?

Lösung

1) Make sure you got the right numbers and the position of the avatar and the turret are in the same coordinate space. A simple trace of each would do. In this case you probably want the world (relative to stage) position of both clips. Make sure they make sense compared to top left corner of the screen (0, 0).

2) Remember that _turretBarrel.rotation is a rotation that ranges from -180 to 180 so this would need to be taken into consideration when calculating angles.

3) Make sure you use the corresponding degrees/radians where appropriate.

4) force focus on avatar, run the game and see if the bounds looks ok. Then do the same thing with the turret.

Another good thing in general for debugging purposes is to setup some kind of debug graphics. i.e. draw a line of what you think is the direction vector to verify your numbers and calculations.

On a side note: This is what the majority of programming is; Debugging. Assume nothing but hard facts, get your numbers from the debugger (probably quicker), or trace output. If you're still using the horrible flash professional IDE. I would really recommend getting one with a proper debugger like FlashDevelop (free) or Flash Builder (commercial)

Andere Tipps

Oliver, it looks like you are calculating the tangens of wrong angle (between player and X-axis). You need something like the following:

function enterFrameHandler(e : Event) : void{
   _turretBarrel.rotation = Math.atan2(enTarget.x - barrel.x, enTarget.y - barrel.y) * 180/Math.PI;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top