Forum Topic: Diagonal Movement

(95 views • 6 replies)

This topic is 1 page long.

<< < > >>
Resigned

Jarts

Reply To Post Reply & Quote

Posted at: 4/4/09 07:31 PM

Jarts NEUTRAL LEVEL 02

Sign-Up: 05/24/08

Posts: 17

I have been looking around for a way to make an object move diagonally in flash and I can't seem to find anything. I need to make an object move at a 60° angle.

Please post if you have any information about this.

BBS Signature

None

Nano256

Reply To Post Reply & Quote

Posted at: 4/4/09 08:20 PM

Nano256 DARK LEVEL 13

Sign-Up: 02/12/05

Posts: 1,473

Trigonometry.

Move on to ActionScript 3.0 already!
The third post below this one is a lie.

BBS Signature

None

Jarts

Reply To Post Reply & Quote

Posted at: 4/5/09 12:39 PM

Jarts NEUTRAL LEVEL 02

Sign-Up: 05/24/08

Posts: 17

Could you elaborate on that?

BBS Signature

Happy

OctoFlash

Reply To Post Reply & Quote

Posted at: 4/5/09 12:43 PM

OctoFlash DARK LEVEL 17

Sign-Up: 12/02/06

Posts: 839

here's an example:

on the onClipEvent (load) thing i create 3 variables: xM (stands for x-movement and yM for y-movement, speed is the movespeed)

xM = 0;
yM = 0;
speed = 5;

and then on the EnterFrame thing:

xM = Math.sin(_rotation*(Math.PI/180))*speed;
yM = Math.cos(_rotation*(Math.PI/180))*speed*-1;
_x += xM;
_y += yM;

this way, it moves in the direction it faces with the speed you defined..

i hope this helped more than Nano256's comment.. ;)

BLAM DAMN SPAM. That rhymes! So what do we learn?
the right answer for the most bbs-posts: THE FREAKIN' COLLAB RULES!!!

BBS Signature

None

TheBoob

Reply To Post Reply & Quote

Posted at: 4/5/09 12:47 PM

TheBoob NEUTRAL LEVEL 12

Sign-Up: 04/12/02

Posts: 371

Everything in flash works with x and y values, if you move something on the x only, its horizontal movment. if you move something on the y, its verticle movement only. if you move both on the x and y axes, you get diagonal movment.
The trig comes in if you want to set a speed, and move based on rotation.
you probably cant just copy and paste, but this should give you an idea.

var speed:Number = 5;
var degrees:Number = 60
var angle:Number = 2*Math.PI*(degrees/360)
dx = speed*Math.cos(angle);
dy = speed*Math.sin(angle);
x += dx;
y += dy;

None

Jarts

Reply To Post Reply & Quote

Posted at: 4/5/09 01:24 PM

Jarts NEUTRAL LEVEL 02

Sign-Up: 05/24/08

Posts: 17

I'm trying to get your code to work OctoFlash but I think I am doing something wrong...
Here is was I have now:

onClipEvent(load){
xM = 12;
yM = 12;
speed = 1; stop();
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)){_x += xM; gotoAndStop(1);}
if (Key.isDown(Key.DOWN)){_y += yM; gotoAndStop(2);}
if (Key.isDown(Key.LEFT)){_x += xM; gotoAndStop(3);}
if (Key.isDown(Key.UP)){_y += yM; gotoAndStop(1);}
xM = Math.sin(_rotation*(Math.PI/180))*speed;
yM = Math.cos(_rotation*(Math.PI/180))*speed*-1;
}
BBS Signature

None

OctoFlash

Reply To Post Reply & Quote

Posted at: 4/5/09 01:27 PM

OctoFlash DARK LEVEL 17

Sign-Up: 12/02/06

Posts: 839

try this:

onClipEvent (load){
xM = 0;
yM = 0;
speed = 1;

//THE FOLLOWING VARIABLE IS TO ROTATE THE OBJECT
rot = 5;
}

onClipEvent (enterFrame){

xM = Math.sin(_rotation*(Math.PI/180))*speed;
yM = Math.cos(_rotation*(Math.PI/180))*speed*-1;

if (Key.isDown (Key.RIGHT)){
_rotation += rot;
}
if (Key.isDown (Key.LEFT)){
_rotation -= rot;
}
if (Key.isDown (Key.UP)){
_x += xM;
_y += yM;
}
if (Key.isDown (Key.DOWN)){
_x -= xM;
_y -= yM;
}
}

is this what you need?

BLAM DAMN SPAM. That rhymes! So what do we learn?
the right answer for the most bbs-posts: THE FREAKIN' COLLAB RULES!!!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 02:52 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!