Yes sorry you are correct, so is there any function I can call that would take a life away if it is NOT hit when the hare pops out?
As marcior suggested if you have only one hare visible at a time you can check it before you show the next hare in you showHare function:
function showHare(e) if lastHare.isVisible then lifeIcons[lives].isVisible = false lives = lives - 1 if( lives == 0) then alert() end end lastHare.isVisible = false local randomHole = math.random(1, 11) lastHare = hares[randomHole] lastHare:setReferencePoint(display.BottomCenterReferencePoint) lastHare.yScale = 0.1 lastHare.isVisible = true Runtime:addEventListener('enterFrame', popOut) end
@primoz.cerar, yeah thanks your suggestions helped!
Do you think it is possible to have “waves” or “rounds” in my game. For example, wave 1 starts off with 20 hares to advance and so on, thanks guys, much help!
Well yes all you need to do is to handle how and when you call the timer for the next hare. I can’t really give you code snippet as it is very dependent on your code and how you want the waves to be. Play with it a little I’m sure you will get it. Clue: When you want a break between waves just put a longer delay in the performWithDelay function.
Here is my showHare() and start timer function, would you suggest a timer in the “showHares function” that when reached a “totalHares” it finishes a wave?
local function showHare() if lastHare.isVisible then lifeIcons[lives].isVisible = false lives = lives - 1 if( lives == 0) then alert() end end lastHare.isVisible = false local randomHole = math.floor(math.random() \* 11) + 1 lastHare = hares[randomHole] lastHare:setReferencePoint(display.BottomCenterReferencePoint) lastHare.yScale = 0.1 lastHare.isVisible = true transition.to(lastHare, { time=300, yScale=1 }) end function startTimer() timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 30, 600) end
a timer in showHare won’t stop the timer you started in startTimerFunction.
You should be checking in starTimer whether you need to make a pause.
local wave = 1 local waveTable = {10, 15, 12, 14} local waveHares = 0 function startTimer() if waveHares == waveTable[wave] then wave = wave + 1 waveHares = 0 timerSource = timer.performWithDelay(timeToNextWave, startTimer) return end timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 30, 600) end
Okay cool, makes sense, your a big help.
To fully comprehend “local waveTable” represents the first wave ending at 10, the second at 15, etc?
Correct
@primoz.cerar, I want to display the number of wave the player is currently on, I did this by adding " wave.text = wave" under “waveHares = 0” in my function startTimer(). The text appears how ever it seems like the waveTable is not updating with the text because it just stays at “0”. Am i missing something here ?
function startTimer() if waveHares == waveTable[wave] then wave = wave + 1 waveHares = 0 wave.text = wave timerSource = timer.performWithDelay(timeToNextWave, startTimer) return end timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 30, 600) end
Well this should be giving you an error as wave is a number and you’re trying to set the text property of number.
You need to set the text of newText object you created for displaying the wave number which I don’t know how you named as it’s not declared in the code you posted.
My mistake , I forgot to mention I did exactly that in my gameview function(), which my “score” works perfectly fine in when a harehit but does not work for my wave, why is this? Thanks!
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' , 397, 9, native.systemFontBold, 25) score:setTextColor(255, 200, 0) wave = display.newText('0' , 300, 5, native.systemFontBold, 25) wave:setTextColor(255, 200, 0) prepareHares() end
function hareHit:tap(e) audio.play(hit) lastHare.isVisible = false if t.type == "hare" then haresHit = haresHit + 1 local t = e.target hareHit = display.newText('+1000', 380, 45, native.systemFontBold, 18) hareHit:setTextColor(255, 0, 0) transition.to( hareHit, { time=800, alpha=.01 } ) score.text = haresHit elseif t.type == "badhare" then lifeIcons[lives].isVisible = false lives = lives - 1 if( lives == 0) then alert() end end end
Well you can not have two things with the same name it works in hareHit because youre setting the text of the score variable and you have not named anything else score. But if you check your code you will see before the startTimer function I gave a declatazion local wave = 1 (or 0 don’t remeber which) which is the current wave number then you’re setting a text to a variable with the same name.
Make you wave text named waveText for example and set waveText.text = tostring(wave).
Yes, the local wave = 1, however the number of wave displayed on the screen stays at 0, so its between two things my waveTable or my waveText.text = tostring(wave) right?
function startTimer() if waveHares == waveTable[wave] then wave = wave + 1 waveHares = 0 waveText.text = waveTable timerSource = timer.performWithDelay(timeToNextWave, startTimer) return end timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 30, 600) 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' , 397, 9, native.systemFontBold, 25) score:setTextColor(255, 200, 0) waveText = display.newText('0' , 300, 5, native.systemFontBold, 25) waveText:setTextColor(255, 200, 0) prepareHares() end
local waveText = 1 local waveTable = {5, 10, 15, 20} local waveHares = 0
You are not listening
local waveText = 1
waveText = display.newText(‘0’ , 300, 5, native.systemFontBold, 25)
The second one will override the first one because you setting to the same variable.
local wave = 1 – create variable for wave number
local waveText – just a forward declaration so you have it in scope. it will hold the text display object
in showGameView:
waveText = display.newText(tostring(wave), 300, 5, native.systemFontBold, 25) – create the text display object
then when you want to update it:
wave = wave + 1 – increase the wave number
waveText.text = tostring(wave) – set the text of the text display object to the wave number
Okay this time I am sure everything is correct but the “1” does not update with the waveTable since it just stays at “1”, would you mind taking a look at the whole code, thank you.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Hide Status Bar display.setStatusBar(display.HiddenStatusBar) -- Background local bg = display.newImage('gameBg.png') local icon = display.newImage('lifeicon1.png', 3, 3) local icon2 = display.newImage('lifeicon2.png', 352, 3) -- Title View local titleBg local playBtn local creditsBtn local titleView -- Credits View local creditsView -- Score local score -- hare local hareGroup = display.newGroup() local hares = {} local lasthare = {} -- Sound local hit = audio.loadSound('hit.wav') local wave = 1 local waveText local waveTable = {1, 2, 3, 4} local wavehares = 0 -- Variables local currenthares = 0 local haresHit = 0 local timerSource local timeToNexthare = 1000 -- Functions local Main = {} local startButtonListeners = {} local showCredits = {} local hideCredits = {} local showGameView = {} local preparehares = {} local startTimer = {} local showhare = {} local popOut = {} local hareHit = {} local alert = {} local lifeIcons = {} local lives = 3 local maxLives = 3 local i for i = 1, maxLives do lifeIcons[i] = display.newImage("lifeicon.png") lifeIcons[i].x = 57 + (lifeIcons[i].contentWidth \* (i - 1)) lifeIcons[i].y = 23 end local haresPositions = { {'hare4.png', 12, 123, "hare"}, {'hare1.png', 50, 32, "hare"}, {'hare2.png', 360, 34, "hare"}, {'hare3.png', 210, 7, "hare"}, {'hare4.png', 193, 168, "hare"}, {'hare5.png', 388, 126, "hare"}, {'hare6.png', 302, 10, "hare"}, {'hare.png', 200, 15, "hare"}, {'hare.png', 200, 15, "hare"}, {'obama1.png', 50, 32, "hare"}, {'obama2.png', 360, 34, "hare"}, } function hareHit:tap(e) audio.play(hit) lasthare.isVisible = false local t = e.target if t.type == "hare" then haresHit = haresHit + 1 hareHit = display.newText('+1000', 380, 45, native.systemFontBold, 18) hareHit:setTextColor(255, 0, 0) transition.to( hareHit, { time=800, alpha=.01 } ) score.text = haresHit elseif t.type == "obama" then lifeIcons[lives].isVisible = false lives = lives - 1 if( lives == 0) then alert() end end end local function preparehares() hares = display.newGroup() for i = 1, #haresPositions do local hare = display.newImage(haresPositions[i][1],haresPositions[i][2],haresPositions[i][3]) hare.type = haresPositions[i][4] hare:addEventListener('tap', hareHit) hare.isVisible = false hares:insert(hare) end startTimer() end function popOut(e) lasthare.yScale = lasthare.yScale + 0.2 if(lasthare.yScale \>=1) then Runtime:removeEventListener('enterFrame', popOut) end end local function showhare() lasthare.isVisible = false local randomHole = math.floor(math.random() \* 11) + 1 lasthare = hares[randomHole] lasthare:setReferencePoint(display.BottomCenterReferencePoint) lasthare.yScale = 0.1 lasthare.isVisible = true transition.to(lasthare, { time=300, yScale=1 }) end function startTimer() if wavehares == waveTable[wave] then wave = wave + 1 waveText.text = tostring(wave) wavehares = 0 timerSource = timer.performWithDelay(timeToNextWave, startTimer) return end timerSource = timer.performWithDelay(timeToNexthare, startTimer) showhare() timeToNexthare = math.max(timeToNexthare - 30, 600) 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, 220, 190, native.systemFontBold, 18) score:setTextColor(204, 152, 102) end -- Main Function function Main() titleBg = display.newImage('titleBg.png') playBtn = display.newImage('playBtn.png', display.contentCenterX + 135.5, display.contentCenterY + 20) creditsBtn = display.newImage('creditsBtn.png', display.contentCenterX - 190.5, display.contentCenterY + 20) 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' , 397, 9, native.systemFontBold, 25) score:setTextColor(255, 200, 0) waveText = display.newText(tostring(wave), 300, 5, native.systemFontBold, 25) waveText:setTextColor(255, 200, 0) preparehares() end Main ()
You are not increasing your waveHares counter. Try this:
function startTimer() if wavehares == waveTable[wave] then wave = wave + 1 waveText.text = tostring(wave) wavehares = 0 timerSource = timer.performWithDelay(timeToNextWave, startTimer) return end timerSource = timer.performWithDelay(timeToNexthare, startTimer) showhare() wavehares = wavehares + 1 timeToNexthare = math.max(timeToNexthare - 30, 600) end
Thanks for your input!
This unfortunately did not work either, I think the problem lies with the “waveText.text = tostring(wave)” or the waveTable which is probably isn’t being read by the code which does not update the wave round. Any other ideas? I’ll keep trying & let you know what i come up with , thanks!
where you have:
waveText.text = tostring(wave)
add infront a print for wave an waveText.text
Without the counter increase I told you about in the last post that code wouldn’t even execute.
So put this infront of waveText.text = tostring(wave)
print(tostring(wave))
print(waveText.text)
Then we will see what you get if you get anything.