Basic Circle Chain engine using Corona SDK

Read all posts about "" game

As you know, I am porting my Circle Chain game to iPhone, and I am playing with various tools to create iPhone games in the painless way.

After the Adobe Air version, today I will show you how to create the basic engine (circles moving in a random direction) using Corona SDK.

I would suggest you to read my post develop iPhone games with Corona SDK – handling normal and retina display to have some basics about the files you need to create in order to configure your game.

This is how I define content size and scale, in config.lua file:

application =
{
	content =
	{
		width = 320,
		height = 480,
		scale = "letterbox"
	},
}

And this is how I define the game orientation, in build.settings file:

settings =
{
	orientation =
	{
		default = "landscapeRight",
	},
}

Now, the interesting part, in main.lua, which I am commenting and explaining what changed from AS3 version:

display.setStatusBar(display.HiddenStatusBar)
local background = display.newImage("background.png")
local mobs = display.newGroup()
math.randomseed(os.time())
for i = 1, 10 do
	 local greenCircle = display.newImage("greencircle.png")
	 greenCircle.x = math.random(0,479)
	 greenCircle.y = math.random(0,319)
	 local randomDirection = math.rad(math.random(0,359))
	 greenCircle.xSpeed = 2*math.cos(randomDirection)
	 greenCircle.ySpeed = 2*math.sin(randomDirection)
	 mobs.insert(mobs,greenCircle)
end

local function update(e)
	for i = 1, mobs.numChildren do
		mobs[i].x = mobs[i].x + mobs[i].xSpeed
		mobs[i].y = mobs[i].y + mobs[i].ySpeed
		if(mobs[i].x<0) then
			mobs[i].x = mobs[i].x + 480
		end
		if(mobs[i].x>480) then
			mobs[i].x = mobs[i].x - 480
		end
		if(mobs[i].y<0) then
			mobs[i].y = mobs[i].y + 320
		end
		if(mobs[i].y>320) then
			mobs[i].y = mobs[i].y - 320
		end
	end	
end

Runtime:addEventListener("enterFrame",update)

Line 1: removes the iPhone’s status bar.

Line 2: that’s how I create and add to the “stage” the background image.

Line 3: I am creating a group of “Display Objects” called mobs, getting something between a Vector and a Display Object Container.

Line 4: Like in the good old times, I have to define a seed for all random number generation, to prevent the system to generate always the same sequence.

Line 5: yes, this is a for loop.

Line 6: again, I create and add to the “stage” a new “Display Object” using the green circle image.

Lines 7-8: placing the circle in a random position acting on x and y properties. math.random() will generate a random number between 0 and 1, while math.random(x,y) will generate a random integer between x and y.

Line 9: generating a random direction, in degrees, then converting it to radians.

Lines 10-11: determining the horizontal and vertical speed using trigonometry, and saving them as properties.

Line 12: inserting the green circle mob into mobs display group

Lines 15-32: function to be executed at every frame, explained later

Line 34: this is the declaration of the enter frame event listener, and it must be placed after the callback function, otherwise it won’t work.

Now let’s see update function:

Line 16: scanning through mobs children, starting from one

Lines 17-18: updating each circle position according to its x and y speed, and no, you can’t use +=

Lines 19-30: handling circles when they fly out of the stage.

This is the result:

The video may seem laggy but the engine on the simulator is working smoothly and perfectly.

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