迷路(くっつける練習)
/**
* Copyright figma000 ( http://wonderfl.net/user/figma000 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xXoQ
*/
// forked from aduonety's forked from: forked from: [QuickBox2D] Doll in space_2
// forked from a24's forked from: [QuickBox2D] Doll in space
// forked from paq's [QuickBox2D] Doll in space
package
{
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import com.actionsnippet.qbox.objects.CircleObject;
import com.actionsnippet.qbox.QuickBox2D;
import com.actionsnippet.qbox.QuickObject;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.events.Event;
import frocessing.math.*;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import jp.progression.commands.lists.*;
import flash.display.Sprite;
[SWF(width = 465, height = 465, backgroundColor = 0x333333, frameRate = 60)]
public class Sample extends MovieClip
{
private var sim:QuickBox2D;
private var fixedPoint:b2Vec2 = new b2Vec2(7.25, 7.25);
private var scale:Number = 10;
private var balls:Array = new Array();
private var list:SerialList = new SerialList();
public var data:Array = new Array(16);
private var boxA:QuickObject;
private var boxB:QuickObject;
private var parts:Array = [];
///*
//フラグ用の配列の生成
public function Makearray():void
{
{
for (var i_m:int = 0; i_m < 16; i_m++)
{
data[i_m] = new Array(16);
}
for(var i:int = 0; i<16; i++){
for(var j:int = 0; j<16; j++)
{
data[i][j]= 0; //初期値0
}
}
}
}
//*/
// 配列の値を返す
public function CheckExistBox(xe:int, ye:int):int
{
var value:int = data[xe][ye];
return value;
}
public function Sample():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
sim = new QuickBox2D(this);
sim.gravity = new b2Vec2(0, 30);
sim.iterations = 10;
//sim.createStageWalls();
//sim.addBox( { x:0,y:15.5,width:600,height:1,density:0} );
sim.setDefault( { fillColor:0xFFFFFF, lineColor:0x333333 } );
//玉の生成
for (var i:int = 0; i < 35; i++)
{
balls.push(sim.addCircle( { x:Math.random() * 14, y:Math.random() * 2, radius:0.15 } ));
}
{
//枠生成
for(var ix:int = 0; ix < 15; ix++){
for(var iy:int = 0; iy < 15; iy++){
if(ix == 0 || ix == 14 || iy == 0 || iy == 14){
/*
//sim.addBox( { x:ix, y:iy, width:1, height:1} );
/////////////////////////////////////////////
if(ix==0 && iy==0)
{
//boxA = sim.addBox( { x:ix, y:iy, width:1, height:1} );
parts.push(sim.addBox( { x:ix, y:iy, width:1, height:1} ));
}
else
{
//boxB = sim.addBox( { x:ix, y:iy, width:1, height:1} );
//boxA = sim.addGroup({objects:[boxA, boxB], x:0, y:0, angle:0});
parts.push(
}
////////////////////////////////////////////////足した
*/
parts.push(sim.addBox( { x:ix, y:iy, width:1, height:1} ));//これでいける
}
}
}
//枠の中身
{
for(var ix_d:int = 1; ix_d < 14; ix_d++){
for(var iy_d:int = 1; iy_d < 14; iy_d++){
if(ix_d % 2 == 0 && iy_d % 2 == 0){
parts.push(sim.addBox( { x:ix_d, y:iy_d, width:1, height:1} ));
Makearray();
switch (int(Math.random() * 4)) // 0~3のランダム値を生成
{
case 0: ix_d++; // 右に倒す
if(CheckExistBox(ix_d,iy_d) == 1){
ix_d--;
break;
}
else {
parts.push(sim.addBox( { x:ix_d, y:iy_d, width:1, height:1} ));
data[ix_d][iy_d] = 1;
ix_d--;
break;
}
case 1: ix_d--; // 左
parts.push(sim.addBox( { x:ix_d, y:iy_d, width:1, height:1} ));
ix_d++;
break;
case 2: iy_d++; // 下
parts.push(sim.addBox( { x:ix_d, y:iy_d, width:1, height:1} ));
iy_d--;
break;
case 3: iy_d--; // 上
parts.push(sim.addBox( { x:ix_d, y:iy_d, width:1, height:1} ));
iy_d++;
break;
}
}
}
}
}
}
boxA= sim.addGroup({objects:parts, x:0.75, y:0.5, angle:0});
boxB= sim.addJoint({type:"revolute", a:boxA.body, b:sim.w.GetGroundBody()});
sim.start();
sim.mouseDrag();
}
private function loop(e:Event):void
{
for ( var i:int = 0; i < balls.length; i ++ )
{
var ball:QuickObject = balls[i];
var fp:b2Vec2 = fixedPoint.Copy();
fp.Subtract(ball.body.GetPosition());
fp.Normalize();
fp.Multiply(50);
balls.push(sim.addCircle( { x:Math.random() * 15.5, y:Math.random() * 7, radius:0.6 } ));
ball.body.ApplyForce(fp, ball.body.GetWorldCenter());
ball.body.ApplyForce(new b2Vec2(0, -30), ball.body.GetWorldCenter());
}
}
}
}