Forum Topic: As Help _xmouse, _ymouse ???

(283 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

bornstar

Reply To Post Reply & Quote

Posted at: 8/21/08 10:24 AM

bornstar NEUTRAL LEVEL 02

Sign-Up: 01/02/08

Posts: 42

How can I get my movie clip to follow the mouse ?
I already wrote those two lines :

onEnterFrame = function () {
ship._x += (_xmouse-ship._x)/5;
ship._y += (_ymouse-ship._y)/5;
};

But I want my ship to rotate slightly just like in this game :
http://www.newgrounds.com/portal/view/45 2407

sorry for my english
I'm French ...

Thank You


None

LonLonRanch

Reply To Post Reply & Quote

Posted at: 8/21/08 10:44 AM

LonLonRanch LIGHT LEVEL 17

Sign-Up: 05/22/05

Posts: 320

I'd expect this tutorial at Kirupa.com could help you out.


None

Paranoia

Reply To Post Reply & Quote

Posted at: 8/21/08 10:46 AM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,697

You'll need to use some trig to work out the angle between the ship and the mouse - look for the trig functions from the Math class (sin, cos, tan, atan, atan2, etc).

You'll also need to convert from radians to degrees for the rotation:

degrees = radians * 180 / Math.PI;
radians = degrees * Math.PI / 180;
BBS Signature

None

bornstar

Reply To Post Reply & Quote

Posted at: 8/21/08 10:50 AM

bornstar NEUTRAL LEVEL 02

Sign-Up: 01/02/08

Posts: 42

Thank you :-D
I came up with that :

onEnterFrame = function () {
	xDiff = _xmouse-ship._x;
	yDiff = _ymouse-ship._y;
	hDiff = Math.sqrt(xDiff*xDiff+yDiff*yDiff);
	ship._x += (xDiff)/5;
	ship._y += (yDiff)/5;
	//ship._x += Math.random()*5-2.5;
	//ship._y += Math.random()*5-2.5;
	if (hDiff>50) {
		ship._rotation += Math.round((Math.asin(yDiff/hDiff)*180/Math.PI-ship._rotation)/20);
	} else {
		ship._rotation -= ship._rotation/Math.sqrt(Math.pow(ship._rotation, 2));
	}
};

It's the best I could do ...
If someone has something more clever ?


None

bornstar

Reply To Post Reply & Quote

Posted at: 8/21/08 11:52 AM

bornstar NEUTRAL LEVEL 02

Sign-Up: 01/02/08

Posts: 42

no one ?


None

RaZzy86

Reply To Post Reply & Quote

Posted at: 8/21/08 12:32 PM

RaZzy86 NEUTRAL LEVEL 03

Sign-Up: 11/25/05

Posts: 12

On the top off my head, check the distance between your ship and the mouse pointer. If the mouse is further away speed increases etc. you can figure that out.
Use pythagaros to get the distance.

I use this class for that (AS3 though, but doesn't make that much of a difference i think)

public static function pyth(stX:Number, stY:Number, ndX:Number, ndY:Number):Number {
					return Math.sqrt( (stX - ndX)*(stX - ndX) + (stY - ndY)*(stY - ndY) )
				}

None

RaZzy86

Reply To Post Reply & Quote

Posted at: 8/21/08 12:34 PM

RaZzy86 NEUTRAL LEVEL 03

Sign-Up: 11/25/05

Posts: 12

Woops you already did that..


None

Toast

Reply To Post Reply & Quote

Posted at: 8/21/08 12:45 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,924

Le jeu que tu as montré n'est pas si simple que ça a programmer. Mais je te souhaite bonne chance quand-meme. Je n'ai pas pu tester ton code, mais je pense que quelque chose comme cela devrait marcher.

onEnterFrame = function () {
xDiff = _xmouse-ship._x;
yDiff = _ymouse-ship._y;
angle = Math.atan(_root._ymouse-_y,_root._xmouse -_x)*(180/Math.PI)+90;
ship._x += (xDiff)/5;
ship._y += (yDiff)/5;
ship._rotation = angle;
};

Il se peut que l'angle soie a l'envers, dans ce cas tu devrais probablement changer le "+90" dans angle a "-90". Si tu veux que l'angle de ton ship soie plus légérement modifié par la souris, modifie le variable "angle" en le divisant par le nombre que tu veux. Pour cela tu dois aussi verifier que quand ton ship est a 0 rotation, sa direction est vers la droite.


None

bornstar

Reply To Post Reply & Quote

Posted at: 8/21/08 03:03 PM

bornstar NEUTRAL LEVEL 02

Sign-Up: 01/02/08

Posts: 42

Merci Toast, mais ton code fonctionne moins bien que le mien ...

Thanks Toast, but your code doesn't works as good as mine ...

Anyone else ?


None

bornstar

Reply To Post Reply & Quote

Posted at: 8/24/08 06:50 AM

bornstar NEUTRAL LEVEL 02

Sign-Up: 01/02/08

Posts: 42

... :-D ? Where are the programmers :-D


None

hesselbom

Reply To Post Reply & Quote

Posted at: 8/24/08 07:01 AM

hesselbom NEUTRAL LEVEL 01

Sign-Up: 07/19/08

Posts: 309

Pretty easy actually.

See if you can work out was is going on in the provided code.

onEnterFrame = update;

function update():Void {
	var x_spd:Number = (_xmouse - ship._x) / 5;
	var y_spd:Number = (_ymouse - ship._y) / 5;
	
	ship._x += x_spd;
	ship._y += y_spd;
	ship._rotation = Math.atan2(y_spd / 20, 1) * to_deg;
};

var to_deg:Number = 180 / Math.PI;

All times are Eastern Standard Time (GMT -5) | Current Time: 04:15 PM

<< 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!