Removing score text and rects and preventing "spam touch"

So I have come across two problems and I’ll try to be as thorough as possible when explaining.

1st) So I use the score text from this tutorial

https://docs.coronalabs.com/tutorial/games/keepScores/index.html

and while it does work it doesn’t come with a command to remove the text when reaching the game over state. 

local scoreText = score.init( { fontSize = 20, font = "CoolCustomFont.ttf", x = display.contentCenterX, y = 30, maxDigits = 7, leadingZeros = true })

Also how would you remove a rect? Because the rects I do have stay until I restart the corona app.

2nd) My app has this function 

local function touchScreen(event) if event.phase == "began" then animation.enterFrame = activateAnimations Runtime:addEventListener("enterFrame", animation) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", animation) end end 

This causes my character to move with every left click, but if I hold left click the character will fly off screen. How would I work around this?

And here’s visual evidence

https://www.youtube.com/watch?v=32eVQ5FqOa0&feature=youtu.be

To remove any display object:

[lua]

display.remove(theObject)

theObject = nil

[/lua]

In the case of your scoreText object, the actual display object to remove would be scoreText.scoreText. 

Difficult to help with question two without seeing the code that is taking place in your ‘animation’ function.

Do you want the movement function to fire more than once?

If yes, then you could write some conditions for it, e.g. only move the character up if the character is below a certain Y position, otherwise don’t do anything.

If not, then you could simply insert some check to the beginning of the function, such as “if tapped == false then”, if that statement is true, then you’d run the function but immediately set “tapped = true” below the check. Then, you’d have to monitor for the touch event’s end. Once the touch event ends, you’d simply set tapped back to false, thus preventing the function from running more than once per tap.

ah sorry forgot that one

local function activateAnimations(event) if( isValidPhysics( animation )) then animation:applyForce(0, -45, animation.x, animation.y) end end

Essentially, but i would like it to move based on the player’s taps. It would be kind of like this: the player taps, then the character moves up and slowly falls if not being tapped,  and if you were to hold the tap then it’d only move up once as if it had been tapped once and it would fall unless tapped again.

I want to eliminate the ability to be able to hold left click and cause continuous jumps.

In that case just implement what I suggested. :stuck_out_tongue:

I came up with this 

local function activateAnimations(event) if( isValidPhysics( animation )) then animation:applyForce(0, -45, animation.x, animation.y) end if animation.y \< 400 then animation:applyForce(0, 30, animation.x, animation.y) end end

But I get this error “game.lua:94: attempt to compare nil with number”

line 94 is

 if animation.y \< 400 then

For the most part, the error messages are really straightforward. “Attempt to compare nil with number” simply means that the variable “animation” does not exist or it is outside of the scope of the function.

So, when you are trying to see if “animation’s y position is less than 400”, the game crashes because you cannot compare something that doesn’t exist with a number.

Now, why is “animation” nil? Well, that’s something for you to figure out. Perhaps you delete it at some point and then try to run the function again? A simple fix for this is to not run the function if it doesn’t exist, or include an extra check in the function for the same thing.

So if I understand you correctly, when something is nil, in the program that means it doesn’t exist?

Well in your professional opinion should I try to fix my error with the code I have or try a different approach?

And is there a list of what makes things nil, so I can determine what makes my animation nil?

To quote myself:
 

So, when something is nil, it means that it either doesn’t exist or it is outside of the scope of the function. :stuck_out_tongue:

In my opinion, you have to always fix any and all errors in your code AND add safety measures to prevent your code from crashing. For instance,
 

if animation then -- proceed only if animation is not nil or false if animation.y then -- proceed only if animation.y exists (but this line is probably not even needed) if animation.y \< 400 then animation:applyForce(0, 30, animation.x, animation.y) end end end

Whenever you include checks like these, they simply prevent the code from reaching a point where it would crash because some necessary variable would be nil or have some undesired value.

Why does something become nil? Again, they may be out of scope, or you’ve set them to nil, or you’ve removed them, etc. I would recommend that you peruse these tutorials ( https://docs.coronalabs.com/tutorial/index.html ). There’s one titled “Scope for Beginners” that should help you to better understand scope.

Should you try alternative approach? It’s up to you. This thread is starting to remind me of your previous thread concerning score counters and dodges, and I feel like most of what I say is either ignored or misunderstood. :smiley: I would wholeheartedly recommend that you study the basics of Lua and Corona more. There are some great tutorials available through the link (especially the “Getting Started Guide”). You will also develop much faster as a programmer and a game developer if you try to figure things out for yourself instead of defaulting to ask help on the forums. I know it’s frustrating when you hit roadblocks, but overcoming them is an important part of the learning process.

Definitely misunderstood but I get you now, and I’m going to start working on a fix. Thanks for the help.

To remove any display object:

[lua]

display.remove(theObject)

theObject = nil

[/lua]

In the case of your scoreText object, the actual display object to remove would be scoreText.scoreText. 

Difficult to help with question two without seeing the code that is taking place in your ‘animation’ function.

Do you want the movement function to fire more than once?

If yes, then you could write some conditions for it, e.g. only move the character up if the character is below a certain Y position, otherwise don’t do anything.

If not, then you could simply insert some check to the beginning of the function, such as “if tapped == false then”, if that statement is true, then you’d run the function but immediately set “tapped = true” below the check. Then, you’d have to monitor for the touch event’s end. Once the touch event ends, you’d simply set tapped back to false, thus preventing the function from running more than once per tap.

ah sorry forgot that one

local function activateAnimations(event) if( isValidPhysics( animation )) then animation:applyForce(0, -45, animation.x, animation.y) end end

Essentially, but i would like it to move based on the player’s taps. It would be kind of like this: the player taps, then the character moves up and slowly falls if not being tapped,  and if you were to hold the tap then it’d only move up once as if it had been tapped once and it would fall unless tapped again.

I want to eliminate the ability to be able to hold left click and cause continuous jumps.

In that case just implement what I suggested. :stuck_out_tongue:

I came up with this 

local function activateAnimations(event) if( isValidPhysics( animation )) then animation:applyForce(0, -45, animation.x, animation.y) end if animation.y \< 400 then animation:applyForce(0, 30, animation.x, animation.y) end end

But I get this error “game.lua:94: attempt to compare nil with number”

line 94 is

 if animation.y \< 400 then

For the most part, the error messages are really straightforward. “Attempt to compare nil with number” simply means that the variable “animation” does not exist or it is outside of the scope of the function.

So, when you are trying to see if “animation’s y position is less than 400”, the game crashes because you cannot compare something that doesn’t exist with a number.

Now, why is “animation” nil? Well, that’s something for you to figure out. Perhaps you delete it at some point and then try to run the function again? A simple fix for this is to not run the function if it doesn’t exist, or include an extra check in the function for the same thing.

So if I understand you correctly, when something is nil, in the program that means it doesn’t exist?

Well in your professional opinion should I try to fix my error with the code I have or try a different approach?

And is there a list of what makes things nil, so I can determine what makes my animation nil?

To quote myself:
 

So, when something is nil, it means that it either doesn’t exist or it is outside of the scope of the function. :stuck_out_tongue:

In my opinion, you have to always fix any and all errors in your code AND add safety measures to prevent your code from crashing. For instance,
 

if animation then -- proceed only if animation is not nil or false if animation.y then -- proceed only if animation.y exists (but this line is probably not even needed) if animation.y \< 400 then animation:applyForce(0, 30, animation.x, animation.y) end end end

Whenever you include checks like these, they simply prevent the code from reaching a point where it would crash because some necessary variable would be nil or have some undesired value.

Why does something become nil? Again, they may be out of scope, or you’ve set them to nil, or you’ve removed them, etc. I would recommend that you peruse these tutorials ( https://docs.coronalabs.com/tutorial/index.html ). There’s one titled “Scope for Beginners” that should help you to better understand scope.

Should you try alternative approach? It’s up to you. This thread is starting to remind me of your previous thread concerning score counters and dodges, and I feel like most of what I say is either ignored or misunderstood. :smiley: I would wholeheartedly recommend that you study the basics of Lua and Corona more. There are some great tutorials available through the link (especially the “Getting Started Guide”). You will also develop much faster as a programmer and a game developer if you try to figure things out for yourself instead of defaulting to ask help on the forums. I know it’s frustrating when you hit roadblocks, but overcoming them is an important part of the learning process.

Definitely misunderstood but I get you now, and I’m going to start working on a fix. Thanks for the help.