紐のようなものをドラッグする:IK drag (6)
/**
* Copyright _wonder ( http://wonderfl.net/user/_wonder )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/lpNe
*/
// forked from _wonder's 複数のセグメントを連動させドラッグする:IK drag (5)
// forked from _wonder's 2つのセグメントを連動させドラッグする:IK drag (4)
// forked from _wonder's セグメントをドラッグする:IK drag (3)
// forked from _wonder's セグメントをマウスに向ける:IK(2)
// forked from _wonder's セグメントを描画:IK(1)
package {
import flash.display.Sprite;
import flash.events.Event;
public class InverseKinematics extends Sprite {
private var segments:Array;
private var num:Number = 100;
public function InverseKinematics() {
init();
}
private function init():void {
segments = new Array();
//
for( var i:uint = 0; i < num; i++ ){
var seg:Segment = new Segment( 4, 4, 0x000000 );
addChild( seg );
segments.push( seg );
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame( e:Event ):void {
drag( segments[0], mouseX, mouseY );
for( var i:uint = 1; i < num; i++ ){
drag( segments[i], segments[i-1].x, segments[i-1].y );
}
}
/* それぞれのセグメントの位置を決定する */
private function drag(seg:Segment, posX:Number, posY:Number):void {
var dx:Number = posX - seg.x;
var dy:Number = posY - 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 = posX - w;
seg.y = posY - h;
}
}
}
import flash.display.Sprite;
import flash.geom.Point;
class Segment extends Sprite {
private var color:uint;
private var segmentWidth:Number;
private var segmentHeight:Number;
public var vx:Number = 0;
public var vy:Number = 0;
public function Segment(segmentWidth:Number,segmentHeight:Number,color:uint=0xffffff){
this.segmentWidth = segmentWidth;
this.segmentHeight = segmentHeight;
this.color = color;
init();
}
public function init():void {
//セグメントの描画
graphics.lineStyle( 0 );
graphics.beginFill( color );
graphics.drawRoundRect( -segmentHeight / 2, -segmentHeight / 2, segmentWidth+segmentHeight, segmentHeight, segmentHeight, segmentHeight );
graphics.endFill();
}
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 );
}
}