衝突
/**
* Copyright Ryogo_Quberey ( http://wonderfl.net/user/Ryogo_Quberey )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/Aocw
*/
// forked from Ryogo_Quberey's QuickBox2D練習 その4(ブロック崩しテスト)
// forked from Ryogo_Quberey's QuickBox2D練習 その3(移動)
// forked from Ryogo_Quberey's QuickBox2D練習 その2
// forked from Ryogo_Quberey's QuickBox2D練習
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import com.actionsnippet.qbox.*;
import Box2D.Common.Math.*;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.Joints.b2RevoluteJoint;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.utils.setTimeout;
import Box2D.Common.Math.*
import Box2D.Collision.b2AABB;
import Box2D.Dynamics.Joints.b2RevoluteJoint;
import Box2D.Dynamics.Joints.b2RevoluteJointDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2ContactListener;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
public class FlashTest extends MovieClip{
public function FlashTest() {
var sim:QuickBox2D = new QuickBox2D(this);
var sw:Number = stage.stageWidth / 30;
var sh:Number = stage.stageHeight / 30;
sim.addBox({x:0, y:sh / 2, width:1, height:sh , density:.0, lineAlpha:0, fillColor:0xffffff, restitution:1})
sim.addBox({x:sw, y:sh / 2, width:1, height:sh, density:.0, lineAlpha:0, fillColor:0xffffff ,restitution:1})
var maru_A:QuickObject = sim.addBox({x:7, y:4, width:2, height:0.3 , density:10, lineAlpha:0, fillColor:0x9400d3});
var maru_B:QuickObject = sim.addBox({x:7, y:6, width:7, height:0.2, density:10, fixedRotation:true, lineAlpha:0, fillColor:0x9400d3});
maru_A.body.SetLinearVelocity(new b2Vec2(5, 0));
var area:QuickObject = sim.addBox({x:7, y:14, width:5, height:0.5, density:0, angle:0 ,restitution:1.2, fixedRotation:false, lineAlpha:0, fillColor:0x9400d3});
//var area2:QuickObject = sim.addBox({x:13, y:10, width:5, height:0.5, density:0, angle:-0.1, restitution:1.2, fixedRotation:true, lineAlpha:0, fillColor:0x9400d3});
var j1:QuickObject = sim.addJoint({type:"revolute", a:sim.w.GetGroundBody(), b:maru_A.body, anchor:maru_A.body.GetWorldCenter()})
var j2:QuickObject = sim.addJoint({type:"prismatic", a:sim.w.GetGroundBody(),enableMotor:true, maxMotorTorque:80, axis:new b2Vec2(1, 0), b:maru_B.body, anchor:maru_B.body.GetWorldCenter()})
sim.addJoint({type:"gear", a:maru_A.body, b:maru_B.body, joint1:j1.joint, joint2:j2.joint});
/////////contactlistener
var contacting:QuickContacts = sim.addContactListener();
contacting.addEventListener(QuickContacts.ADD, onAd);
function onAd(evt:Event):void{
const SCALE:int = 30, BALL:int=10, W:int=500, H:int=500;
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(3,0xFF1493);
circle.graphics.drawCircle(0,5,5);
addChild(circle);
//衝突した座標の取得
circle.x=contacting.currentPoint.position.x*SCALE;
circle.y=contacting.currentPoint.position.y*SCALE;
/*if(contacting.currentPoint.position.y < 8){
circle.alpha = 0;
} */
//力の取得
var stl:Number = contacting.currentPoint.velocity.Length()/5;
//力の大きさに合わせて円を大きくする
var t:ITween = BetweenAS3.to(circle, {scaleX:stl, scaleY:stl, alpha:0}, 1.5);
t.onCompleteParams = [circle];
t.onComplete = function(c:Sprite):void{removeChild(c)};
t.play();
////////////////mizuiro balls
}
var circles:Array = [];
var circleNum:int = 20;
var i:int = 0;
for (i = 0; i<circleNum; i++){
circles[i] = sim.addCircle({x: 7, y:-2 - i, radius:0.1 + Math.random()*0.4, fillColor:0x00ffff, lineAlpha:0});
}
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
// rotate all boxes
// move circles to top of sceen after they fall off bottom
for (i= 0; i<circleNum; i++){
if (circles[i].y> 20){
circles[i].y = -1;
circles[i].x = Math.random()*(stage.stageWidth / 30 - 11) + 4;
// access to Box2D b2Body methods
circles[i].body.SetLinearVelocity(resetVec);
}
}
}
var resetVec:b2Vec2 = new b2Vec2();
sim.start();
sim.mouseDrag();
//////////////////////////////////////////////////keyboard
var charVel:b2Vec2 = new b2Vec2();
// angular velocity of character
var charVelAng:Number = 1;
addEventListener(Event.ENTER_FRAME, inLoop);
function inLoop(evt:Event):void {
charVel = area.body.GetLinearVelocity();
charVelAng = area.body.GetAngularVelocity();
if (key[Keyboard.RIGHT]){
area.x +=1;
}
if (key[Keyboard.LEFT]){
area.x -=1;
}
if (area.x == 15){
area.x =14;
}
if (area.x == 0){
area.x =1;
}
}
// basic key setup
var key:Object = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
function onKeyPressed(evt:KeyboardEvent):void {
key[evt.keyCode] = true;
key.keyCode = evt.keyCode;
}
function onKeyReleased(evt:KeyboardEvent):void { key[evt.keyCode] = false}
}
}
}