Finding the closest object in proxmity to the mouse Coordinates
- by Cam
Hey there,
i've been working on a problem for a while now, which involves targeting the closest movieClip in relation to the x y coords of the mouse, I've attached a nice little acompanying graphic.
Each mc added to the stage has it's own sub-class (HotSpots) which uses Pythag to measure distance from mouse. At this stage i can determine the closest value from my Main class but can't figure out how to reference it back to the movieclip... hope this makes sense. Below are the two Classes.
My Main Class which attachs the mcs, and monitors mouse movement and traces closest value
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Main extends MovieClip
{
var pos:Number = 50;
var nodeArray:Array;
public function Main(){
nodeArray = [];
for(var i:int = 0; i < 4; i++)
{
var hotSpot_mc:HotSpots = new HotSpots;
hotSpot_mc.x += pos;
hotSpot_mc.y += pos;
addChild(hotSpot_mc);
nodeArray.push(hotSpot_mc);
// set some pos
pos += 70;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE,updateProxmity)
}
public function updateProxmity(e:MouseEvent):void
{
var tempArray:Array = new Array();
for(var i:int = 0; i < 4; i++)
{
this['tf'+[i]].text = String(nodeArray[i].dist);
tempArray.push(nodeArray[i].dist);
}
tempArray.sort(Array.NUMERIC);
var minValue:int = tempArray[0];
trace(minValue)
}
}
}
My HotSpots Class
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
public class HotSpots extends MovieClip
{
public var XSide:Number;
public var YSide:Number;
public var dist:Number = 0;
public function HotSpots()
{
addEventListener(Event.ENTER_FRAME, textUp);
}
public function textUp(event:Event):void
{
XSide = this.x - MovieClip(root).mouseX;
YSide = this.y - MovieClip(root).mouseY;
dist = Math.round((Math.sqrt(XSide*XSide + YSide*YSide)));
}
}
}
thanks in advance