Hi guys, im quite new to corona and lua. Im working on one simple side scrolling game and still learning. Ive encountered some problems with my scoring and timer module.
- Whenever the character hits the obstacles, the score will add 1. But the scores wont be replaced but it will shows a new score under the old score so you can see there multiple numbers on a single point.
- My timer wont refresh everytime the game is loaded.
Ive been looking on it for days but still cant figure out why it just wont work out. Can you guys help me to figure this out and explain to me where my mistakes are? Below is my codes for the game. Your help is greatly appreciated!
– requires
local physics = require “physics”
physics.start()
local de = require “de”
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
– preload audio
local sndKill = audio.loadSound(“boing-1.wav”)
local sndBlast = audio.loadSound ( “blast.mp3” )
local sndLose = audio.loadSound ( “wahwahwah.mp3” )
–declare your object before using it, it is a good habit
local ceiling
local thefloor
local background
local jet
local tmr
score = 1
local one
local two
local obstacles
local obstacles2
local obstacles3
local obstacles4
– background
function scene:createScene(event) – Managing game elements
local screenGroup = self.view
background = display.newImage(“space.png”)
background.y = 300
background.x = 260
background.speed = 3
screenGroup:insert(background)
jet = display.newImage(“beetleship.png”)
jet.type = “jet”
jet.x = 100
jet.y = 100
local r = math.random(50,400)/100
jet:scale(r,r)
physics.addBody(jet,“dynamic”, {density=0.1, bounce=0.1, friction=0.2, radius=12})
screenGroup:insert(jet)
local a = math.random(50,700)/100
obstacles = display.newCircle( 80, 120, 10 ) --green circle is in the middle
obstacles:setFillColor( 0, 255,255)
obstacles.type = “collect”
obstacles.x = 250
obstacles.y = 100
obstacles.speed = math.random(1,3)
obstacles:scale(a,a)
obstacles.initY = obstacles.y
obstacles.amp = math.random(20,100)
obstacles.angle = math.random(1,360)
physics.addBody(obstacles,“static”, {density=0.1, bounce=0.1, friction=0.2, radius=12})
screenGroup:insert(obstacles)
obstacles2 = display.newCircle( 80, 120, 10 ) --green circle is in the middle
obstacles2:setFillColor( 0 ,255,255)
obstacles2.type = “collect”
obstacles2.x = 250
obstacles2.y = 100
obstacles2.speed = math.random(1,3)
obstacles2:scale(a,a)
obstacles2.initY = obstacles2.y
obstacles2.amp = math.random(20,100)
obstacles2.angle = math.random(1,360)
physics.addBody(obstacles2,“static”, {density=0.1, bounce=0.1, friction=0.2, radius=12})
screenGroup:insert(obstacles2)
obstacles3 = display.newCircle( 80, 120, 10 ) --green circle is in the middle
obstacles3:setFillColor( 0, 255,255)
obstacles3.type = “collect”
obstacles3.x = 250
obstacles3.y = 100
obstacles3.speed = math.random(2,6)
obstacles3:scale(a,a)
obstacles3.initY = obstacles3.y
obstacles3.amp = math.random(20,100)
obstacles3.angle = math.random(1,360)
physics.addBody(obstacles3,“static”, {density=0.1, bounce=0.1, friction=0.2, radius=12})
screenGroup:insert(obstacles3)
obstacles4 = display.newCircle( 80, 120, 10 ) --green circle is in the middle
obstacles4:setFillColor( 255,255,0)
obstacles4.type = “avoid”
obstacles4.x = 250
obstacles4.y = 100
obstacles4.speed = math.random(2,6)
obstacles4:scale(a,a)
obstacles4.initY = obstacles4.y
obstacles4.amp = math.random(20,100)
obstacles4.angle = math.random(1,360)
physics.addBody(obstacles4,“static”, {density=0.1, bounce=0.1, friction=0.2, radius=12})
screenGroup:insert(obstacles4)
end
function scrollSpace(self, event) – Making the background to move dynamically
if self.x < -350 then
self.x = 900
else
self.x = self.x-self.speed
end
end
function moverocks(self, event) – Controls the obstacles movements
print(self.x)
if self.x < 0 then – -60
self.x = 500
self.y = math.random(90,220)
self.speed = math.random(2,6)
self.amp = math.random(20,100)
self.angle = math.random(1,360)
else
self.x = self.x-self.speed
self.angle = self.angle + .1
self.y = self.amp*3*math.sin(self.angle)+self.initY
end
end
function activateJets(self, event) – Activates the jet
self:applyForce(0, -1.5, self.x, self.y)
end
function touchScreen(event) – When the screen is touched, activates event
print(“touch”)
if event.phase == “began” then
print(“began”)
jet.enterFrame = activateJets
Runtime:addEventListener(“enterFrame”, jet)
–audio.play(sndBlast)
end
if event.phase == “ended” then
Runtime:removeEventListener(“enterFrame”, jet)
print(“ended”)
end
end
function onCollision(event) – Event when the jet hits the obstacles
if event.phase == “began” then
print(“collide!”)
local score1 = display.newText(score, 0 , 0 , native.systemFont, 32)
score1:setTextColor(255, 255, 255)
score1.x = 200
score1.y = -20
addSetText(score1)
one = event.object1
two = event.object2
if one.type == “jet” and two.type == “collect” then
score = score+1
print(score)
elseif one.type == “jet” and two.type == “avoid” then
score = 0
storyboard.gotoScene(“restart”, “fade”, 400)
timer.cancel(tmr)
tmr = nil
end
end
–audio.play(sndkill)
end
– Create a method to add the setText method to text objects
function addSetText(obj)
–Create a custom method for setting text
function obj:setText(txt,align)
local a = align or display.CenterReferencePoint
local oldX = self.x
local oldY = self.y
self.text = txt
self.x = oldX
self.y = oldY
end
end
function scene:enterScene(event) – Event occurs when the game is played
Runtime:addEventListener(“touch”, touchScreen)
background.enterFrame = scrollSpace
Runtime:addEventListener(“enterFrame”, background)
obstacles.enterFrame = moverocks
Runtime:addEventListener(“enterFrame”, obstacles)
obstacles2.enterFrame = moverocks
Runtime:addEventListener(“enterFrame”, obstacles2)
obstacles3.enterFrame = moverocks
Runtime:addEventListener(“enterFrame”, obstacles3)
obstacles4.enterFrame = moverocks
Runtime:addEventListener(“enterFrame”, obstacles4)
Runtime:addEventListener(“collision”, onCollision)
local scoreText = display.newText("Score ", 0, 0, native.systemFont, 19)
scoreText.x = 190
scoreText.y = 8.5
local output = display.newText(“0”, 0 , 0 , native.systemFont, 32)
output:setTextColor(255, 255, 255)
output.x = 300
output.y = -20
local stateText = display.newText(“Started”, 0, 0, native.systemFont, 17)
stateText:setTextColor(255, 255, 255)
stateText.x = 290
stateText.Y = -20
–Add the setText method to the two text objects
addSetText(output)
addSetText(stateText)
tmr = timer.performWithDelay(1000, function(e)
output:setText(e.count) --setText is a function to show the counter on the screen
if(e.count == 30) then
stateText:setText(“Finished”)
if tmr then
timer.cancel(tmr)
tmr = nil
end
storyboard.gotoScene(“restart”, “fade”, 400)
stateText = nil
end
end, 30)
end
function scene:exitScene(event) – Remove EventListener
–[[if(tmr==nil) then
timer.cancel(tmr)
end]]–
obstacles.enterFrame=nil
obstacles2.enterFrame=nil
obstacles3.enterFrame=nil
obstacles4.enterFrame=nil
background.enterFrame=nil
Runtime:removeEventListener(“collision”, onCollision)
Runtime:removeEventListener(“touch”, touchScreen)
Runtime:removeEventListener(“enterFrame”, background)
Runtime:removeEventListener(“enterFrame”, spaceship)
end
function scene:destroyScene(event) – Destroy the scene
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene