How to remove a table or array

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)     fallLeaf = display.newImage("leaf.png")     fallLeaf.x = math.random ( 0, 1000); fallLeaf.y = math.random ( -100, 50)     fallLeaf:rotate(math.random(0, 360))     local smaller = 1 + math.random(-1, 1) / 2     fallLeaf:scale (smaller, smaller)     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()     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.

Okay

Something like this will work…

for key,obj in pairs(littleM) do obj:removeSelf() obj = nil end

The other way would be to create a displayGroup, and then :insert each image to that group, and then call removeSelf() on the group. Note that you’d still need to nil the reference to littleM if you did this alternate approach.

Thank you…

but where do I put this code?

in the exitScene, or the destroyScene?

I also added the physics to the littleM and when I go to another scene, the image “floor” that has physics, removeSelf

and all the images “littleM” just goes down out of the view…

Are they still in memory, or they are removed from memory?

littleM = {}         for column = 1, 18 do             for row = 1, 8 do             littleM[column] = display.newImage( "buttonMute.png", 11 + (column\*50), 400 - (row\*40) )             physics.addBody( littleM[column], { density=0.2, friction=0.1, bounce=0.4 } )         end     end

Thanks

I’m not 100% sure how to answer given I don’t have much code to review and therefore not how to suggest where to put it… it would depend on what you are doing with the scenes. Normally you’d attach all display groups to the scene (and they would be shown/hidden) and only destroy them in the destroyScene event. It is hard to say what you are doing.

Oh, and calling removeSelf on the display object will destroy physics bodies and physics joints

The way many coders do it is to mark the littleM instance for destruction by moving it to another table i.e. tableOfDestruction and when you feel like it do this i.e. inside onExitScene() local destroyLittleBuggers() for key, obj in pairs(tableOfDestruction) do obj:removeSelf() obj = nil end this is a nice approach because its not always allowed to just destroy your littleM´s inside other chunks of code in these cases you just --------inside some chunk of code i.e a loop table.insert(tableOfDestruction, littleM[i]) --if you like to remove the instance later by putting it into the other table littleM[i]:removeSelf() --|| if you like to remove the instance littleM[i] = nil --|| if you like to release the placeholder/variable littleM[i] littleM[i].alpha = 0 – if you just like to hide it littleM[i]:RemoveEventListener(“someEvent”, littleM[i]) --if you made this kind of approach Hope this didn get too messy

Hi guys, gslender, and Hendrix000007.

I admire you a lot, I wish I knew programming like you…

When you explain things, I read them, and they look great.

But my mind is not “yet” to that level.

But if you have patience with me, I will learn, I promise you that.

This is the complete code for littleM

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 function homeWorld()         storyboard.gotoScene( "mainMenu", "flip", 200 )         return true end local function buttonBackHandler()         storyboard.gotoScene( "physicsWorld", "fromLeft", 200 )         return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event )     local group = self.view          local yellowRectangle = display.newRect(0, 0, 1024, 768 )         group:insert ( yellowRectangle )             yellowRectangle.strokeWidth = 8             yellowRectangle:setFillColor(244, 234, 234)             yellowRectangle:setStrokeColor(255, 255, 0)          buttonHome = widget.newButton{     defaultFile="buttonHome.png",     onRelease = homeWorld     }     group:insert ( buttonHome )     buttonHome.x = 950     buttonHome.y = 710          buttonBack = widget.newButton{     defaultFile="buttonBack.png",     onRelease = buttonBackHandler     }     group:insert ( buttonBack )     buttonBack.x = 66     buttonBack.y = 710         end --------------------------------------------------------------ENTER SCENE---------- function scene:enterScene( event )     local group = self.view     local floor = display.newImage( "buttonSoundRed.png" )     group:insert ( floor )     floor.x = 512; floor.y = 710     physics.addBody( floor, "static", { friction=0.5 } )     littleM = {}         for column = 1, 4 do             for row = 1, 4 do             littleM[column] = display.newImage( "buttonMute.png", 350 + (column\*50), -400 - (row\*40) )             physics.addBody( littleM[column], { density=0.2, friction=0.1, bounce=0.4 } )         end     end              local bricks = {}         local n = 0         local function throwBrick()             n = n + 3             bricks[n] = display.newImage( "buttonPlay.png", -20, 140  - (n\*11) )             physics.addBody( bricks[n], { density=3.0, friction=0.5, bounce=0.05 } )             bricks[n].isBullet = true             bricks[n].angularVelocity = 1             bricks[n]:applyForce( 1600, 0, bricks[n].x, bricks[n].y )         end         local function start()             timer.performWithDelay( 300, throwBrick, 2 )         end         timer.performWithDelay( 3800, start ) end -------------------------------------------------------------------------------------- --------------------------------------------------------------EXIT SCENE---------- function scene:exitScene() for key,obj in pairs(littleM) do                obj:removeSelf()                obj = nil         end           end -------------------------------------------------------------------------------------- -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event )     local group = self.view                  if buttonHome then             buttonHome:removeSelf()             buttonHome = nil         end                  if buttonBack then             buttonBack:removeSelf()             buttonBack = nil         end end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thanks

Hi there

First of all, your physic bodies fall out off the screen because you didn´t specify a type of body:

your code inside the littleM chunk:

physics.addBody( littleM[column], { density=0.2, friction=0.1, bounce=0.4 } )

this will keep it on the screen :slight_smile:

physics.addBody( littleM[column], “static” , { density=0.2, friction=0.1, bounce=0.4 } )

and if you change it to “dynamic” it will respond to the physic world

Yes, the objects keeps falling, and falling…and falling :slight_smile:

To fix this you could do:

Inside your instantiating of littleM you could assign “the children” in the group a name like this:

local littleM = {}

        for column = 1, 18 do
            for row = 1, 8 do
            littleM[column] = display.newImage( “buttonMute.png”, (11 + (column*50)), (400 - (row*40)) )

             littleM[column].name = “littleManiacs” --<<HERE<<
            physics.addBody( littleM[column], “static”, { density=0.2, friction=0.1, bounce=0.4 } )
        end
    end

Now you can make an eventListener that keep track og the bodies that falls on the outside of your screen then it will be removed
i.e. like this:

enterFrameListener = function( event )
        if ( event.target.name == “littleManiacs” and event.target >= display.contentHeight + 50 ) then

            – do all the clean up routine like stoping physics, remove RuntimeListener and timers and so on

            – like your routine :slight_smile:

            killAllLittleM() --now you have, by doing this, checked if the actual object goes by the name littleManiacs

                                 – and wether the objects is inside the acceptable reach of your screen

        end

Runtime:addEventListener( “enterFrame”, enterFrameListener ) --remember to remove this type of listeners

                                                                                                      --when leaving the scene

Let us know how far you get with this and we will help you all the way man…this IS the fun part :slight_smile:

Please note that I haven´t tested this code, just dug it out of my head now so there may be some bugs LOL

    end

Heres a funny one you can play with:

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local physics = require(“physics”)
physics.start()

local onTouch = function(event)
if event.phase == “began” then
  event.target.bodyType = “dynamic”
  timer.performWithDelay( 1000, function() event.target:removeSelf() end)
end
return true
end

local littleM = {}

        for column = 1, 18 do
            for row = 1, 8 do
            littleM[column] = display.newImage( “buttonMute.png”, (11 + (column*50)), (400 - (row*40)) )
            physics.addBody( littleM[column], “static”, { density=0.2, friction=0.1, bounce=0.4 } )
   littleM[column]:addEventListener(“touch”, onTouch)
        end
    end

Victor, back to your top post… is there a reason you did not do a

group:insert(littleM[column])

???

Hi guys. I’m 52…I wish I would have started this when I was 20…(but in 1980, there were no computers, sorry)

There is so much to learn, I’m just beginning to see things. This is a lot of fun…

Anyway…

Rob, thanks you very much, that solve the problem, it works!!!

I did not put

physics.addBody( littleM[column], "static", { density=0.2, friction=0.1, bounce=0.4 } )

Because I was just using

physics.addBody( littleM, "static", { density=0.2, friction=0.1, bounce=0.4 } )

So that means you also have to use the number on the table right? by the way the function I copy said “for i” and “for j” but it makes more sense to use column and rows.

but it works, thanks Rob.

As for the funny function, Hendrix000007 – IT’S really nice! Thanks. I can make something really good with it.

I need a little favor, maybe it’s too much for me now, but I guess I have to try.

The littleM removeSelf when I touch it, great!

How can I make the program have a “SCORE” that keeps track of the littleM that have been removed?