Circe Chain engine made with CreateJS

Read all posts about "" game

When I celebrated the 1000th blog post, I told you I would have started to blog about HTML5 game development, and today I want to show you the classic Circle Chain engine made with CreateJS, a suite of Javascript libraries & tools for building rich, interactive experiences with HTML5.

Before I start with this tutorial, remember there’s an iPhone version of Circle Chain available for free in the App Store and I am going to release the Stencyl project for free once it reaches 1,000 downloads (415 to go).

Like all HTML5 projects, this Circle Chain prototype consist in an HTML part…



	
		
		
		
		
		
		
	
	
		
	

and a javascript part, fully commented:

// CreateJS features a Javascript library that lets you manage and co-ordinate
// the loading of assets, so we are going to use it
var preloader;
// manifest array will store every asset we need to preload, just like the
// manifest of a real world ship. As you can see, I am defining each manifest
// item with its relative path and an unique id
var manifest=[
	{src:"images/background.png", id:"background"},
	{src:"images/greencircle.png", id:"greenCircle"}
];
// the stage, the king of every game. 
var stage
// I need an array to store all my green circles, circleBitmap will do it
var circleBitmap = new Array();
// play function is the executed once the page loads
function play(){
	// first, I construct the stage as a Stage instance on the canvas id
	stage = new Stage(document.getElementById("canvas"));
	// then it's time to create the preloader
	preloader = new PreloadJS();
	// loadManifest is the method required to preaload all assets
	preloader.loadManifest(manifest);
	// and onComplete is the event trigger which will call loaded function
	preloader.onComplete = loaded;	
}
// loaded function will place all sprites on the stage and initialize the game
function loaded(event){
	// this is a variable I'll be using to assign a random direction to each
	// green circle
	var randomDirection;
	// look how I am picking up graphic assets from the preloader
	var bgImg=preloader.getResult("background").result;
	// once the background is picked up, I assign it to a Bitmap variable
     var bgBitmap = new Bitmap(bgImg);
     // then it's added to stage with addChild, just like AS3
	stage.addChild(bgBitmap);
	// the same concept is applied to the green circle, 10 times
     var circleImg=preloader.getResult("greenCircle").result;
     for(i=0;i<10;i++){
     	circleBitmap[i]=new Bitmap(circleImg);
     	// look at x and y properties
		circleBitmap[i].x=Math.random()*450+25-9;
		circleBitmap[i].y=Math.random()*450+25-9;
		// choosing a random direction
		randomDirection=Math.random()*2*Math.PI;
		// and defining xSpeed and ySpeed as custom properties
		circleBitmap[i].xSpeed=2*Math.cos(randomDirection);
		circleBitmap[i].ySpeed=2*Math.sin(randomDirection);
		// finally adding green circles to stage
		stage.addChild(circleBitmap[i]);
	}
	// look how I am setting the game to be 30fps!! So easy!!
	Ticker.setFPS(30);
	// and this is something like AS3 enter frame event, calling gameLoop
	// function at every tick
	Ticker.addListener(gameLoop);
}
function gameLoop(){
	// this loop just updates green circles position
	for(i=0;i<10;i++){
		circleBitmap[i].x+=circleBitmap[i].xSpeed;
		circleBitmap[i].y+=circleBitmap[i].ySpeed;
		if(circleBitmap[i].x>500-9){
			circleBitmap[i].x-=500;
		}
		if(circleBitmap[i].x<0-9){
			circleBitmap[i].x+=500;
		}
		if(circleBitmap[i].y>500-9){
			circleBitmap[i].y-=500;
		}
		if(circleBitmap[i].y<0-9){
			circleBitmap[i].y+=500;
		}
	}
	// at the end don't forget to update the stage to show what's going on
	stage.update();
}

as you can see, it's not that different than an AS3 script.

And this is the result:

Next time, I'll show you the complete game.

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

215 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
// Stairs
// 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