Corona – Sprites Simple Game

Here’s an example that builds off the last post about creating an explosion sprite. In this example we’ll create some alien sprites that move down the screen. When touched these aliens explode.

The explosion sprite sheet and code was taken from this post: Corona – Sprite Explosion.

For this example I used this sprite sheet for the alien: 

A simple game using Sprites

From here I needed to make a sprite. I followed the same basic outline used when I created the explosion. in this case I won’t be animating the alien. Instead I’ll use the sprite sheet to show any one of the 25 aliens. Here’s the code I sued to set up the alien sprite:

local alien_sheet = sprite.newSpriteSheet( "Alien_32.png", 32, 32 )
local alien_set = sprite.newSpriteSet( alien_sheet, 1, 25 )
sprite.add( alien_set, "alien", 1, 25, 1000, 0 )

The code above creates alien sprites 32 by 32 pixels. There are 25 images in the sprite sheet.

Next we need a function to create aliens.

local function make_alien()
	local alien = sprite.newSprite( alien_set )
	alien.currentFrame = math.random( 1, 25 )
	alien.x = math.random( 32, 288 )
	alien.y = -math.random( 40, 140 )
	local transition_time = math.random( 5000, 10000 )
	alien.transition = transition.to( alien, {y=end_y, time=transition_time, onComplete=alien_complete} )
	alien:addEventListener( "touch", remove_alien )
end

Next I needed a factory function to create aliens. This function creates an alien sprite, positions it above the top of the screen and set it to a random frame 1 of 25.

Next I added a transition to move the sprite down the screen. I assigned the transition to the sprite itself. in the property transition. The idea is that the sprite will keep track of it’s transition. If the transition needs to be canceled we’ll have a reference. Tapping a sprite will remove the sprite in which case we’ll want to remove the transition also.

The transition also has an onComplete. If this is called we know the alien made it off the bottom of the screen.

Notice the y value in the transition. There is a variable here end_y. I define this above as:

local end_y = display.contentHeight + 40

This defines position 40 pixels past the bottom of the screen.

I also added a touch listener to the alien. Tapping the alien will remove it and add an explosion. in the case of a touch we’ll also need to remove the transition and remove the touch event listener.

Transition Complete

This function handles removing the alien when the transition completes:

local function alien_complete( target ) 
	local alien = target
	alien:removeEventListener( "touch", remove_alien )
	alien:removeSelf()
end

Here we: remove the touch event listener and then remove the alien.

Handling touches

I used the following function to handle touch events.

local function remove_alien( event ) 
	local phase = event.phase 
	if phase == "began" then 
		local alien = event.target
		local explosion =  make_explosion()
		explosion.x = alien.x
		explosion.y = alien.y
		alien:removeEventListener( "touch", remove_alien )
		transition.cancel( alien.transition )
		alien:removeSelf()
	end 
end

First we check for the pause “began”. Then get a reference to the alien. Next make a new explosion, by calling the factory function. This function returns a reference to the explosion object. Save this to a local variable. Then position the explosion at the position of the alien. Last, cancel the transition on the alien, remove it’s touch event and last remove the alien.

Using time.performWithDelay() to make sprites

The last step is to use the timer to create aliens. I used the following code to call on my alien factory function to make an alien once per second.

local timer = timer.performWithDelay( 1000, make_alien, -1 )

Complete source code for the example

You will need to download the two images used in the example or make your own images.

-- Hide status bar
display.setStatusBar( display.HiddenStatusBar )

-- Import Sprite
require( "sprite" )

-----------------------------------------------------------------------------------------
-- Set up a sprite  sheet for the alien. 
-----------------------------------------------------------------------------------------
local explosion_sheet = sprite.newSpriteSheet( "explosion_43FR.png", 93, 100 ) 
local explosion_set = sprite.newSpriteSet( explosion_sheet, 1, 40)
sprite.add( explosion_set, "explosion", 1, 40, 30, 1 )

local alien_sheet = sprite.newSpriteSheet( "Alien_32.png", 32, 32 )
local alien_set = sprite.newSpriteSet( alien_sheet, 1, 25 )
sprite.add( alien_set, "alien", 1, 25, 1000, 0 )
-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------
-- These functions acts as a factory to make explosion sprites and remove them.
-----------------------------------------------------------------------------------------
-- This function removes the explosion
local function remove_explosion( event )
	local phase = event.phase
	if phase == "loop" then 
		local explosion = event.target
		explosion:removeEventListener( "sprite", remove_explosion )
		explosion:removeSelf()
	end 
end 

-- This function makes a new explosion.
local function make_explosion()
	local explosion = sprite.newSprite( explosion_set )
	explosion:prepare()
	explosion:play()
	explosion:addEventListener( "sprite", remove_explosion )

	return explosion
end
-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------
-- Make aliens
-----------------------------------------------------------------------------------------
local end_y = display.contentHeight + 40

local function remove_alien( event ) 
	local phase = event.phase 
	if phase == "began" then 
		local alien = event.target
		local explosion =  make_explosion()
		explosion.x = alien.x
		explosion.y = alien.y
		alien:removeEventListener( "touch", remove_alien )
		transition.cancel( alien.transition )
		alien:removeSelf()
	end 
end 

local function alien_complete( target ) 
	local alien = target
	alien:removeEventListener( "touch", remove_alien )
	alien:removeSelf()
end 

local function make_alien()
	local alien = sprite.newSprite( alien_set )
	alien.currentFrame = math.random( 1, 25 )
	alien.x = math.random( 32, 288 )
	alien.y = -math.random( 40, 140 )
	local transition_time = math.random( 5000, 10000 )
	alien.transition = transition.to( alien, {y=end_y, time=transition_time, onComplete=alien_complete} )
	alien:addEventListener( "touch", remove_alien )
end 

local timer = timer.performWithDelay( 1000, make_alien, -1 )
-----------------------------------------------------------------------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *