ForumsProgramming Forumcharacter movement in as2

9 3229
Annihalation
offline
Annihalation
479 posts
Nomad

Alright, I'm making a game in as2 because its a lot easier than as3. I've tried and looked for tutorials, but nothing has given me the right results yet. So my question is...

I want my characters in my flash game to move in the direction they are facing. I was told the equation had something like a math sine divided by angle, times speed, but that didn't work. Can someone help me out? thanks

  • 9 Replies
joeybetz
offline
joeybetz
107 posts
Shepherd

I depends on how your characters move. Are they supposed to move only left and right like in a platformer or any direction possible. I'm assuming the latter.

myClip._rotation = Math.atan2(ySpeed, xSpeed) * 57.2958279;

That should be it. My AS2 is rusty, but I believe it's still similar to AS3.

Annihalation
offline
Annihalation
479 posts
Nomad

oh, my characters move from a top-down perspective ( A and D to rotate the main character, W to move forward, S to move backwards :P)

and I know how to get them to rotate, its the "moving forward" part I cant get

joeybetz
offline
joeybetz
107 posts
Shepherd

Ok, that is the opposite of what I posted, but in any case here is the code that you would probably use.

var a:Number = myClip._rotation * 0.017453277;
myClip._x += Math.cos(a) * speed;
myClip._y += Math.sin(a) * speed;

I believe that is it. Just update the speed value whenever W is pressed. Otherwise set it to 0.

Annihalation
offline
Annihalation
479 posts
Nomad

howcome I have to multiply my _rotation by 0.017453277 ?

Annihalation
offline
Annihalation
479 posts
Nomad

well, with that coding he moves on the x- axis correctly, but not on the y axis... ill try more tests

joeybetz
offline
joeybetz
107 posts
Shepherd

That number is short for PI/180. The rotation has to be converted from degrees to radians for both the Math.sin() and Math.cos() methods.

I wonder why the y-axis is not working. Are you doing something different to the y-axis from the x-axis?

Annihalation
offline
Annihalation
479 posts
Nomad

I wrote Math.cos instead of Math.sin lol... my bad sry...

alright, it works now... but now I got another question

how would I code a function that acts with a variable? Like I want to make a function that loops itself every frame (40 times a second on my game) as long as a variable is == 1? the guy shoots a fireball, but he only can have one fireball in existence at one time, so I made a variable for it. Now I want to make the variable for as long as the fireball exists, it travels forward at <my speed>

is this possible? and thanks for all your help

joeybetz
offline
joeybetz
107 posts
Shepherd

You should try:

function updateFireball()
{
if( !fireball ) return;

//Do fireball update code here
}

//My enter frame loop somewhere
updateFireball();

Does that make sense?

Annihalation
offline
Annihalation
479 posts
Nomad

yea, ill test it out :P

Showing 1-9 of 9