Noob Level 10000 question

Hi friends. Im completely new to Lua. I used to program back in the day with C++ but its been a while. Please don’t mind the absolute stupid questions below and in the future.

I went through the first chapter and got the simple balloon thing working. Great. Now, I wanted to add a bit more to that like a “Loss counter”

The idea is that every time the balloon hits the platform the tapCounter resets and the losCounter goes up.

Can someone help? I tried this code in red below but im pretty sure im not referencing the right properties.

local tapCount = 0 local losCount = 0 local background = display.newImageRect("background.png", 360, 570) background.x = display.contentCenterX background.y = display.contentCenterY local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local winText = display.newText(winCount, 30, 20, native.systemFont, 40 ) winText:setFillColor( 0, 255, 0 ) local losText = display.newText(winCount, 290, 20, native.systemFont, 40 ) losText:setFillColor( 255, 0, 0 ) local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-25 local balloon = display.newImageRect( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.alpha = 0.8 local physics = require( "physics" ) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=55, bounce=0.3 } ) local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount if ( balloon.y == platform.y ) then losText = losText + 1 tapCount = 0 end end balloon:addEventListener( "tap", pushBalloon )
if ( balloon.y == platform.y ) then     losText = losText + 1     losCount = losCount + 1     losText.text = tostring(losCount)     tapCount = 0 end

You and jdsmedeirosbr both have an error in your code that will crash the game if the condition is met. Also, you can improve the logic a bit, for instance:

local thresholdOffset = balloon.height\*0.5 - 10 local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) if ( balloon.y \<= platform.y - thresholdOffset) then tapCount = 0 losCount = losCount + 1 losText.text = losCount end tapCount = tapCount + 1 tapText.text = tapCount end

Since losText is a display object, i.e. a table, setting it to losText = losText + 1 should crash the game if the condition is ever met.

Now, the reason why I say “if the condition is ever met” is because what you are looking for now is if the balloon and the platform are on exactly the same y coordinates. If one is at 320 and the other one is at 320.1, then they aren’t the same. So in order to address that, you’d need to use <= or >=, depending on your needs.

Secondly, I don’t think that even with <= or >= you can get it to work since it would require the balloon to be inside the platform for them to have the same y position. So, you need to offset the threshold a bit.

Finally, it might be better to have a check like that occur inside an enterFrame listener (http://docs.coronalabs.com/api/event/enterFrame/index.html) so that the text and counters are updated as soon as that threshold is hit instead of only after the balloon is tapped.

Also, it’s generally a better practice to require libraries and modules like physics at the start of your files.

Thank you so much for the guidance.

I took a crack at it and made a bit of progress. Now i just have to figure out what makes the event stop. The losCounter increments indefinitely at this point because the event is triggering all the time.

What can I do to have it exit that trigger?

Thanks again in advance. Very helpful :) 

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------- -- variables ----------------------------------------------------------------------------------------- local tapCount = 0 local losCount = 0 ----------------------------------------------------------------------------------------- -- requires ----------------------------------------------------------------------------------------- local physics = require( "physics" ) ----------------------------------------------------------------------------------------- -- display properties ----------------------------------------------------------------------------------------- local background = display.newImageRect("background.png", 360, 570) background.x = display.contentCenterX background.y = display.contentCenterY local tapText = display.newText(tapCount, display.contentCenterX, 60, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local losText = display.newText(losCount, display.contentCenterX, 100, native.systemFont, 40 ) losText:setFillColor( 255, 0, 0 ) local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-25 local balloon = display.newImageRect( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.alpha = 0.8 ----------------------------------------------------------------------------------------- -- logic ----------------------------------------------------------------------------------------- physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=55, bounce=0.3 } ) local thresholdOffset = platform.height+35 local myListener = function( event ) if ( balloon.y \>= platform.y - thresholdOffset) then tapCount = 0 tapText.text = tapCount losCount = losCount + 1 losText.text = losCount end end Runtime:addEventListener( "enterFrame", myListener ) local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount end balloon:addEventListener( "tap", pushBalloon )

Well, first of all, you’ve mixed up the parameters for the two functions.

local function pushBalloon( event ) -- You'll want "event" here. local myListener = function() -- "event" does nothing here.

Also, it’s generally safer to use touch events instead of tap events because tap events easily turn to touches if you move your finger even a pixel while touching the screen. See https://docs.coronalabs.com/api/event/touch/index.html.

And all you need to do to stop it from triggering endlessly is to add :removeEventListener() inside the condition. Once it has triggered, you can stop monitoring for it. Now, if you need to re-enable it, you could do add something like this inside your pushBalloon function:

if not myListenerRunning then myListenerRunning = true Runtime:addEventListener( "enterFrame", myListener ) end

And just declare  myListenerRunning before the two functions. Then as you remove the listener, just set myListenerRunning back to false.

Thank you for your help. I think I understand. I think the “physics” portion of it is what is messing things up. The balloon “bounces” when it hits the platform so in essence its counting multiple times. I guess ill have to use another counter or something to account for that and stop the main count when the first increment happens.

Btw the touch event was blasting the balloon off the screen so I reverted back to tap.

I’m still learning so it will take some time before I can figure this out correctly. 

Im attaching the project here so please feel free to suggest fixes and/or suggestions