Game Development Reference
In-Depth Information
A mouse joint allows a player to move bodies with the mouse, and we will create it
with the following features:
• Pick a body by clicking on it
• Move a body following the mouse as long as the button is pressed
• Release a body once the button is released
Some quiet before the storm; the beginning of this process does not differ to other
scripts you have already mastered through the topic.
1.
So, we are importing the required classes:
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
2.
Then we need the class-level variables for the world itself, and the conversion
from pixels to meters:
private var world:b2World;
private var worldScale:Number=30;
And even the Main function has nothing new, as it just places two box shapes
on the stage: the big static ground and a smaller dynamic box on it.
public function Main() {
world=new b2World(new b2Vec2(0,9.81),true);
debugDraw();
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,470/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
var groundBody:b2Body=world.CreateBody(bodyDef);
groundBody.CreateFixture(fixtureDef);
bodyDef.position.Set(320/worldScale,430/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
polygonShape.SetAsBox(30/worldScale,30/worldScale);
fixtureDef.density=1;
fixtureDef.friction=0.5;
fixtureDef.restitution=0.2;
 
Search WWH ::




Custom Search