Create a Flash game like Roly Poly with Box2D – Step 2

Read all posts about "" game

Welcome to the second step. It’s time to build a level and let the player rotate it with the mouse.

I am going to show you two ways to rotate the level, anyway let’s start fromt the beginning: in the making of step 1 I told you it’s a tile based game, so I am going to use an array to store the tiles.

At the moment each array item can have three values:

0: empty space
1: tile
2: the player

So let me write some more code:

package {
	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.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),false);
		private var worldScale:Number=30;
		private var rolyCenter:b2Vec2=new b2Vec2(320,240);
		private var rolyLevel:b2Body;
		private var rolyRadius:Number=230;
		private var rolyPieces:Number=24;
		private var tileSize:Number=32;
		private var level:Array=new Array();
		private var startAngle:Number;
		private var startRolyAngle:Number;
		public function Main() {
			level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1);
			level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1);
			level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1);
			level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1);
			level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1);
			level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			var centerAngle:Number=2*Math.PI/rolyPieces;
			var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale;
			rolyCenter.Multiply(1/worldScale);
			debugDraw();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.position=rolyCenter;
			rolyLevel=world.CreateBody(bodyDef);
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.restitution=0;
			fixtureDef.friction=1;
			for (var i:Number=0; i

There's nothing interesting in it, apart from level array used to build the level.

This is what we have at the moment:

A static level with a dynamic sphere. We must first make the level dynamic, with a revolute joint to pin it to the stage, following the same concept explained int he post creation of a rotating room with Flash and Box2D, so I would change the script this way:

package {
	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.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),false);
		private var worldScale:Number=30;
		private var rolyCenter:b2Vec2=new b2Vec2(320,240);
		private var rolyLevel:b2Body;
		private var rolyRadius:Number=230;
		private var rolyPieces:Number=24;
		private var tileSize:Number=32;
		private var level:Array=new Array();
		private var startAngle:Number;
		private var startRolyAngle:Number;
		public function Main() {
			level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1);
			level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1);
			level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1);
			level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1);
			level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1);
			level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			var centerAngle:Number=2*Math.PI/rolyPieces;
			var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale;
			rolyCenter.Multiply(1/worldScale);
			debugDraw();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.position=rolyCenter;
			rolyLevel=world.CreateBody(bodyDef);
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.restitution=0;
			fixtureDef.friction=1;
			for (var i:Number=0; i

I used a revolute joint to pin the level to the stage, and made it dynamic. The problem is the level isn't perfectly balanced, so it will start to rotate although there isn't any player interaction:

We need a motor with an high torque to keep the level fixed in its starting position, no matter if it's perfectly balanced, this way:

package {
	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.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),false);
		private var worldScale:Number=30;
		private var rolyCenter:b2Vec2=new b2Vec2(320,240);
		private var rolyLevel:b2Body;
		private var rolyRadius:Number=230;
		private var rolyPieces:Number=24;
		private var tileSize:Number=32;
		private var level:Array=new Array();
		private var startAngle:Number;
		private var startRolyAngle:Number;
		public function Main() {
			level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1);
			level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1);
			level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1);
			level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1);
			level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1);
			level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			var centerAngle:Number=2*Math.PI/rolyPieces;
			var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale;
			rolyCenter.Multiply(1/worldScale);
			debugDraw();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.position=rolyCenter;
			rolyLevel=world.CreateBody(bodyDef);
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.restitution=0;
			fixtureDef.friction=1;
			for (var i:Number=0; i

And now we have our dynamic pinned level pacificly waiting for our interaction:

Now, a simple mouse joint will let us have our first working example of a roly poly level:

package {
	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.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),false);
		private var worldScale:Number=30;
		private var rolyCenter:b2Vec2=new b2Vec2(320,240);
		private var rolyLevel:b2Body;
		private var rolyRadius:Number=230;
		private var rolyPieces:Number=24;
		private var tileSize:Number=32;
		private var level:Array=new Array();
		private var startAngle:Number;
		private var startRolyAngle:Number;
		private var mouseJoint:b2MouseJoint;
		public function Main() {
			level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1);
			level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1);
			level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1);
			level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1);
			level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1);
			level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			var centerAngle:Number=2*Math.PI/rolyPieces;
			var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale;
			rolyCenter.Multiply(1/worldScale);
			debugDraw();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.position=rolyCenter;
			rolyLevel=world.CreateBody(bodyDef);
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.restitution=0;
			fixtureDef.friction=1;
			for (var i:Number=0; i

And that's it:

Click and drag the mouse everywhere to rotate the level.

Since I said I was showing you two ways to rotate the level, what about rotating it according to horizontal mouse position? The more the mouse on the left, the faster the rotation counter-clockwise, the more the mouse on the right, the faster the rotation clockwise, only acting on revolute joint motor speed.

package {
	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.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),false);
		private var worldScale:Number=30;
		private var rolyCenter:b2Vec2=new b2Vec2(320,240);
		private var rolyLevel:b2Body;
		private var rolyRadius:Number=230;
		private var rolyPieces:Number=24;
		private var tileSize:Number=32;
		private var level:Array=new Array();
		private var startAngle:Number;
		private var startRolyAngle:Number;
		private var rolyJoint=b2RevoluteJoint;
		public function Main() {
			level[0]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[1]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[2]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[3]=new Array(1,1,1,1,0,0,0,0,0,0,0,1,1,1,1);
			level[4]=new Array(1,1,1,1,2,0,0,0,0,0,0,0,1,1,1);
			level[5]=new Array(1,1,1,1,1,1,1,1,1,0,0,0,1,1,1);
			level[6]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[7]=new Array(1,1,1,1,1,1,1,1,1,0,0,1,1,1,1);
			level[8]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[9]=new Array(1,1,1,0,0,0,0,0,0,0,0,1,1,1,1);
			level[10]=new Array(1,1,1,0,0,1,1,0,0,1,1,1,1,1,1);
			level[11]=new Array(1,1,1,0,0,1,1,1,1,1,1,1,1,1,1);
			level[12]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[13]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			level[14]=new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);
			var centerAngle:Number=2*Math.PI/rolyPieces;
			var rolySide:Number=rolyRadius*Math.tan(centerAngle/2)/worldScale;
			rolyCenter.Multiply(1/worldScale);
			debugDraw();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.position=rolyCenter;
			rolyLevel=world.CreateBody(bodyDef);
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.restitution=0;
			fixtureDef.friction=1;
			for (var i:Number=0; i

And this is the result:

Move the mouse along its x axis to see the level rotate.

No need to download anything, just copy/paste the code you need into Main class you can find at step 1.

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