Drag along path

I know this has been brought up before but mine’s different.

I have some script to drag along a circle (listed below) but here’s the catch: I dont want to use angles to calculate the position. I want there to be multiple objects (balls in this case) and I want the Y position of the clicked ball to BE the Y position of the mouse, then the X position gets calculated on the circle. This way now, if you have your mouse out in the far right corner of the screen, the ball is not all the way up the circle. I want it to match the mouse so that I can achieve a straight-line drag but with an x-position in the circle :slight_smile: Thanks.


var rX:Number = 150;
var rY:Number = 300;
var numItems:Number = 12;

var zeroSector:Object = sector(0, rX, rY);

function sector(degree:Number, radiusX:Number, radiusY:Number):Object {
    // coordinates of a point on ellipse
    var xpos:Number = radiusX * Math.cos(degree * Math.PI / 180);
    var ypos:Number = radiusY * Math.sin(degree * Math.PI / 180);
    
    // find the angle that has the Y-coords of the mouse position, solve for angle somehow...
    return {x:xpos, y:ypos};
}

function objPosition():Void { // detecting coordinates of a clip 
        var ratio:Number = rX / rY;

        var anAngle = Math.atan2(this._parent._xmouse, this._parent._ymouse);
           var deg:Number = 90-(Math.atan2(Math.sin(anAngle), Math.cos(anAngle) * ratio)) * (180 / Math.PI);
        
        var _sector:Object = sector(deg, rX, rY);
        this._x = _sector.x;
        this._y = _sector.y;
        //this._y = this._parent._ymouse;
}
// draw ellipse
this.createEmptyMovieClip("mc", 0);
mc._x =-90;
mc._y = 300;
this.mc.lineStyle(0, 0xAAAAAA);
this.mc.moveTo(zeroSector.x, zeroSector.y);
for (d = 0; d <= 360; d += 5) {
    var _sector:Object = sector(d, rX, rY);
    this.mc.lineTo(_sector.x, _sector.y);
}
// create draggalbe clip
mc.createEmptyMovieClip("drag_mc", 0);
mc.drag_mc.lineStyle(50, 0x002277, 100);
mc.drag_mc.lineTo(0, 1);
mc.drag_mc._x = zeroSector.x;
mc.drag_mc._y = zeroSector.y;

mc.drag_mc.onPress = function():Void  {
    this.onMouseMove = objPosition;
    this.onRelease = this.onReleaseOutside = function ():Void {
        delete this.onRelease;
        delete this.onReleaseOutside;
        delete this.onMouseMove;
    };
};

Okay so since nobody here wants to bother writing the code to help out, I’ve figured it out with a little while alone with a notebook in front of me…


var rX:Number = 150;
var rY:Number = 300;
var numItems:Number = 12;
var zeroSector:Object = sector(0, rX, rY);

this.createEmptyMovieClip("path", 0);
path._x =-90;
path._y = 300;
path.lineStyle(0, 0xAAAAAA);
path.moveTo(zeroSector.x, zeroSector.y);

for (d = 0; d <= 360; d += 5) {
    var _sector:Object = sector(d, rX, rY);
    this.path.lineTo(_sector.x, _sector.y);
}

function sector(degree:Number, radiusX:Number, radiusY:Number):Object {
    var xpos:Number = radiusX * Math.cos(degree * Math.PI / 180);
    var ypos:Number = radiusY * Math.sin(degree * Math.PI / 180);
    return {x:xpos, y:ypos};
}

function objPosition():Void { // detecting coordinates of a clip         
	var deg:Number = Math.asin(this._parent._ymouse / rY) * (180 / Math.PI);
	
	var _sector:Object = sector(deg, rX, rY);
	this._x = _sector.x;
	this._y = this._parent._ymouse;

	// find difference between original position and current position
	for (I = 0; I < numItems; I++) {
		if (path["music" + I] == this) {
			thisball = I;
			difference = this._y - ((I * (500 / (numItems))) - 232.5);
		}
	}
	// add difference to each ball
	for (I = 0; I < numItems; I++) {
		var deg:Number = Math.asin((this._y + ((I - thisball) * (500 / numItems))) / rY) * (180 / Math.PI);
		var _sector:Object = sector(deg, rX, rY);
		path["music" + I]._x = _sector.x;
		path["music" + I]._y = this._y + ((I - thisball) * (500 / numItems));
	}
}

for (var I = 0; I < numItems; I++) {
	clip = path.attachMovie("musicbar", "music" + I, I);
	
	angle = I * (100 / (numItems - 1)) - 50;
	clip._x = rX * Math.cos(angle * Math.PI / 180);
	clip._y = (I * (500 / (numItems))) - 232.5;

	clip.onPress = function():Void  {
		this.onMouseMove = objPosition;
		this.onRelease = this.onReleaseOutside = function ():Void {
			delete this.onRelease;
			delete this.onReleaseOutside;
			delete this.onMouseMove;
		}
	}
}

There needs to be a clip with the linkage name “musicbar”. Enjoy to anyone who googles this and finds it.