I've started a new project (but don't worry, I haven't abandoned Everwar). I've just purchased the Corona SDK, which lets you quickly and easily write mobile applications for Android, iPhone/iPod Touch, and iPad. Corona uses the language
Lua as a base, which it then compiles to run on any of the devices. I'm a big fan of the "write once, run anywhere" approach.
Lua is a high performance interpreted language used in many games already. I believe it's usually used for AI in a game, but Corona has extended it to a complete solution. I've never used Lua before, and I've toyed with the idea of writing mobile games, so this seems like an excellent learning opportunity.
This series of posts will be about the lessons I learn writing in Lua for the first time, as it relates to using the Corona SDK. The project? A mobile version of Zombie Slaughter.
First StabIn just under an hour, I was able to figure out how to draw a background, animate a single zombie, move that zombie, and then create lots more zombies. The code has been surprisingly straightforward. The only surprise I had was scaling the background to fit whatever screen resolution the device offered. The SDK api documentation suggests to me that if you write:
Quoted Text
local background = display.newImage( "background.jpg", true )
it should auto scale it to match the screen resolution. It didn't. Perhaps it's just because the Game edition of the SDK is still in Alpha, but all it took was doing the scaling manually.
Quoted Text
background:scale( display.contentWidth/background.stageWidth, display.contentHeight/background.stageHeight )
So here's the entire code for my first Mobile Zombie Slaughter demo:
Quoted Text
-- Zombies Mobile Take 1
require "sprite"
display.setStatusBar( display.HiddenStatusBar)
math.randomseed(os.time())
-- load the background
local background = display.newImage( "background.jpg")
background:scale( display.contentWidth/background.stageWidth, display.contentHeight/background.stageHeight )
-- reposition background since scaling puts origin in middle of image
background.x = background.stageWidth/2
background.y = background.stageHeight/2
-- load the zombie
local zombieSheet = sprite.newSpriteSheet( "zombie_anim.png", 32, 38 )
local zombieAnimation = sprite.newSpriteSet( zombieSheet, 1, 4 ) -- startFrame 1, 4 frames
sprite.add( zombieAnimation, "zombie", 1, 4, 1000, 0 ) -- start frame 1, 4 frames every 1000 ms, loop forever
-- init the zombie pool
local zombiePool = {}
local function addZombieToPool()
local zombie = sprite.newSprite( zombieAnimation )
zombie.x = display.contentWidth + 32
zombie.y = math.random (32, display.contentHeight - 32)
zombie.dx = 0.02
zombie:prepare("zombie")
zombie:play()
zombiePool[#zombiePool+1] = zombie
end
-- a per-frame event to move the zombies
local tPrevious = system.getTimer()
local maxZombies = 500
local zombieIncrement = 5000
local lastZombieAdded = tPrevious
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
local xOffset = ( 0.2 * tDelta )
-- iterate through each zombie and move it
local i
for i = 1, #zombiePool, 1 do
zombiePool[i].x = zombiePool[i].x - zombiePool[i].dx * tDelta
if (zombiePool[i].x + zombiePool[i].stageWidth) < 0 then
zombiePool[i].x = display.contentWidth + 32
end
end
-- see if it's time to add a new Zombie
if #zombiePool < maxZombies then
if (event.time - lastZombieAdded) > zombieIncrement then
addZombieToPool()
lastZombieAdded = event.time
end
end
end
-- Seed the pool with a starter zombie
addZombieToPool()
-- kick it all off
Runtime:addEventListener( "enterFrame", move );