Hey everybody, I am currently working on a Whack a mole based concept game and I have all of the basics down and the game works well so far. However I only have one group of moles that when tapped they give you a point and your score updates and so on. The problem I am having is that i want to add another different group of moles that do another set of functions like when they are tapped a point is taken away. I tried to add another group and give them functions like my first group of moles but I just keep on getting an error when i try to play it in the corona simulator. I know there is an easier and better way of grouping all of the different kinds of moles with different functions but i cannot put my finger on it. If you think you can help me feel free to comment, i will gladly appreciate it !
display.setStatusBar(display.HiddenStatusBar)
local bg = display.newImage(‘gameBg.png’)
local titleBg
local playBtn
local creditsBtn
local titleView
local creditsView
local score
local m1
local m2
local m3
local m4
local m5
local m6
local m7
local m8
local moles
local lastMole = {}
local hit = audio.loadSound(‘hit.wav’)
local timerSource
local currentMoles = 0
local molesHit = 0
local totalMoles = 10
local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local prepareMoles = {}
local startTimer = {}
local showMole = {}
local popOut = {}
local moleHit = {}
local alert = {}
function Main()
titleBg = display.newImage(‘titleBg.png’)
playBtn = display.newImage(‘playBtn.png’, display.contentCenterX - 25.5, display.contentCenterY + 40)
creditsBtn = display.newImage(‘creditsBtn.png’, display.contentCenterX - 40.5, display.contentCenterY + 85)
titleView = display.newGroup(titleBg, playBtn, creditsBtn)
startButtonListeners(‘add’)
end
function startButtonListeners(action)
if(action == ‘add’) then
playBtn:addEventListener(‘tap’, showGameView)
creditsBtn:addEventListener(‘tap’, showCredits)
else
playBtn:removeEventListener(‘tap’, showGameView)
creditsBtn:removeEventListener(‘tap’, showCredits)
end
end
function showCredits:tap(e)
playBtn.isVisible = false
creditsBtn.isVisible = false
creditsView = display.newImage(‘creditsView.png’)
transition.from(creditsView, {time = 300, x = -creditsView.width, onComplete = function() creditsView:addEventListener(‘tap’, hideCredits) creditsView.x = creditsView.x - 0.5 end})
end
function hideCredits:tap(e)
playBtn.isVisible = true
creditsBtn.isVisible = true
transition.to(creditsView, {time = 300, x = -creditsView.width, onComplete = function() creditsView:removeEventListener(‘tap’, hideCredits) display.remove(creditsView) creditsView = nil end})
end
function showGameView:tap(e)
transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners(‘rmv’) display.remove(titleView) titleView = nil end})
score = display.newText(‘0’ … ‘/’ … totalMoles, 58, 6, native.systemFontBold, 16)
score:setTextColor(238, 238, 238)
prepareMoles()
end
function prepareMoles()
m1 = display.newImage(‘mole.png’, 80.5, 11)
m2 = display.newImage(‘mole.png’, 198.5, 51)
m3 = display.newImage(‘mole.png’, 338.5, 34)
m4 = display.newImage(‘mole.png’, 70.5, 110)
m5 = display.newImage(‘mole.png’, 225.5, 136)
m6 = display.newImage(‘mole.png’, 376.5, 96)
m7 = display.newImage(‘mole.png’, 142.5, 211)
m8 = display.newImage(‘mole.png’, 356.5, 186)
moles = display.newGroup(m1, m2, m3, m4, m5, m6, m7, m8)
for i = 1, mole.numChildren do
moles[i]:addEventListener(‘tap’, moleHit)
moles[i].isVisible = false
end
startTimer()
end
function startTimer()
timerSource = timer.performWithDelay(1400, showMole, 0)
end
function showMole(e)
if(currentMoles == totalMoles) then
alert()
else
lastMole.isVisible = false
local randomHole = math.floor(math.random() * 8) + 1
lastMole = moles[randomHole]
lastMole:setReferencePoint(display.BottomCenterReferencePoint)
lastMole.yScale = 0.1
lastMole.isVisible = true
Runtime:addEventListener(‘enterFrame’, popOut)
currentMoles = currentMoles + 1
end
end
function popOut(e)
lastMole.yScale = lastMole.yScale + 0.2
if(lastMole.yScale >= 1) then
Runtime:removeEventListener(‘enterFrame’, popOut)
end
end
function moleHit:tap(e)
audio.play(hit)
molesHit = molesHit + 1
score.text = molesHit … ‘/’ … totalMoles
lastMole.isVisible = false
end
function alert()
timer.cancel(timerSource)
lastMole.isVisible = false
local alert = display.newImage(‘alertBg.png’)
alert:setReferencePoint(display.CenterReferencePoint)
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3})
local score = display.newText(haresHit … ‘/’ … totalHares, 220, 190, native.systemFontBold, 20)
score:setTextColor(204, 152, 102)
end
Main()