0

I'm making a simple game in AS3 for android/apple phones and I'm using the Accelerometer to move around a little guy through a tunnel thing. So I want to know how to make him look in the direction that he is moving in.

I have a movieclip called "ball" which is the guy. Then I use the "AccelerometerEvent.Update" eventlistener to a function called "accUpdate" where I've made two variables which are "accY" and "accY" which are being updated as you move the phone in different directions. Then in my main update function I have this snippet of code for the ball to move

if (!isNaN(accX)){
    ball.x -= accX*15;
}
if(!isNaN(accY)){
    ball.y += accY*15;
}

Please tell me if there is any more information you need to do this. Thanks in advance :D

akmozo
  • 9,829
  • 3
  • 28
  • 44

1 Answers1

0

Keep track of the previous position (x and y). It's better to even track few, not just one (will be smoother). Then use this: How to calculate the angle between a line and the horizontal axis?

It's basically using Math.atan2, but you'd better read it thorough. This way you know the "last" position, the new one, and then you calculate the angle that you need to rotate the object to. It's like drawing a line between two points (old position and new position).

Community
  • 1
  • 1
Andrey Popov
  • 7,362
  • 4
  • 38
  • 58