All objects appear in whack a mole clone issue.

Based off of whack a worm source code at mobile tuts. Looked at the code. Menu works with the sliding credits, but when I press play, all the hares show up.

Took a look at the popping function and the timer.

function startTimer()  
 timerSource = timer.performWithDelay(1400, showHare, 0)  
end   
  
function showHare(e)  
 if(currentHares == totalHares) then   
 alert()  
 else  
 lastHare.isVisible = false   
 local randomHole = math.floor(math.random() \* 8) + 1  
  
 lastHare = hares[randomHole]  
 lastHare:setReferencePoint(display.BottomCenterReferencePoint)  
 lastHare.yScale = 0.1  
 lastHare.isVisible = true  
  
 Runtime:addEventListener('enterFrame', popOut)  
  
 currentHares = currentHares + 1  
 end   
end  
  
function popOut(e)  
 lastHare.yScale = lastHare.yScale + 0.2  
  
 if(lastHare.yScale \>= 1) then  
 Runtime:removeEventListener('enterFrame', popOut)  
 end  
end  
  
function hareHit:tap(e)  
 audio.play(hit)  
 haresHit = haresHit + 1  
 score.text = haresHit .. '/' .. totalHares  
 lastHare.isVisible = false  
end  

This is what the main looks like with the movieclip added in to the source.

display.setStatusBar(display.HiddenStatusBar)  
  
local movieclip = require('movieclip')  
local bg = display.newImage('gameBg.png')  
  
local titleBg  
local playBtn  
local creditsBtn   
local titleView  
local creditsView  
local score  
local h1  
local h2  
local h3  
local h4  
local h5  
local h6  
local h7  
local h8  
  
local hares  
local lastHare = {}  
  
local hit = audio.loadSound('hit.wav')  
local timerSource   
local currentHares = 0  
local haresHit = 0   
local totalHares = 10  
local Main = {}  
local startButtonListeners = {}  
local showCredits = {}  
local hideCredits = {}  
local showGameView = {}  
local prepareHares = {}  
local startTimer = {}  
local showHare = {}  
local popOut = {}  
local hareHit = {}  
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' .. '/' .. totalHares, 58, 6, native.systemFontBold, 16)  
 score:setTextColor(238, 238, 238)  
 prepareHares()  
end  
  
function prepareHares()  
 h1 = display.newImage('hare.png', 80.5, 11)  
 h2 = display.newImage('hare.png', 198.5, 51)  
 h3 = display.newImage('hare.png', 338.5, 34)  
 h4 = display.newImage('hare.png', 70.5, 110)  
 h5 = display.newImage('hare.png', 225.5, 136)  
 h6 = display.newImage('hare.png', 376.5, 96)  
 h7 = display.newImage('hare.png', 142.5, 211)  
 h8 = display.newImage('hare.png', 356.5, 186)  
  
 hares = display.newGroup(h1, h2, h3, h4, h5, h6, h7, h8)  
  
 for i = 1, hare.numChildren do   
 hares[i]:addEventListener('tap', hareHit)  
 hares[i].isVisible = false   
 end  
  
 startTimer()  
end  
  
function startTimer()  
 timerSource = timer.performWithDelay(1400, showHare, 0)  
end   
  
function showHare(e)  
 if(currentHares == totalHares) then   
 alert()  
 else  
 lastHare.isVisible = false   
 local randomHole = math.floor(math.random() \* 8) + 1  
  
 lastHare = hares[randomHole]  
 lastHare:setReferencePoint(display.BottomCenterReferencePoint)  
 lastHare.yScale = 0.1  
 lastHare.isVisible = true  
  
 Runtime:addEventListener('enterFrame', popOut)  
  
 currentHares = currentHares + 1  
 end   
end  
  
function popOut(e)  
 lastHare.yScale = lastHare.yScale + 0.2  
  
 if(lastHare.yScale \>= 1) then  
 Runtime:removeEventListener('enterFrame', popOut)  
 end  
end  
  
function hareHit:tap(e)  
 audio.play(hit)  
 haresHit = haresHit + 1  
 score.text = haresHit .. '/' .. totalHares  
 lastHare.isVisible = false  
end  
  
function alert()  
 timer.cancel(timerSource)  
 lastHare.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()  

What am I doing wrong? Whats preventing the game from starting and why are all the objects shown?

[import]uid: 128294 topic_id: 22460 reply_id: 322460[/import]

My gut reaction (without being able to test as I’m at work), is that:

[lua]hares = display.newGroup(h1, h2, h3, h4, h5, h6, h7, h8)[/lua]

is not a correct method of adding objects to a display group (I could well be wrong but I’ve never seen it done that way before).

Therefore the loop to hide them all and the code to have them pop up would do nothing. For what it’s worth here’s how I would have setup the hares:

[lua]local hareGroup = display.newGroup()
local hares = {}

function prepareHares()

local hareX = {80.5, 198.5, 338.5, 70.5, 225.5, 376.5, 142.5,356.5}
local hareY = {11, 51, 34, 110, 136, 96, 211, 186}

for a = 1, 8, 1 do

local hare = display.newImageRect(“hare.png”, 75, 75) – substitute for correct dimensions of your image
hare.x = hareX[a]; hare.y = hareY[a]
hare:addEventListener(“tap”, hareHit)
hare.isVisible = false
hare.id = a
hareGroup:insert(hare)

hares[a] = hare

end

startTimer()
end [/lua] [import]uid: 93133 topic_id: 22460 reply_id: 89596[/import]

@nick_sherman

With locals always going on top right? So would it look like this?

local score  
  
local hareGroup = display.newGroup()  
local hares = {}  
  
local hares  
local lastHare = {}  
  
local hit = audio.loadSound('hit.wav')  
local timerSource   
local currentHares = 0  
local haresHit = 0   
local totalHares = 10  
local Main = {}  
local startButtonListeners = {}  
local showCredits = {}  
local hideCredits = {}  
local showGameView = {}  
local prepareHares = {}  
local startTimer = {}  
local showHare = {}  
local popOut = {}  
local hareHit = {}  
local alert = {}  
  
function prepareHares()  
   
 local hareX = {80.5, 198.5, 338.5, 70.5, 225.5, 376.5, 142.5,356.5}  
 local hareY = {11, 51, 34, 110, 136, 96, 211, 186}  
   
 for a = 1, 8, 1 do  
   
 local hare = display.newImageRect("hare.png", 75, 75) -- substitute for correct dimensions of your image  
 hare.x = hareX[a]; hare.y = hareY[a]  
 hare:addEventListener("tap", hareHit)  
 hare.isVisible = false  
 hare.id = a  
 hareGroup:insert(hare)  
   
 hares[a] = hare  
   
 end  
   
 startTimer()   
end   
  
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  
  

[import]uid: 128294 topic_id: 22460 reply_id: 89598[/import]

Yup, that’s right, although you need to take out the ‘local hares’ after ‘local hares = {}’.

Also, your ‘lastHare’ variable should not be set up as an array, as it will just contain a pointer to one object at a time. [import]uid: 93133 topic_id: 22460 reply_id: 89600[/import]

@nick_sherman
What do I add then? [import]uid: 128294 topic_id: 22460 reply_id: 89719[/import]

Works now. However, don’t know how to code retry, or back to main menu. How is that done? [import]uid: 128294 topic_id: 22460 reply_id: 90127[/import]

LakeView Hotel, would you mind sharing how you got the codes to work ? I have the same concept as you game wise but i cannot get mine to work in the format you have. thanks alot i appreciate it .

LakeView Hotel, would you mind sharing how you got the codes to work ? I have the same concept as you game wise but i cannot get mine to work in the format you have. thanks alot i appreciate it .