s
First of all, you need to start using more locals… this way you avoid memory leaks in your project. For instance, on function prepareHares you could do something like (to avoid creating globals):
local haresPositions = { {'hare4.png', 12, 123}, {'hare1.png', 50, 32}, {'hare2.png', 360, 34}, {'hare3.png', 210, 7}, {'hare4.png', 193, 168}, {'hare5.png', 388, 126}, {'hare6.png', 302, 10}, {'hare.png', 200, 15}, {'hare.png', 200, 15}, {'hare1.png', 50, 32}, {'hare2.png', 360, 34}, } local function prepareHares() hares = display.newGroup() for i = 1, #haresPositions do local hare = display.newImage(haresPositions[1],haresPositions[2],haresPositions[3]) hare.type = "hare" hare:addEventListener('tap', hareHit) hare.isVisible = false hares:insert(hare) end startTimer() end
As for your question of how to speed up the game, there are many ways you could do that, but here is a suggestion.
(note that I dont have your hare images, so I coulnt test it myself… there might be a bug or two here, but you get the idea)
local timerSource local timeToNextHare = 1000 local function showHare() lastHare.isVisible = false local randomHole = math.random(1, #haresPositions) lastHare = hares[randomHole] lastHare:setReferencePoint(display.BottomCenterReferencePoint) lastHare.yScale = 0.1 lastHare.isVisible = true transition.to(lastHare, { time=300, yScale=1 }) end local function startTimer() timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 50, 200) end
I have organized my coding as you showed me and it has given me more simulator errors than before.
I cannot check if your suggestion to speed the game up faster works because it is saying that there is an error on my (preparehares), first of all starting with , local hare = display.newImage(haresPositions[1],haresPositions[2],haresPositions[3]).
if you can guide me and help me through out my code much would be aprreciated, thanks!
try
local hare = display.newImage(haresPositions[1][1],haresPositions[1][2],haresPositions[1][3])
also you can not add them to the group as you are doing try this:
local hares = display.newGroup
local function prepareHares()
for i = 1, 11 do
hare = display.newImage(haresPositions[i][1],haresPositions[i][2],haresPositions[i][3])
hare.type = “hare”
hare:addEventListener(“tap”, hareHit)
hare.isVisible = false
hares:insert(hare)
end
startTimer()
end
@primoz.cerar, Im grateful for the reply, I think that solved that but now it is showing me an error in my start timer function where my
“showHare()” is , why is that?
What error is it showing julianchiu?
It would be nice of you wrote what the error says also but I think your problem is that the showHare function is declared after you use it in the startTimer function. Move the showHare function above the startTimer function you will have the same problem with popOut so put it before showHare. Put hareHit before prepareHares and alert before that. You can make all of these functions local.
Also put all the functions you call in startButtonListeners before it.
@primoz.cerar and @marcior,
I took your suggesestion about rearranging the the functions and it made it work!
But as always another problem always arise after the next.
Everything works smoothly Except for the hare is only popping out from ONE location and NOT like the 11 different locations i had at the start, why is this?and again yourguys help is much appreciated!
Are you using
local hare = display.newImage(haresPositions[1][1],haresPositions[1][2],haresPositions[1][3])
or
local hare = display.newImage(haresPositions[i][1],haresPositions[i][2],haresPositions[i][3])
to create the hares?
The first one would create the problem you are describing… the second should do the trick.
You guys are life savers, that did the trick!
Okay my last question, I promise.
for the startTimer function in the bottom line,
Which is the default time the hares are going to be popping out at and which number is the number that increases the speed?
Thanks!
local function startTimer() timerSource = timer.performWithDelay(timeToNextHare, startTimer) showHare() timeToNextHare = math.max(timeToNextHare - 50, 200) end
The delta for the hares to popup is always the value of “timeToNextHare”
In the code I proposed to you, “timeToNextHare” starts at 1000, but the last line in “startTimer” reduces it by 50 (meaning the second hare shows up 950 after the first, and the third 900 after the second, and so forth).
math.max picks the maximum value of the two numbers. That means that if at any point “timeToNextHare” drops to 200, then the comparison would be math.max(200-50,200), or math.max(150,200), which results in 200. That is: “timeToNextHare” will never drop bellow 200 as it probably would become too fast for any player to follow.
You need to adjust these values to better fit your gameplay (ie. the minimum value for “timeToNextHare”, which is currently 200; and amount by which the time between hares is reduced, which is currently 50)
Alright thanks, makes perfect sense now.
In my previous preparehares function I had types to each hare so later in my developing I would identify them as either good or bad Hares.
function prepareHares()
b1 = display.newImage(‘hare4.png’, 12, 123)
b1.type = “hare”
b2 = display.newImage(‘hare1.png’, 50, 32)
b2.type = “hare”
b3 = display.newImage(‘hare2.png’, 360, 34)
b3.type = “hare”
b4 = display.newImage(‘hare3.png’, 210, 7)
b4.type = “hare”
b5 = display.newImage(‘hare4.png’, 193, 168)
b5.type = “hare”
b6 = display.newImage(‘hare5.png’, 388, 126)
b6.type = “hare”
b7 = display.newImage(‘hare6.png’, 302, 10)
b7.type = “hare”
b8 = display.newImage(‘hare.png’, 200, 15)
b8.type = “hare”
b9 = display.newImage(‘hare.png’, 200, 15)
b9.type = “hare”
b10 = display.newImage(‘hare1.png’, 50, 32)
b10.type = “badhare”
b11 = display.newImage(‘hare2.png’, 360, 34)
b11.type = “badhare”
hares = display.newGroup(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11)
Is there anyway I can add types to the local hares positions you have given me?
Yeah, sure…
you can do it by making the code look like this:
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"}, {'hare1.png', 50, 32, "badhare"}, {'hare2.png', 360, 34, "badhare"}, } local function prepareHares() for i = 1, 11 do 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
What if i want to incorporate a loss of life for everytime a hare that pops out isnt hit, would i need to adjust my hareHit function or where would i start?
I think the best way here would be inside your function “showHare”…
Before you make lastHare.isVisible = false, you can check if lastHare.isVisible is “true” (because if it is 'false" it means that the user hit it)
First you would need to set a timer after which you hide the hare and take alife if it wasn’t hit. Remebmer to save the handle to the timer so you can cancel it if it does get hit. If timer elapses do the same thing you do if the user hits a bad hare.
Or do it when you show the next hare as marcior suggested if you only plan to have one hare visible at a time. Otherwise you will have to change the lastHare to a table of visible hares anyway.
okay so here is my hareHit function:
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 == "badhare" then lifeIcons[lives].isVisible = false lives = lives - 1 if( lives == 0) then alert() end end end
Couldn’t I use the same function as “badhare” but if its not hit, lives = lives - 1?
How would I incorporate a “notHit” function or such into the HareHit()?
Not sure what you mean… hareHit is ONLY called if the hare is hit right? so by definition a hare that is notHit wouldn’t call this function…
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?