1

Here is my code to start off:

numTrumps=100

  local function startGame()
    myTrump=display.newImageRect("tp.png",25,25)
     myTrump.x=Random(50,_W-50)
     myTrump.y=(_H+10)
     physics.addBody(myTrump,"dynamic",{density=.1,friction=0,bounce=.9,radius=9})

    function myTrump:touch(event)

    if(timeLeft~=false) then

        if (playerReady==true) then
            if(event.phase=="ended") then
            removeTrumps(self)
        end

    end
end
end
        myTrump:addEventListener("touch",myTrump)

trumps=trumps+1

if(trumps==numTrumps) then
    gameTimer=timer.performWithDelay(1000,countDown,totalTime)
else

    playerReady=false
end
end



  local gameTimer=timer.performWithDelay(20,startGame,numTrumps)


local function Restart(event)
    if "began"==event.phase then 

if(timeLeft~=false) then

    timer.cancel(gameTimer)
     gameTimer1=timer.performWithDelay( 20, ByeTrump, 96)

    end

end
        if "moved"==event.phase then

end
            if "ended"==event.phase then

            composer.gotoScene("restarttest")

            end
        end


function ByeTrump()
    display.remove(myTrump)
end

So basically the startGame function is repeated 100 times since numTrumps=100 so each time a new image of the same object (myTrump) is created. Once i restart i want to remove the copies of that object so i made a function (ByeTrump) in which one of the myTrump's are removed, and i repeated that 96 times by making another gametimer in restart function. I made 96 disappear not 100 for testing purposes but in my game you have 20 seconds to touch as many of the 100 myTrumps possible and upon touching they are gone.Once 20 seconds is up you can no longer touch them so you have to restart.

So my problem is when i click restart only one of the myTrump's disappear not the remaining 99 (i am not touching any of the myTrumps in the 20 seconds for testing purposes).So when the new 100 myTrumps come after restarting they collide with the ones from the last round even though the last ones aren't visible anymore its like they are invisible a=but still colliding with the new ones.

RoBoTV
  • 21
  • 5

1 Answers1

0

By storing all the Trumps (lol) in the same variable you're creating each object and then, the next step, removing the connection you had with it by linking the connection with another object, so the first object is still there, but you can't reach it anymore.

Probably a single call to collectgarbage() should work (not sure though) because what the garbage collector does is eliminating any object you don't have any connection with anymore, but I'm not sure if it will work with your type of objects, so here's the standard way to do it.

If you don't want to replace the connection with the first object with a second object you need both an high number of different variables (trump1, trump2... trump100) or, better yet, a single data structure that can hold more than one variable.

That is, tables.

So you should initialize that table:

allTrumps = {}

And then, in the startGame function, instead of storing the new object in myTrump, you add a new field to the table which will hold that object:

allTrumps[#allTrumps + 1] = display.newImageRect(...)

What are you doing is checking the lenght of your table (or the number of objects in it) and storing an object in the next position.

When you want to remove the trumps, you can start from the first or from the last trump you added. We'll start from the last for semplicity.

function ByeTrump()
    display.remove(allTrumps[#allTrumps])

end

This will remove the last trump you created, so the table will contain one less object, the size will decrease by one, and you can already call the function again to remove another trump.

user6245072
  • 2,051
  • 21
  • 34
  • Thanks but how can I add a touch event listener to each myTrump(each image) generated as well as how can I add a physics body to each one? – RoBoTV May 31 '16 at 21:26
  • I added my startgame function the whole thing to the code so you know where my touch listener is. – RoBoTV May 31 '16 at 21:42
  • I'm not familiar enough with corona to answer this one, sorry. Try writing another question. – user6245072 Jun 01 '16 at 05:00