// forked from kkstudio2007's forked from: forked from: flash on 2010-5-16
// forked from dec's forked from: flash on 2010-5-16
// forked from dec's flash on 2010-5-16
package {
import flash.display.*;
import flash.events.*;
import com.actionsnippet.qbox.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Common.Math.b2Vec2;
import flash.text.TextField;
public class FlashTest extends MovieClip {
public var timer:Number=0;
public var mouseClicked:Boolean=false;
public var middle:QuickObject;
public var lJoint:QuickObject;
public var rJoint:QuickObject;
public var dropArea:QuickObject;
public var releasebox:QuickObject;
public var contacts:QuickContacts;
public var prizes:Array=[];
public var horizSpeed:Number=0, vertSpeed:Number=0;
private var state:String = "Left";//"Down", "Up" "Right"
private var catchcnt:int = 1;
private var score:int = 0;
public function FlashTest() {
inittrace(stage);
// write as3 code here..
var sim:QuickBox2D=new QuickBox2D(this);
sim.setDefault({fillColor:0xff00000});
sim.createStageWalls();
releasebox=sim.addBox({x:13.6,y:0,width:.2,height:3.5,density:0});
sim.addBox({x:14.83,y:2.94,width:.2,height:3.5,angle:-45*(Math.PI/180),density:0});
sim.addBox({x:11.5,y:13,width:.2,height:10,density:0});
dropArea=sim.addBox({x:13.3,y:15,width:3.3,height:.3,fillColor:0xFFFF00,draggable:false});
sim.setDefault({fillColor:0x00FFFF});
sim.addCircle({x:2,y:2,radius:.5});
sim.addCircle({x:5,y:3,radius:.9});
for(var b:int=0;b<40;b++){
if(b%2==0){
prizes[b]=sim.addCircle({x:4,y:2,radius:.5,draggable:false});
}
else{
prizes[b]=sim.addBox({x:4,y:2,radius:.6,draggable:false});
}
}
middle=sim.addBox({x:10.55,y:4,width:1,height:1,density:10,fixedRotation:true,draggable:false});
var lClaw:QuickObject=sim.addPoly({x:10,y:5,points:[0,0 , -1,1 , 0,2 , -0.5,1 , 0.5,.05],wireframe:false,draggable:false});
var rClaw:QuickObject=sim.addPoly({x:11.1,y:5,points:[0,0 , 1,1 , 0,2 , .5,1 , -0.5,.05],wireframe:false,draggable:false});
lJoint=sim.addJoint({type:"revolute",a:lClaw.body,b:middle.body,x1:lClaw.x,y1:lClaw.y,x2:middle.x-1,y2:middle.y-1,enableMotor:true,maxMotorTorque:250,enableLimit:true,lowerAngle:-180*(Math.PI/180),upperAngle:10*(Math.PI/180)});
rJoint=sim.addJoint({type:"revolute",a:rClaw.body,b:middle.body,x1:rClaw.x,y1:rClaw.y,x2:middle.x+1,y2:middle.y-1,enableMotor:true,maxMotorTorque:250,enableLimit:true,lowerAngle:-10*(Math.PI/180),upperAngle:180*(Math.PI/180)});
contacts=sim.addContactListener();
contacts.addEventListener(QuickContacts.ADD,onAdd);
sim.start();
sim.mouseDrag();
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpEvent);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownEvent);
stage.addEventListener(Event.ENTER_FRAME,loop1);
}
private function loop1(event:Event):void{
var speed:Number=5;
var j:b2RevoluteJoint=lJoint.joint as b2RevoluteJoint;
if(mouseClicked&&(state=="Left"||state=="Right")&&catchcnt>0){
vertSpeed=5;
j.SetMotorSpeed(-speed);
j=rJoint.joint as b2RevoluteJoint;
j.SetMotorSpeed(speed);
if(state=="Left")
state="Right"; WTrace.wtrace(state);
}
else{
vertSpeed=-3;
j.SetMotorSpeed(speed);
j=rJoint.joint as b2RevoluteJoint;
j.SetMotorSpeed(-speed);
}
if(mouseX>300&&state=="Right"){
horizSpeed=3;
}
else if(mouseX<150&&state=="Left"){
horizSpeed=-3;
}
else{
horizSpeed=0;
}
middle.body.SetLinearVelocity(new b2Vec2(horizSpeed,vertSpeed));
middle.body.SetAngularVelocity(0);
}
private function mouseUpEvent(event:MouseEvent):void{
mouseClicked=false;
catchcnt--;WTrace.wtrace(catchcnt);
}
private function mouseDownEvent(event:MouseEvent):void{
mouseClicked=true;
}
private function onAdd(event:Event):void{
for(var i:int=0;i<prizes.length;i++){
var prize:QuickObject=prizes[i];
if(contacts.isCurrentContact(prize,dropArea)){
prize.destroy();
score++;WTrace.wtrace(score);
}
}
if(contacts.isCurrentContact(middle,releasebox))
{
if(state=="Right")
state="Left"; WTrace.wtrace(state);
if(catchcnt<=0)
catchcnt=1; WTrace.wtrace(catchcnt);
}
}
}
}
//WONDERFL TRACE
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
function inittrace(s:Stage):void
{
WTrace.initTrace(s);
}
//global trace function
var trace:Function;
//wtreace class
class WTrace
{
private static var FONT:String = "Fixedsys";
private static var SIZE:Number = 12;
private static var TextFields:Array = [];
private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void
{
trace_stage = stg;
trace = wtrace;
}
private static function scrollup():void
{
// maximum number of lines: 100
if (TextFields.length > 100)
{
var removeme:TextField = TextFields.shift();
trace_stage.removeChild(removeme);
removeme = null;
}
for(var x:Number=0;x<TextFields.length;x++)
{
(TextFields[x] as TextField).y -= SIZE*1.2;
}
}
public static function wtrace(... args):void
{
var s:String="";
var tracefield:TextField;
for (var i:int;i < args.length;i++)
{
// imitating flash:
// putting a space between the parameters
if (i != 0) s+=" ";
s+=args[i].toString();
}
tracefield= new TextField();
tracefield.autoSize = "left";
tracefield.text = s;
tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE);
tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);
scrollup();
TextFields.push(tracefield);
}
}