How to remove a table or array

First you declare a variable like this that can hold the score:

local score = 0

Then you can make a function that increase and decrease the value of the score like this:

local scoreFunc = function(scoreValue)

  score = score + scoreValue

  return score

end

What you should consider is to maybe use something like this in your function that deals with the score like:

if score <= 0 then

 --action for dealing with death

elseif score >= 1000 then

–action when you win the game

end

This could be done like this:

local scoreFunc = function(scoreValue)

  if score <= 0 then
  --action for dealing with death
  elseif score >= 1000 then
  --action when you win the game
end

  score = score + scoreValue
  return score
end

So if we use the “funny code” I posted earlier you could just add this to the function:

local onTouch = function(event)
if event.phase == “began” then

  scoreFunc( 10 ) --adds 10 to the score
  event.target.bodyType = “dynamic”
  timer.performWithDelay( 1000, function() event.target:removeSelf() end)
end
return true
end

You could also put the if chunk inside a enterFrame RuntimeListener, then it will check if youre “in or out” every frame of the game.

This all depends on how you like the gameplay :slight_smile:

If you like I can send you a class I made, dealing with scores

…and remeber, youre not too old for this, Im 44 and started just 3 years ago with lua and Corona , I know what I´m going to do when I retire :slight_smile:

Thank you Hendrix,

I read the score = and function and Value, but I get confuse.

I read somewhere I need to put a “String” or local text first with … (two dots) or something

where I display the text “Your score is …”

and there it will be a text that changes the score. Something like that…

Also, where do I put the functions in the create, enter scene or before, outside the scenes.?

I have one app already on the app store “Piano For Kids - Level 1” two weeks there, is doing great!

I’m working now in my second app.

My goal is to have at least 10 app by the end of next year!

Thanks for your help

Victor

Actually Victor in 1980 the computer revolution was alive and well.  I’m also 52.  The pharmacist friend in my small home town of 2500 had built is own kit computer and with my limited programming built the first pharmacy database.  A couple of my friends had personal computers in their dorm rooms.  I was busy hacking my college’s main-frame to play multi-player games.  Admittedly things were much simpler then.  Your iPad has more computing power than the entire college campus had in 1980. 

I know Rob, 1980 in Mexico, in a little town call Tijuana, and very poor…

That’s why I said no computers.

My first experience with computers was on 1984. We bought a little “Atari” computer to make music. It was just the keyboard, no monitor, we used one old black and white tv, we could hardly see what was in there, but we made some good songs back then.

What youre describing is a static string concatenated with a score variable like this:

local myScoreText = “Your score is ”
local score = 0

local scoreHolder = display.newText( myScoreText … score, – + all the other text params you have )
Remember the two dots means concatenate in LUA, the result – if your score is i.e. 150:

Result:

Your score is 150

Thank you Hendrix, with your help I’m getting closer to answer a question I made 6 months ago

“How to keep track of the score?” I still don’t have the final answer but I’m getting close

I did a few things you told me, and I can see the “text” in the simulator – Your score is 0

but when I touch each image, is not adding any points.

Here is the complete code

local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local physics = require("physics") physics.start() physics.setScale( 36) local littleM local buttonHome local buttonBack local myScoreText = "Your score is " local score = 0 local function homeWorld() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;storyboard.gotoScene( "mainMenu", "flip", 200 )&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return true end local function buttonBackHandler() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;storyboard.gotoScene( "physicsWorld", "fromLeft", 200 )&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return true end local scoreFunc = function(scoreValue) &nbsp; if score \<= 0 then &nbsp; --action for dealing with death &nbsp; elseif score \>= 1000 then &nbsp; --action when you win the game end &nbsp; score = score + scoreValue &nbsp; return score end local onTouch = function(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "began" then &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scoreFunc( 10 ) --adds 10 to the score &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;event.target.bodyType = "dynamic" &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.performWithDelay( 1000, function() event.target:removeSelf() end) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event ) &nbsp;&nbsp; &nbsp;local group = self.view &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local yellowRectangle = display.newRect(0, 0, 1024, 768 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;group:insert ( yellowRectangle ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;yellowRectangle.strokeWidth = 8 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;yellowRectangle:setFillColor(244, 234, 234) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;yellowRectangle:setStrokeColor(255, 255, 0) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;buttonHome = widget.newButton{ &nbsp;&nbsp; &nbsp;defaultFile="buttonHome.png", &nbsp;&nbsp; &nbsp;onRelease = homeWorld &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;group:insert ( buttonHome ) &nbsp;&nbsp; &nbsp;buttonHome.x = 950 &nbsp;&nbsp; &nbsp;buttonHome.y = 710 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;buttonBack = widget.newButton{ &nbsp;&nbsp; &nbsp;defaultFile="buttonBack.png", &nbsp;&nbsp; &nbsp;onRelease = buttonBackHandler &nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;group:insert ( buttonBack ) &nbsp;&nbsp; &nbsp;buttonBack.x = 66 &nbsp;&nbsp; &nbsp;buttonBack.y = 710 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local scoreHolder = display.newText(myScoreText .. score, 100, 600, native.systemFont, 24) &nbsp;&nbsp; &nbsp;group:insert ( scoreHolder ) &nbsp;&nbsp; &nbsp;scoreHolder:setTextColor(255, 0, 0) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; end --------------------------------------------------------------ENTER SCENE---------- function scene:enterScene( event ) &nbsp;&nbsp; &nbsp;local group = self.view &nbsp;&nbsp; &nbsp;local floor = display.newImage( "buttonSoundRed.png" ) &nbsp;&nbsp; &nbsp;group:insert ( floor ) &nbsp;&nbsp; &nbsp;floor.x = 512; floor.y = 710 &nbsp;&nbsp; &nbsp;physics.addBody( floor, "static", { friction=0.5 } ) &nbsp;&nbsp; &nbsp;littleM = {} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for column = 1, 4 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for row = 1, 4 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;littleM[column] = display.newImage( "buttonMute.png", 350 + (column\*50), -400 - (row\*40) ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;group:insert ( littleM[column] ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.addBody( littleM[column], "dynamic", { density=0.2, friction=0.1, bounce=0.4 } ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local bricks = {} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local n = 0 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local function throwBrick() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;n = n + 3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bricks[n] = display.newImage( "buttonPlay.png", -20, 140&nbsp; - (n\*11) ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;group:insert ( bricks[n] ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.addBody( bricks[n], "dynamic", { density=3.0, friction=0.5, bounce=0.05 } ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bricks[n].isBullet = true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bricks[n].angularVelocity = 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bricks[n]:applyForce( 1600, 0, bricks[n].x, bricks[n].y ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local function start() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.performWithDelay( 300, throwBrick, 2 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.performWithDelay( 3800, start ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*GOOD\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*-- &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local onTouch = function(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if event.phase == "began" then &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;event.target.bodyType = "dynamic" &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.performWithDelay( 600, function() event.target:removeSelf() end) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local littleM = {} &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for column = 1, 18 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for row = 1, 8 do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;littleM[column] = display.newImage( "buttonMute.png", (11 + (column\*50)), (400 - (row\*40)) ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;group:insert ( littleM[column] ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.addBody( littleM[column], "static", { density=0.2, friction=0.1, bounce=0.4 } ) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;littleM[column]:addEventListener("touch", onTouch) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end ----\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*---- &nbsp;&nbsp; &nbsp; end -------------------------------------------------------------------------------------- --------------------------------------------------------------EXIT SCENE---------- function scene:exitScene() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for key,obj in pairs(littleM) do &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj:removeSelf() &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end -------------------------------------------------------------------------------------- -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event ) &nbsp;&nbsp; &nbsp;local group = self.view&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if buttonHome then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonHome:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonHome = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if buttonBack then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonBack:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;buttonBack = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

I really hope that you can help me solve this problem

6 months is a long time waiting for this moment.

Victor, one of the reasons why you haven’t gotten an answer to this it because the answer is “However you want to do it”.

Generally you have a variable that holds the score as a number.  There are multiple ways around moving that value around from scene to scene, but if you find the No more Globals post, using the mydata table is a good way.

Then how you display the score is 100% up to you.  You can use display.newText() to show the score just as a number.  You could output a label like “Score” and then the number.  You could use bitmap fonts to make the digits look cool. 

This is the simplest feature.  Having a number and adding more to it as the score goes up.

I will post you a file tomorrow with all tweaks so you see how its done

(it two hours past midnight here in Norway so I have to go to bed)

Good night Hendrix …

Hi Rob, I guess I’m not asking the right question. That’s why maybe, you said “However you want to do it”

I guess for now, out of the “multiple ways” of doing it, I will get 1, out of all of those… any one.

Sense I don’t know how to do non of them, any one will do just fine.

I guess I’m not getting the concept yet of “holding the score as a number”

and I got confuse with this “No more Globals post, using the mydata table is a good way.”

I guess like Hendrix said, one code but very simple with just the basic information to have a score

will help me a lot.

I look a the “ghost” something , a sample code, too advanced for me yet I don’t get it

some other sample codes, they have too much for me, right now.

Remember, I’m in pre-school Rob.

So any simple code, but in storyboard, like I mention before, 98% of the sample codes are in just main.lua

one file, and when I try do do that in storyboard, big problems, like the making an animation stop, still

un-resolved.

Thanks for your help Rob, if there is anything I can do for you, let me know please

Heres the code/project in a zip file

copy the link and paste it in a new browser window:

www.3claws.org/test15.zip

Check it out and you´ll get it :slight_smile:

You should read up on Robs exceptional explanation on the use of dynamic scaling for diff resolutions

He has written some real good stuff on ratios for each devices

Use newImageRect and NOT newImage - it do not support dynamic size

Hi Hendrix I just went there…

Sorry, we could not locate the page you are requesting to view. Please click here to return to the community index

Not right click on the link, …mark the whole line, copy it and paste it into a new browser url field

I got it.

I don’t know if I got the right program. I start the app, and I see big yellow letters “SPILL” two times and they fall down.

I see on the top left corner, a lot of blue circles, the image you put for “buttonMute”

That’s it! – I see nothing else… No score, no buttons to go somewhere else.

I touch the blue circles, and they removeSelf, just like my littleM images, but no score.

I need to analyze the code more, but would you tell me where is the score keeping track?

ok, copy this link and paste it in a new browser window and download

www.3claws.org/ballRoller.zip

Thank you Hendrix. I got it. I’m going to study the program.

Thats good man

I have never used the widget class before so I had a little trouble when trying to get the custom images for the buttons.

Maybe “Rob the Jedi master” can enlighten us in the code I just posted ;D

For shure thank him we will (in the Yoda way…)

I don’t see any posted code?  I see a zip file.  I’d prefer not opening a zip files if possible.   Can you post a snippet of what your curious about?

Victor, regarding the score.  If you understand the mydata thing, lets go with that.

At the beginning of the game do:

     mydata.score = 0

You have to start the score somewhere.  Then however your award points (collect coins, defeat bad guys, etc.  this is the part that is up to you…  you then add your points you just earned to your score:

    mydata.score = mydata.score + points

Again, points is whatever is important to your game.   Now you have a variable that is a number that you are keeping score with.  The 2nd part of the equation is how do you want to **SHOW** the score.  And again there are a zillion ways to do so.  But to keep it simple, let’s just put the score in the top right corner using display.newText().

When your game level scene (assuming you’re using something like storyboard) creates itself:

      local scoreDisplay = display.newText(string.format("%06d", 0), 0, 0, native.systemFontBold, 20)

      scoreDisplay.x = display.contentWidth - scoreDisplay.width

      scoreDisplay.y = 20

      group:insert(scoreDisplay)

Now there are a couple of oddities here.  The string.format() is going to take a number (0 in this case) and draw it with upto 5 leading zeros.  This is important so the string gets a default width for positioning reasons later.  It also keeps the score basically in the same spot.

The next two 0’s are the X and Y to draw the string.  If we use those to draw the string, it will be based on a reference point of Top  Left.  I prefer to use the Center reference point, so I start out putting it at 0,0, then the next two lines position it where I want it.  By getting the width of the display.newText() (on of the reasons we formatted out the 6 zeros) I can position the text easier.

Now we have a score value that we are computing and keeping track of (mydata.score) and we have a display text object called scoreDisplay.

So what’s left?  Update the score on the screen.  When you add points to score (mydata.score = mydata.score + points), you should update the display.  Your code will end up looking like this:

    mydata.score = mydata.score + points

    scoreDisplay.text = string.format("%06d", mydata.score)

With those two lines of code, you can keep score and display score.

Not sure if you’ve done Jay’s tutorial Attack of the Cuteness yet but there is a very simple way to keep track of the score.  You can either follow along or just open the zip file which has a completed version.

http://www.coronalabs.com/resources/videos/

Hi guys, I just bought the $37.00 videos that Jay has on the Corona labs. I’m studying those from the very beginning. I don’t have the score part yet. But I will study that.

I have one question, it has nothing to do with score, but I don’t know where to post it. so here it is.

I have this function

local function fallOne(event) &nbsp;&nbsp; &nbsp;fallLeaf = display.newImage("leaf.png") &nbsp;&nbsp; &nbsp;fallLeaf.x = math.random ( 0, 1000); fallLeaf.y = math.random ( -100, 50) &nbsp;&nbsp; &nbsp;fallLeaf:rotate(math.random(0, 360)) &nbsp;&nbsp; &nbsp;local smaller = 1 + math.random(-1, 1) / 2 &nbsp;&nbsp; &nbsp;fallLeaf:scale (smaller, smaller) &nbsp;&nbsp; &nbsp;transition.to(fallLeaf, {time=6000, x = 500, y = 500, onComplete=greenTrash}) end

A simple function that makes leaves fall from a tree, on a “tap”

My logic it’s that onComplete that transition, I called other function

that remove the image.

local function greenTrash() &nbsp;&nbsp; &nbsp;display.remove (fallLeaf) end

it works fine, the first time!

but I have to wait, until the trasition to is finish. the leaf is falling, reaches the end, the y=500 and removed.

but if I tap on the button on the middle of the transition, that image will not be removed.

Why?.

My logic it’s not working…

onComplete remove the image, isn’t it that supposed to happend “everytime”?

Victor

Just go to the “New User” forum and start a new thread, give it a title relative to the question you’re asking…

If someone comes along later and wants to learn about scores, and they search the forums, they will have a hard time finding this thread and because it’s so long, they won’t read through it even if it comes up in search since the title is about removing things from tables.