Snippets and notes about actionscript.

Box2dFlash: Basic movement

Posted: December 16th, 2008 | Author: | Filed under: Flash, Notes | Tags: , , , | No Comments »

Here is a quick example of getting a ball to roll around and hop up. It is a modification of the HelloWorld.as example that comes with Box2dFlash. Use the arrow keys to move around.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package{
	// Flash imports
	import flash.display.Sprite;
	import flash.events.*;
	// Box2D 2.0.1 imports
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
 
	public class Main extends Sprite{
 
		// Global variables
		public var m_world:b2World;
		public var m_iterations:int = 10;
		public var m_timeStep:Number = 1.0/30.0;
 
		public var moveLeft:Boolean;
		public var moveRight:Boolean;
		public var moveUp:Boolean;
 
		public var body:b2Body;
		public var playerBody:b2Body;
		public var bodyDef:b2BodyDef;
		public var boxDef:b2PolygonDef;
		public var circleDef:b2CircleDef;
 
 
		public function Main(){
 
			// Initiate variables
			moveLeft = false;
			moveRight = false;
			moveUp = false;
 
			// Add keyboard listener
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
 
			// Add event for main loop
			stage.addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
 
			// Creat world AABB
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-100.0, -100.0);
			worldAABB.upperBound.Set(100.0, 100.0);
 
			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
 
			// Allow bodies to sleep
			var doSleep:Boolean = true;
 
			// Construct a world object
			m_world = new b2World(worldAABB, gravity, doSleep);
 
 
			// set debug draw
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			var dbgSprite:Sprite = new Sprite();
			addChild(dbgSprite);
			dbgDraw.m_sprite = dbgSprite;
			dbgDraw.m_drawScale = 30.0;
			dbgDraw.m_fillAlpha = 0.0;
			dbgDraw.m_lineThickness = 1.0;
			dbgDraw.m_drawFlags = 0xFFFFFFFF;
			m_world.SetDebugDraw(dbgDraw);
 
 
			// Add ground body
			bodyDef = new b2BodyDef();
			bodyDef.position.Set(10, 12);
			boxDef = new b2PolygonDef();
			boxDef.SetAsBox(30, 3);
			boxDef.friction = 0.3;
			boxDef.density = 0; // static bodies require zero density
			// Add sprite to body userData
			var bodySprite:Sprite = new Sprite();
			bodySprite.graphics.beginFill(0x000000, 1);
			bodySprite.graphics.drawRect(-900, -90, 1800, 180); // Offset sprite to be at center
			bodySprite.graphics.endFill();
			bodyDef.userData = bodySprite; // Add sprite to body
			bodyDef.userData.width = 30 * 2 * 30;
			bodyDef.userData.height = 30 * 2 * 3;
			addChild(bodyDef.userData);
			body = m_world.CreateBody(bodyDef);
			body.CreateShape(boxDef);
			body.SetMassFromShapes();
 
			// Add circle
			bodyDef = new b2BodyDef(); // We reuse the same bodyDef to construct next body
			bodyDef.position.x = 5;
			bodyDef.position.y = 0;
			//bodyDef.isBullet = true; // Constantly check collisions, expensive but accurate
			circleDef = new b2CircleDef();
			circleDef.radius = 1;
			circleDef.density = 0.5;
			circleDef.friction = 0.5;
			circleDef.restitution = 0.1;
			var circleSprite:Sprite = new Sprite();
			circleSprite.graphics.beginFill(0xFF0000, 0.5);
			circleSprite.graphics.drawCircle(0, 0, 10);
			circleSprite.graphics.endFill();
			bodyDef.userData = circleSprite;
			bodyDef.userData.width = 1 * 2 * 30;
			bodyDef.userData.height = 1 * 2 * 30;
			playerBody = m_world.CreateBody(bodyDef); // playerBody is what we'll use to reference circle
			playerBody.CreateShape(circleDef);
			playerBody.SetMassFromShapes();
			addChild(bodyDef.userData); // Add sprite to stage
 
		}
 
		public function Update(e:Event):void {
 
			// Movement
			if( moveLeft ) { // Apply negative horizontal force on circle's center
				playerBody.WakeUp();
				if( body.IsSleeping()==false ) {
					playerBody.ApplyForce(new b2Vec2(-10,0), playerBody.GetWorldCenter());
				}
			}
			else if( moveRight ) {
				playerBody.WakeUp();
				if( playerBody.IsSleeping()==false ) {
					playerBody.ApplyForce(new b2Vec2(10,0), playerBody.GetWorldCenter());
				}
			}
			if( moveUp ) { // Apple negative verticale force on circle's center
				playerBody.WakeUp();
				if( playerBody.IsSleeping()==false ) {
					playerBody.ApplyForce(new b2Vec2(0,-20), playerBody.GetWorldCenter());
				}
			}
 
			m_world.Step(m_timeStep, m_iterations);
			// Go through body list and update sprite positions/rotations
			for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
				if (bb.m_userData is Sprite) {
					bb.m_userData.x = bb.GetPosition().x * 30;
					bb.m_userData.y = bb.GetPosition().y * 30;
					bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
				}
			}
		}
 
		public function keyDownHandler(ke:KeyboardEvent):void {
			if( ke.keyCode==37 ) { // Left Arrow
				moveLeft = true;
			}
			if( ke.keyCode==39 ) { // Right Arrow
				moveRight = true;
			}
			if( ke.keyCode==38 ) { // Up Arrow
				moveUp = true;
			}
		}
 
		public function keyUpHandler(ke:KeyboardEvent):void {
			if( ke.keyCode==37 ) { // Left Arrow
				moveLeft = false;
			}
			if( ke.keyCode==39 ) { // Right Arrow
				moveRight = false;
			}
			if( ke.keyCode==38 ) { // Up Arrow
				moveUp = false;
			}
		}
	}
}


Leave a Reply