forked from: 【AS100本ノック】15回目:伸びる
AS100本ノック
* 15回目のお題は「伸びる」
* あなたなりの「伸びる」を表現してください。
*
* 参考 http://l00oo.oo00l.com/blog/archives/264
* 「覇王色」の覇気・・・!!!のあの人です。
/**
* Copyright mex_takagi ( http://wonderfl.net/user/mex_takagi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/neP4
*/
// forked from mex's 【AS100本ノック】15回目:伸びる
/**
* AS100本ノック
* 15回目のお題は「伸びる」
* あなたなりの「伸びる」を表現してください。
*
* 参考 http://l00oo.oo00l.com/blog/archives/264
* 「覇王色」の覇気・・・!!!のあの人です。
**/
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Point;
import flash.ui.Mouse;
[SWF(width="465", height="465", backgroundColor="#FFFFFF")]
public class Main extends Sprite
{
private var _rightLeg:RubberParts;
private var _leftLeg:RubberParts;
private var _rightArm:RubberParts;
private var _leftArm:RubberParts;
private var _head:Sprite;
private var _body:Sprite;
private var _waist:Sprite;
/**
* constructor
*/
public function Main()
{
_head = head();
_body = body();
_waist = waist();
_rightLeg = new RubberParts(8, 15);
_leftLeg = new RubberParts(8, 15);
_rightArm = new RubberParts(8, 15);
_leftArm = new RubberParts(8, 15);
addChild(_head);
addChild(_rightLeg);
addChild(_leftLeg);
addChild(_body);
addChild(_waist);
addChild(_rightArm);
addChild(_leftArm);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
Mouse.hide();
}
/**
* enterFrameHandler
* @param event Event
*/
private function enterFrameHandler(event:Event):void
{
var baseX:Number = mouseX;
var baseY:Number = mouseY;
_head.x = baseX;
_head.y = baseY - 30;
_body.x = baseX;
_body.y = baseY;
_waist.x = baseX;
_waist.y = baseY + 80;
_rightArm.move(new Point(baseX - 25, baseY));
_leftArm.move(new Point(baseX + 25, baseY));
_rightLeg.move(new Point(baseX - 15, baseY + 130));
_leftLeg.move(new Point(baseX + 15, baseY + 130));
}
////////////////////////////////////////////////////////////////////////////////
//
// 以下、体のパーツ
//
///////////////////////////////////////////////////////////////////////////////
/**
* head
* @return Sprite
*/
private function head():Sprite
{
var head:Sprite = new Sprite();
var face:Shape = circle(0xF1BB93, 22);
face.scaleX = 0.9;
var leftEye:Shape = circle(0x000000, 4);
var rightEye:Shape = circle(0x000000, 4);
leftEye.x = 8;
leftEye.y = 6;
rightEye.x = -8;
rightEye.y = 6;
var cap:Sprite = cap();
cap.y = -10;
head.addChild(face);
head.addChild(leftEye);
head.addChild(rightEye);
head.addChild(cap);
return head;
}
/**
* cap
* @param
*/
private function cap():Sprite
{
var cap:Sprite = new Sprite();
var g:Graphics = cap.graphics;
g.beginFill(0xFF9922);
g.moveTo(-20, 0);
g.curveTo(-20, -20, 0, -20);
g.curveTo(20, -20, 20, 0)
g.lineTo( -20, 0);
var red:Shape = square(0xFF0000, 40, 8);
red.x = -20;
var rib:Shape = square(0xFF9922, 70, 3);
rib.x = -35;
rib.y = 8;
cap.addChild(red);
cap.addChild(rib);
return cap;
}
/**
* 胴体パーツ
* @return Sprite
*/
private function body():Sprite
{
var sprite:Sprite = new Sprite();
var g:Graphics = sprite.graphics;
g.beginFill(0x922f2a);
g.moveTo(-22, -7);
g.lineTo(-6, -5);
g.lineTo(0, 10);
g.lineTo(6, -5);
g.lineTo(22, -7);
g.lineTo(22, 80);
g.lineTo(-22, 80);
g.lineTo( -22, -10);
g.beginFill(0xF1BB93);
g.moveTo(-6, -5);
g.lineTo(6, -5);
g.lineTo(0, 10);
g.lineTo(-6, -5);
g.endFill();
var button0:Shape = circle(0xFFFF00, 4);
var button1:Shape = circle(0xFFFF00, 4);
var button2:Shape = circle(0xFFFF00, 4);
button0.x = 2;
button0.y = 25;
button1.x = button0.x;
button1.y = button0.y + 18;
button2.x = button1.x;
button2.y = button1.y + 18;
sprite.addChild(button0);
sprite.addChild(button1);
sprite.addChild(button2);
return sprite;
}
/**
* circle
* @param color uint
* @param rad Number
* @return Shape
*/
private function circle(color:uint, rad:Number):Shape
{
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.beginFill(color);
g.drawCircle(0, 0, rad);
g.endFill();
return shape;
}
/**
* square
* @param color uint
* @param w Number
* @param h Number
* @return Shape
*/
private function square(color:uint, w:Number, h:Number):Shape
{
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.beginFill(color);
g.drawRect(0, 0, w, h);
g.endFill();
return shape;
}
/**
* 腰パーツ
* @return Sprite
*/
private function waist():Sprite
{
var waist:Sprite = new Sprite();
var g:Graphics = waist.graphics;
g.beginFill(0x033e7a);
g.moveTo(-22, 0);
g.lineTo(22, 0);
g.lineTo(29, 50);
g.lineTo(2, 50);
g.lineTo(0, 30);
g.lineTo(-2, 50);
g.lineTo(-29, 50);
g.lineTo(-22, 0);
g.endFill();
var leftRib:Shape = square(0xFFFFFF, 27, 3);
leftRib.x = 2;
leftRib.y = 50;
var rightRib:Shape = square(0xFFFFFF, 27, 3);
rightRib.x = -29;
rightRib.y = 50;
waist.addChild(rightRib);
waist.addChild(leftRib);
return waist;
}
}
}
/**
* 伸びる体のパーツ
*/
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
class RubberParts extends Sprite
{
private var _basePoint:Point;
private var _pointArray:Array;
private var _midArray:Array;
private var _canvas:Sprite;
private var _g:Graphics;
private var _points:uint;
private var _gravity:Number;
private var _spring:Number;
private var _friction:Number;
/**
* constructor
*/
public function RubberParts(points:uint, gravity:uint)
{
_pointArray = new Array();
_midArray = new Array();
_canvas = new Sprite();
_g = _canvas.graphics;
addChild(_canvas);
_points = points;
_gravity = gravity;
_spring = 1;
_friction = 0.5;
for (var i:int = 0; i < _points; i++)
{
var point:ExtendPoint = new ExtendPoint();
_pointArray.push(point);
}
}
/**
* setPoints
*/
private function setPoints():void
{
var v:Vector.<Point> = new Vector.<Point>();
for (var i:uint = 0; i < _points; i++)
{
var point:ExtendPoint = _pointArray[i];
v[i] = new Point(point.x, point.y);
}
drawSpline(v);
}
/**
* drawSpline
* @param v Vector.<Point>
*/
private function drawSpline(v:Vector.<Point>):void
{
if (v.length < 2) return;
v.splice(0, 0, v[0]);
v.push(v[v.length - 1]);
_g.clear();
_g.lineStyle(15, 0xF1BB93);
_g.moveTo(_basePoint.x, _basePoint.y);
_g.lineTo(v[0].x, v[0].y);
var numSegments:uint = 4;
for (var i:uint = 0; i < v.length - 3; i++)
{
var p0:Point = v[i];
var p1:Point = v[i + 1];
var p2:Point = v[i + 2];
var p3:Point = v[i + 3];
splineTo(p0, p1, p2, p3, numSegments);
}
}
/**
* splineTo
* @param p0 Point
* @param p1 Point
* @param p2 Point
* @param p3 Point
* @param numSegments uint
*/
private function splineTo(p0:Point, p1:Point, p2:Point, p3:Point, numSegments:uint):void
{
_g.moveTo(p1.x, p1.y);
for (var i:uint = 0; i < numSegments; i++)
{
var t:Number = (i + 1) / numSegments;
_g.lineTo
(
catmullRom(p0.x, p1.x, p2.x, p3.x, t),
catmullRom(p0.y, p1.y, p2.y, p3.y, t)
);
}
}
/**
* catmullRom
* @param p0 Number
* @param p1 Number
* @param p2 Number
* @param p3 Number
* @param t Number
* @return Number
*/
public function catmullRom(p0:Number, p1:Number, p2:Number, p3:Number, t:Number):Number
{
var v0:Number = (p2 - p0) * 0.5;
var v1:Number = (p3 - p1) * 0.5;
return (2 * p1 - 2 * p2 + v0 + v1) * t * t * t + ( -3 * p1 + 3 * p2 - 2 * v0 - v1) * t * t + v0 * t + p1;
}
/**
* enterFrameHandler
* @param event Event
*/
public function move(basePoint:Point):void
{
_basePoint = basePoint;
_pointArray[i].x = _basePoint.x;
_pointArray[i].y = _basePoint.y;
var i:uint = 0;
for (i = 1; i < _points; i++)
{
var point:ExtendPoint = _pointArray[i];
var prevPoint:ExtendPoint = _pointArray[i - 1];
point.vx += (prevPoint.x - point.x) * _spring;
point.vy += (prevPoint.y - point.y) * _spring;
point.vy += _gravity;
point.vx *= _friction;
point.vy *= _friction;
point.x += point.vx;
point.y += point.vy;
}
setPoints();
}
}
/**
* ポイント
*/
class ExtendPoint
{
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
/**
* constructor
*/
public function ExtendPoint()
{
x = 0;
y = 0;
vx = 0;
vy = 0;
}
}