reach
Reaching with a single segment
the target will be the mouse. You need the distance between x and y axes. Use Math.atan2 for the angle between in radians. Convert to degrees and voila!
package {
import flash.display.Sprite;
import flash.events.Event;
public class UnoSegment extends Sprite
{
private var segmentU:Segment;
public function UnoSegment ()
{
init();
}
private function init():void
{
segmentU = new Segment(200, 30)
addChild(segmentU);
segmentU.x = stage.stageWidth / 2;
segmentU.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var dx:Number = mouseX - segmentU.x;
var dy:Number = mouseY - segmentU.y;
var angle:Number = Math.atan2(dy, dx);
segmentU.rotation = angle * 180 / Math.PI;
}
}
}
drag
Dragging with a single segment
this is not using StartDrag and StopDrag methods (but you could). The segment is attached to the mouse at the second pivot point. Same as the reach method(rotate sprite to mouse) with one more step. Move the segment into position where the second pivot point is the exact same position as the mouse.
You need: distance between 2 pins for each axis. Take difference from segments getPin() point and its current x, y location, call it w & h. subtract w & h from current mouse position. This is where to put the segment. only OnEnterFrame method changed from reach.as
private function onEnterFrame(event:Event):void
{
var dx:Number = mouseX - segmentU.x;
var dy:Number = mouseY - segmentU.y;
var angle:Number = angle * 180 / Math.PI;
//new stuff and junk
var w:Number = segmentU.getPin().x - segmentU.x;
var h:Number = segmentU.getPin(). - segmentU.y;
segmentU.x = mouseX - w;
segmentU.y = mouseY - h;
}
The segment is perma attached to mouse and rotates to drag behind it. Push the segment around in the opposite direction!
that's it that's all for now folks.. over and out. shsshchssshhhshsh...