View Full Version : circular movement towards mouse
pi3rc3
December 12th, 2006, 04:42 PM
i was looking at the circular movement tutorial on this site and am now trying to make it so the object points towards the mouse but only moves around that circular axis. any ideas on how i can do this in as3?
nathan99
December 12th, 2006, 11:22 PM
I don't get it :/
CriTiCeRz
December 12th, 2006, 11:31 PM
nathan I think he means that he wants an mc to follow the mouse making circles.. lol
pi3rc3
December 12th, 2006, 11:53 PM
i made alittle diagram to explain..
http://www.wormp.com/pictures/circular.GIF
where the bluish line is the path i want the movie clip (red circle) to move along. but as you see it adjust its position based on the mouses location relative to the center of the path.
hope that clears up any confusion
nathan99
December 13th, 2006, 12:01 AM
ah, like this;
var radius:Number = 50;
//The radius of the circle
var centerX:Number = 200;
//The center X position of the circle
var centerY:Number = 200;
//The center Y position of the circle
MovieClip.prototype.startCycle = function():Void {
this.onEnterFrame = function():Void {
this.ang = Math.atan2(this._y-_ymouse, this._x-_xmouse);
//Rotate depending on mouse location
this._x = centerX+(Math.cos(this.ang)*radius);
//Move along the X
this._y = centerY+(Math.sin(this.ang)*radius);
//Move along the Y
};
};
im0.startCycle();
?
nathan99
December 13th, 2006, 12:02 AM
^^ im0 is the instance name of the red circle
pi3rc3
December 13th, 2006, 12:18 AM
exactly, thanks.
pi3rc3
January 22nd, 2007, 09:43 PM
now im trying to get the same effect but so the circle points towards the mouse and not away. ive tried to change the calculations with no success. anyone know how this could be accomplished?
jjcorreia
January 22nd, 2007, 10:32 PM
.prototypes...yuck :P
nathan99
January 23rd, 2007, 10:10 AM
var radius:Number = 50;
//The radius of the circle
var centerX:Number = 200;
//The center X position of the circle
var centerY:Number = 200;
//The center Y position of the circle
MovieClip.prototype.startCycle = function(toMouse:Boolean):Void {
this.onEnterFrame = function():Void {
this.ang = Math.atan2(centerY-_ymouse, centerX-_xmouse);
//Rotate depending on mouse location
this._x = centerX+(Math.cos(this.ang)*radius)*(toMouse ? 1 : -1);
//Move along the X
this._y = centerY+(Math.sin(this.ang)*radius)*(toMouse ? 1 : -1);
//Move along the Y
};
};
circle1.startCycle(true); // true moves to mouse
circle2.startCycle(false); // false moves away from mouse
Fixed the formula ;)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.