Playing with Box2DFlashAS3

Some time ago I told you to watch Box2DFlashAS3 physics engine.

I dont’ know if you did it, but I did. While the original Box2D project come to 2.0.0 version, the AS3 porting is “still” at 1.4.3, but soon it will be upgraded.

So I think it’s absolutely time to play with this library.

While I won’t be able to provide a full tutorial before some days, I made an interesting prototype in only 58 lines, brackets included.

package {
	import flash.display.*;
	import flash.events.*;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	public class theclass extends Sprite {
		var body_def:b2BodyDef;
		var shape_def:b2BoxDef;
		public var the_world:b2World;
		public function theclass() {
			var container:b2AABB = new b2AABB();
			container.minVertex.Set(0.0, 0.0);
			container.maxVertex.Set(500.0, 400.0);
			the_world = new b2World(container, new b2Vec2(0, 300.0), true);
			body_def = new b2BodyDef();
			shape_def = new b2BoxDef();
			shape_def.extents.Set(250,10);
			shape_def.friction = 1;
			body_def.position.Set(250,390);
			body_def.AddShape(shape_def);
			body_def.userData = new floor();
			body_def.userData.width = 500;
			body_def.userData.height = 20;
			addChild(body_def.userData);
			the_world.CreateBody(body_def);
			addEventListener(Event.ENTER_FRAME, render);
			addEventListener(MouseEvent.CLICK, mouse_handler);
		}
		private function mouse_handler(event:MouseEvent):void {
			body_def = new b2BodyDef();
			shape_def = new b2BoxDef();
			shape_def.extents.Set(Math.floor(Math.random()*15)+15,Math.floor(Math.random()*15)+15);
			shape_def.friction = 1;
			shape_def.density = 1;
			shape_def.restitution = 0.2;
			body_def.position.Set(Math.random()*400+50,0);
			body_def.AddShape(shape_def);
			body_def.userData = new crate();
			body_def.userData.width = shape_def.extents.x * 2;
			body_def.userData.height = shape_def.extents.y * 2;
			addChild(body_def.userData);
			body_def.userData.gotoAndStop(Math.floor(Math.random()*6)+1);
			the_world.CreateBody(body_def);
		}
		public function render(e:Event):void {
			the_world.Step(1/30, 10);
			for (var x:b2Body = the_world.m_bodyList; x; x = x.m_next) {
				if (x.m_userData is Sprite) {
					x.m_userData.x = x.m_position.x;
					x.m_userData.y = x.m_position.y;
					x.m_userData.rotation = x.m_rotation * 57.2957795;
				}
			}
		}
	}
}

And the result is really interesting, and virtually impossible to achieve without the library… just click on the floor and see what happens…

I think it’s time to start learning AS3 with this interesting library… meanwhile you can download the source code with the last version of Box2DFlashAS3 library.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers