In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

3つのセグメントをリーチさせる:IK reach (3)

/**
 * Copyright _wonder ( http://wonderfl.net/user/_wonder )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ywDE
 */

// forked from _wonder's リーチの仕組み:IK reach (2)
// forked from _wonder's セグメントを描画:IK drag (1)

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class InverseKinematics extends Sprite
    {
        private var seg0:Segment;
        private var seg1:Segment;
        private var seg2:Segment;
        
        public function InverseKinematics() 
        {
            seg0 = new Segment( 60, 20, 0xccff00 );
            seg0.x = stage.stageWidth / 2;
            seg0.y = stage.stageHeight / 2
            addChild( seg0 );
            
            seg1 = new Segment( 60, 20, 0xcc0000 );
            seg1.x = stage.stageWidth / 2;
            seg1.y = stage.stageHeight / 2
            addChild( seg1 );
            
            seg2 = new Segment( 60, 20, 0xffcc00 );
            seg2.x = stage.stageWidth / 2;
            seg2.y = stage.stageHeight / 2
            addChild( seg2 );
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(e:Event):void {
            /* seg0 */
            var dx:Number = mouseX - seg0.x;
            var dy:Number = mouseY - seg0.y;
            var angle:Number = Math.atan2( dy, dx );
            seg0.rotation = angle * 180 / Math.PI;
            
            var w:Number = seg0.getPin().x - seg0.x;
            var h:Number = seg0.getPin().y - seg0.y;
            var tx:Number = mouseX - w;
            var ty:Number = mouseY - h;
            
            /* seg1 */
            dx = tx - seg1.x;
            dy = ty - seg1.y;
            angle = Math.atan2( dy, dx );
            seg1.rotation = angle * 180 / Math.PI;
            
            w = seg1.getPin().x - seg1.x;
            h = seg1.getPin().y - seg1.y;
            tx = tx - w;
            ty = ty - h;
            
            /* seg2 */
            dx = tx - seg2.x;
            dy = ty - seg2.y;
            angle = Math.atan2( dy, dx );
            seg2.rotation = angle * 180 / Math.PI;
            
            seg2.x = stage.stageWidth / 2;
            seg2.y = stage.stageHeight / 2;
            
            seg1.x = seg2.getPin().x;
            seg1.y = seg2.getPin().y;
            
            seg0.x = seg1.getPin().x;
            seg0.y = seg1.getPin().y;
        }
        
    }

}

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();
        
        //ピン
        graphics.drawCircle( 0, 0, 2 );
        
        graphics.lineStyle( 0, 0xcc0000 );
        graphics.beginFill( 0xcc0000 );
        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 );
    }
}