増やしてみた forked from: Droid
/**
* Copyright paq ( http://wonderfl.net/user/paq )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zNrH
*/
package
{
import com.actionsnippet.qbox.QuickBox2D;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* Droid 君を増やしてみた.
*
* @author paq
* @version 0.2
*/
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]
public class Droid2 extends Sprite
{
public function Droid2()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private var _qbox:QuickBox2D;
private var _canvas:MovieClip;
private function init(event:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_canvas = new MovieClip();
addChild(_canvas);
_qbox = new QuickBox2D(_canvas, {debug:false});
_qbox.start();
_qbox.mouseDrag();
QuickBox2DUtils.createStageWalls(_qbox, {top:false});
for (var i:int = 0; i < 5; i++)
{
new DroidObject(_qbox, { scale: 0.3, x: 2 + i * 1.5, fillColor:Math.random()*0xFFFFFF } );
}
var timer:Timer = new Timer(3000);
timer.addEventListener(TimerEvent.TIMER, function():void {
timer.removeEventListener(TimerEvent.TIMER, arguments.callee);
timer = null;
new DroidObject(_qbox, { x: 5, y: -1, scale: 1 } );
});
timer.start();
}
}
}
import Box2D.Common.Math.b2Vec2;
import com.actionsnippet.qbox.QuickBox2D;
import com.actionsnippet.qbox.QuickObject;
import flash.display.Graphics;
import flash.display.Sprite;
/**
* QuickBox2Dで簡単にDroid君を作成するためのクラスです.
*
* <p>QuickObjectを継承していません。</p>
*
* @author paq
*/
class DroidObject
{
/**
* 新しい DroidObject クラスのインスタンスを作成します.
*
* @param qbox
* @param params
*/
public function DroidObject(qbox:QuickBox2D, params:Object = null)
{
this.qbox = qbox;
this.params = params;
init();
}
public var qbox:QuickBox2D;
public var params:Object;
public var defaults:Object;
/**
* 初期化します.
*
* @private
*/
private function init():void
{
defineDefaults();
setDefaults();
build();
}
/**
* Droid君を作成します.
*
* @private
*/
private function build():void
{
var p:Object = params;
// angle は指定されるとおかしなことになるので、変数に格納して0にする
var angle:Number = p.angle;
p.angle = 0;
qbox.setDefault(p);
//qbox.setDefault( { fillColor:0, lineAlpha:1, fillAlpha:0 } );
// よく使うパラメーター
var x:Number = p.x;
var y:Number = p.y;
var scale:Number = p.scale;
// ドロイド君の中心座標
var centerX:Number = x - 2 * scale * (1 / scale);
var centerY:Number = y - 3 * scale * (1 / scale);
// スキン
var skin:Sprite = new Sprite();
var g:Graphics = skin.graphics;
g.beginFill(p.fillColor);
g.drawCircle(0, -50 * scale, 60 * scale);
g.drawRect(-60 * scale, -60 * scale, 120 * scale, 5 * scale);
g.endFill();
g.beginFill(p.fillColor);
g.drawRoundRect(-60 * scale, -55 * scale, 120 * scale, 115 * scale, 15, 15);
g.beginFill(p.eyeColor);
g.drawCircle(-30 * scale, -80 * scale, 4 * scale);
g.drawCircle(30 * scale, -80 * scale, 4 * scale);
qbox.main.addChild(skin);
var v:b2Vec2;
var groups:Vector.<QuickObject> = new Vector.<QuickObject>();
// 頭
var head:QuickObject = qbox.addCircle({ x: x, y: y, radius: 2 * scale, fillAlpha:0 });
// 耳 アンテナかもしれない
var earLeft:QuickObject = qbox.addBox({
x: x - 1.2 * scale,
y: y - 1.8 * scale,
width: 1 * scale,
height: 0.2 * scale,
angle: 1
});
var earRight:QuickObject = qbox.addBox({
x: x + 1.2 * scale,
y: y - 1.8 * scale,
width: 1 * scale,
height: 0.2 * scale,
angle: -1
});
// 胴体を作る
var bodyBox:QuickObject = qbox.addBox({
x: x,
y: y + 1.8 * scale,
width: 4 * scale,
height: 4 * scale,
fillAlpha: 0
});
bodyBox.userData = skin;
// 顔と体と耳(?)をひとまとめにする
var bodyGroup:QuickObject = qbox.addGroup( { x: centerX, y: centerY, objects: [head, bodyBox, earLeft, earRight] } );
groups.push(bodyGroup);
// 角を丸くするためのパラメーター
var circleParamsA:Object = {
x: x - 0.5 * scale,
y: y - 1.0 * scale,
radius: 0.5 * scale
}
var circleParamsB:Object = {
x: x - 0.5 * scale,
y: y + 1.0 * scale,
radius: 0.5 * scale
}
// 左腕を作る
var armLeft:QuickObject = qbox.addBox({
x: x - 0.5 * scale,
y: y,
width: 1 * scale,
height: 2.2 * scale
});
var armLeftCircleA:QuickObject = qbox.addCircle(circleParamsA);
var armLeftCircleB:QuickObject = qbox.addCircle(circleParamsB);
var armLeftGroup:QuickObject = qbox.addGroup( { x: bodyGroup.x - 2.2 * scale, y: bodyGroup.y + 1.2 * scale, objects: [armLeftCircleA, armLeft, armLeftCircleB] } );
groups.push(armLeftGroup);
// 胴体と繋げる
v = armLeftGroup.body.GetWorldCenter();
qbox.addJoint({
type: QuickBox2D.DISTANCE,
a:armLeftGroup.body,
b:bodyGroup.body,
x1: v.x,
y1: v.y - 1.2 * scale,
x2: v.x + 1.2 * scale,
y2: v.y - 1.2 * scale
});
// 右腕を作る
var armRight:QuickObject = qbox.addBox({
x: x - 0.5 * scale,
y: y,
width: 1 * scale,
height: 2.2 * scale
});
var armRightCircleA:QuickObject = qbox.addCircle(circleParamsA);
var armRightCircleB:QuickObject = qbox.addCircle(circleParamsB);
var armRightGroup:QuickObject = qbox.addGroup( { x: bodyGroup.x + 3.2 * scale, y: bodyGroup.y + 1.2 * scale, objects: [armRightCircleA, armRight, armRightCircleB] } );
groups.push(armRightGroup);
// 胴体と繋げる
v = armRightGroup.body.GetWorldCenter();
qbox.addJoint({
type: QuickBox2D.DISTANCE,
a:armRightGroup.body,
b:bodyGroup.body,
x1: v.x,
y1: v.y - 1.2 * scale,
x2: v.x - 1.2 * scale,
y2: v.y - 1.2 * scale
});
// 左足を作る
var legLeft:QuickObject = qbox.addBox({
x: x - 0.5 * scale,
y: y,
width: 1 * scale,
height: 1.8 * scale
});
var legLeftCircleA:QuickObject = qbox.addCircle({
x: x - 0.5 * scale,
y: y + 0.8 * scale,
radius: 0.5 * scale
});
var legLeftGroup:QuickObject = qbox.addGroup( { x: bodyGroup.x - 0.5 * scale, y: bodyGroup.y + 4 * scale, objects: [legLeftCircleA, legLeft] } );
groups.push(legLeftGroup);
// 胴体と繋げる
v = legLeftGroup.body.GetWorldCenter();
qbox.addJoint( {
type: QuickBox2D.REVOLUTE,
a: legLeftGroup.body,
b: bodyGroup.body,
x1: v.x,
y1: v.y - 0.7 * scale,
collideConnected: false,
enableLimit: true,
lowerAngle: -1,
upperAngle: 0.5
});
// 右足を作る
var legRight:QuickObject = qbox.addBox({
x: x - 0.5 * scale,
y: y,
width: 1 * scale,
height: 1.8 * scale
});
var legRightCircleA:QuickObject = qbox.addCircle({
x: x - 0.5 * scale,
y: y + 0.8 * scale,
radius: 0.5 * scale
});
var legRightGroup:QuickObject = qbox.addGroup( { x: bodyGroup.x + 1.5 * scale, y: bodyGroup.y + 4 * scale, angle:angle, objects: [legRightCircleA, legRight] } );
groups.push(legRightGroup);
// 胴体と繋げる
v = legRightGroup.body.GetWorldCenter();
qbox.addJoint( {
type: QuickBox2D.REVOLUTE,
a: legRightGroup.body,
b: bodyGroup.body,
x1: v.x,
y1: v.y - 0.7 * scale,
collideConnected: false,
enableLimit: true,
lowerAngle: -0.5,
upperAngle: 1
});
for (var i:int = 0, len:int = groups.length; i < len; i++)
{
groups[i].angle = angle;
}
}
/**
* デフォルトのパラメーターを定義します.
*
* @private
*/
private function defineDefaults():void
{
defaults = {x:2, y:2, linearDamping:0, angularDamping:0, isBullet:false,
fixedRotation:false,
allowSleep: true,
isSleeping:false,
density:1.0, friction:0.5, restitution:0.2, angle:0.0,
maskBits:0xFFFF, categoryBits:1, groupIndex:0,
draggable: true,
lineColor:0xA4CA39, lineAlpha:0,
lineThickness:0,
fillColor:0xA4CA39, fillAlpha:1,
width: 1, height: 1,
eyeColor: 0xFFFFFF,
scale: 1}
}
/**
* 指定されていないパラメーターをデフォルトのパラメーターにします.
*
* @private
*/
private function setDefaults():void
{
params = params || {};
for (var key:String in defaults) {
if (params[key] == null) {
params[key]=defaults[key];
}
}
}
}
import com.actionsnippet.qbox.QuickBox2D;
/**
* QuickBox2D に関するユーティリティです.
*
* @author paq
*/
class QuickBox2DUtils
{
/**
* 新しい QuickBox2DUtils クラスのインスタンスを作成します.
*/
public function QuickBox2DUtils()
{
}
/**
* 壁を作成します.
*
* @param qbox
* @param params
*/
public static function createStageWalls(qbox:QuickBox2D, params:Object = null):void
{
var p:Object = params || {};
var defaults:Object = {
thickness: 1,
inner: false,
top: true,
bottom: true,
left: true,
right: true
};
for (var key:String in defaults)
{
if (p[key] == null) p[key] = defaults[key];
}
p.lineAlpha = p.fillAlpha = p.inner ? 1 : 0;
// 動かないようにする
p.density = 0.0;
// 画面内に表示する場合は太さを反転させる
if (p.inner) p.thickness = -p.thickness;
var sw:Number = qbox.main.stage.stageWidth / 30;
var sh:Number = qbox.main.stage.stageHeight / 30;
// Top
if (p.top)
{
p.x = sw / 2;
p.y = -p.thickness / 2;
p.width = sw;
p.height = p.thickness
qbox.addBox(p);
}
// Bottom
if (p.bottom)
{
p.x = sw / 2;
p.y = sw + p.thickness / 2;
p.width = sw;
p.height = p.thickness
qbox.addBox(p);
}
// Left
if (p.left)
{
p.x = -p.thickness / 2;
p.y = sw / 2;
p.width = p.thickness;
p.height = sh;
qbox.addBox(p);
}
if (p.right)
{
// Right
p.x = sh + p.thickness / 2;
p.y = sh / 2;
p.width = p.thickness;
p.height = sh;
qbox.addBox(p);
}
}
}