Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

Basic Circle Chain engine using Corona SDK

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:

1
2
3
4
5
6
7
8
9
application =
{
	content =
	{
		width = 320,
		height = 480,
		scale = "letterbox"
	},
}

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

1
2
3
4
5
6
7
settings =
{
	orientation =
	{
		default = "landscapeRight",
	},
}

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

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
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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 2.80 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. Orlando

    on February 25, 2012 at 2:25 am

    Emanuele, I need your advice please. Which software is more convenience for developing games and interactive ebooks for IOS? I have a dilemma on what software to use: Corona or iStencyl.

    Thanks in advance for your answer

  2. Bloodnovski

    on February 29, 2012 at 3:14 pm

    No offense, but this blog is getting kinda boring :( .
    I’m really thankful to all your tutorials as i learned as3 here, got many game ideas here, and now all posts i see seem to be done in an hour.
    Don’t want to whine but i would love to see more inspiring posts here :)

    Keep up the good work
    Bloodnovski

  3. Emanuele Feronato

    on February 29, 2012 at 5:11 pm

    @Orlando: still testing, today I published something with StencylWorks, have a look at it

    @Bloodnovski: thank you for your feedback, during the last time I am blogging about “quick tricks” to port AS3 games to mobile devices because there are a lot of developers with a big AS3 games portfolio trying to easily convert them for mobile devices.
    Obviously if you aren’t interested in it you may find it boring, but there’s a lot more on the horizon, included (but not limited to) a realistic way to break objects after a collision with Box2D. This will allow you to have stuff which breaks in the realistic way if it falls on the ground. Does it sound more interesting to you?

  4. Bloodnovski

    on March 7, 2012 at 10:36 pm

    @Emanuele Feronato

    Now i feel bad because i commented. I know it is hard to run this good blog, but i just expressed my opinion, which doesn’t necessarily have to be right.
    Thanks for the new posts(which are always welcome) though. Thanks for the time you took to respond.
    Cheers

  5. Emanuele Feronato

    on March 8, 2012 at 11:09 am

    You’re welcome, feedback is always appreciated and a criticism is way more useful than silence.