chain
/**
* Copyright _ryotaros ( http://wonderfl.net/user/_ryotaros )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cbbs
*/
// forked from _ryotaros's Basic Segment
package {
import flash.display.Sprite;
import flash.events.Event;
public class FlashTest extends Sprite {
public var segment:Segment;
public var segs:Array;
public var segNum:int = 6;
public function FlashTest() {
init();
}
public function init():void {
segs =[];
for (var i:int = 0; i < segNum; i++){
segment = new Segment(60,20);
addChild(segment);
segment.x =stage.stageWidth/2;
segment.y =stage.stageHeight/2;
segs.push(segment);
}
addEventListener(Event.ENTER_FRAME, onenterframe);
}
public function onenterframe(e:Event):void {
drag(segs[0],mouseX,mouseY);
for (var i:int = 1 ;i < segNum; i++){
drag(segs[i],segs[i-1].x,segs[i-1].y);
}
}
public function drag(seg:Segment, xPos:Number, yPos:Number):void{
var dx:Number = xPos - seg.x;
var dy:Number = yPos - seg.y;
var angle:Number = Math.atan2(dy, dx);
seg.rotation = angle * 180 / Math.PI;
var w:Number = seg.getPin().x - seg.x;
var h:Number = seg.getPin().y - seg.y;
seg.x = xPos - w;
seg.y = yPos - h;
}
}
}
import flash.display.Sprite;
import flash.geom.Point;
class Segment extends Sprite {
public var segmentWidth:int;
public var segmentHeight:int;
public var segmentColor:int;
public var vx:Number = 0;
public var vy:Number = 0;
public function Segment(sWidth:int=10,sHeight:int=10,color:uint=0xff0000):void{
segmentWidth = sWidth;
segmentHeight = sHeight;
segmentColor = color;
init();
}
public function init():void{
graphics.lineStyle(0);
graphics.beginFill(segmentColor);
graphics.drawRoundRect(-segmentHeight / 2, -segmentHeight /2, segmentWidth + segmentHeight, segmentHeight, segmentHeight, segmentHeight);
graphics.endFill();
graphics.drawCircle(0, 0, 2);
graphics.drawCircle(segmentWidth, 0, 2);
}
public function getPin():Point {
var angle:Number = rotation * Math.PI / 180;
var xPos:Number = x + Math.cos(angle) * segmentWidth;
var yPos:Number = y + Math.sin(angle) * segmentWidth;
return new Point(xPos, yPos);
}
}