Code help for Corona Sdk

My code to start off:

[lua]

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

–End of start game function

  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

[/lua]

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). How can i fix this?

You probably should use a table/array to store your myTrumps in so you can later access them all. Now each time you create a new myTrump you are loosing your reference to the previous ones.

local myTrumps = {} myTrumps[#myTrumps+1] = display.newImageRect("tp.png",25,25)

Later when you’re ready to remove them just loop over the table and remove them:

for i = numTrumps, 1, -1 do      if myTrumps[i] then           display.remove( myTrumps[i] )           myTrumps[i] = nil      end end

Rob

Thanks Rob but how can I add a touch event listener to each of the myTrump as well as a physics body? I showed my whole startgame function for any clarifications.

Maybe something like this?

local myTrumps local function startGame() myTrumps[#myTrumps + 1]=display.newImageRect("tp.png",25,25) myTrumps[#myTrumps].x=Random(50,\_W-50) myTrumps[#myTrumps].y=(\_H+10) myTrumps[#myTrumps].id = #myTrumps physics.addBody(myTrumps[#myTrumps],"dynamic",{density=.1,friction=0,bounce=.9,radius=9}) function myTrumps[#myTrumps]:touch(event) if(timeLeft~=false) then if (playerReady==true) then if(event.phase=="ended") then removeTrumps(self) end end end end myTrumps[#myTrumps]:addEventListener("touch",myTrumps[#myTrumps]) trumps=trumps+1 if(trumps==numTrumps) then gameTimer=timer.performWithDelay(1000,countDown,totalTime) else playerReady=false end end --End of start game function

You’re removeTrumps() will likely need to be updated to do a table.remove( myTrumps, indexOfTheTrumpToRemove), perhaps the same with ByeTrump(). ByeTrump only removes the last one too. Your timer calling ByeTrump needs to know which one to remove, which logically could be the oldest one:

local function ByeTrump()     display.remove( myTrumps[1] )     myTrumps[1] = nil     table.remove( myTrumps, 1) end

I can’t see your RemoveTrump() function but since  you’re passing in “self”, then RemoveTrump() has a reference to the object to remove.

local function RemoveTrump( trump )      local thisTrump = trump.id -- we added the array index to the table above      display.remove( myTrumps[thisTrump] )      myTrumps[thisTrump] = nil      table.remove( myTrumps, thisTrump ) end

or something similar (code untested and could have typos!)

Thank you so much almost everything works the trumps go away and everything. But the touch function doesn’t work. I think it’s the way u wrote it. Usually the myTrump:touch is green but when I added the #myTrump it got messed up. And there were some typos but I fixed them. For example you forgot the ={}

Don’t get hung up on the syntax highlighting, at least not here in the the forums. As for the errors in the code, I don’t have your app so I can’t really test it. I do the best I can while typing it in, but that’s why I put this disclaimer at the bottom: (code untested and could have typos!)

My recommendation at this point would be to:

  1. Put some prints in the touch function to dump out the event table and see what you’re getting.

  2. Instead of using “self”, use event.target.

Rob

[lua]

local function removeTrumps(obj)

    obj:removeSelf()

    

    trumps=trumps-1

     --[[local thisTrump=myTrump.id

    display.remove(myTrump[thisTrump])

    myTrump[thisTrump]=nil

    table.remove(myTrump,thisTrump)]]–

   

    if (timeLeft~=false) then 

    if(trumps==0) then

    timer.cancel(gameTimer)

    gameOver(“winner”)

    

    elseif(trumps<=40) then

   

    gameOver(“notbad”)

    elseif(trumps>=31) then

   

    gameOver(“loser”)

    end

    

    end

    end

    

    

    local function startGame()

    myTrump[#myTrump+1]=display.newImageRect(“tp.png”,25,25)

    myTrump[#myTrump].x=Random(50,_W-50)

    myTrump[#myTrump].y=(_H+10)

    myTrump[#myTrump].id=#myTrump

    physics.addBody(myTrump[#myTrump],“dynamic”, {density=.1, friction=0, bounce=.9, radius=9})

    

    

     --TOUCH FUNCTION FIX IT

       function onTouch(event)

    

    if(timeLeft~=false) then

    

    if (playerReady==true) then

    if(event.phase==“ended”) then

    removeTrumps(self)

   

    

    end

    end

    end

    end

    --if i put onTouch then removetrumps is ? if i leave it as #myTrump it just doenst recognize the touch

    myTrump[#myTrump]:addEventListener(“touch”, onTouch)

    trumps=trumps+1

    

    if(trumps==numTrumps) then

    gameTimer=timer.performWithDelay(1000,countDown,totalTime)

    else

    

    playerReady=false

    end

    end[/lua]

So here is my problem. I dont know how to properly set the onTouch event listener to the table object #myTrump and im trying to make it so when you click the image it disappears but its not working right. The way it is right now it talks about obj (in the removeTrumps function)being a nil value. How do i fix this? The objects aren’t disappearing.

On line forty two do

removeTrumps(event.target)

You probably should use a table/array to store your myTrumps in so you can later access them all. Now each time you create a new myTrump you are loosing your reference to the previous ones.

local myTrumps = {} myTrumps[#myTrumps+1] = display.newImageRect("tp.png",25,25)

Later when you’re ready to remove them just loop over the table and remove them:

for i = numTrumps, 1, -1 do &nbsp;&nbsp;&nbsp;&nbsp; if myTrumps[i] then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove( myTrumps[i] ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myTrumps[i] = nil &nbsp;&nbsp;&nbsp;&nbsp; end end

Rob

Thanks Rob but how can I add a touch event listener to each of the myTrump as well as a physics body? I showed my whole startgame function for any clarifications.

Maybe something like this?

local myTrumps local function startGame() myTrumps[#myTrumps + 1]=display.newImageRect("tp.png",25,25) myTrumps[#myTrumps].x=Random(50,\_W-50) myTrumps[#myTrumps].y=(\_H+10) myTrumps[#myTrumps].id = #myTrumps physics.addBody(myTrumps[#myTrumps],"dynamic",{density=.1,friction=0,bounce=.9,radius=9}) function myTrumps[#myTrumps]:touch(event) if(timeLeft~=false) then if (playerReady==true) then if(event.phase=="ended") then removeTrumps(self) end end end end myTrumps[#myTrumps]:addEventListener("touch",myTrumps[#myTrumps]) trumps=trumps+1 if(trumps==numTrumps) then gameTimer=timer.performWithDelay(1000,countDown,totalTime) else playerReady=false end end --End of start game function

You’re removeTrumps() will likely need to be updated to do a table.remove( myTrumps, indexOfTheTrumpToRemove), perhaps the same with ByeTrump(). ByeTrump only removes the last one too. Your timer calling ByeTrump needs to know which one to remove, which logically could be the oldest one:

local function ByeTrump() &nbsp;&nbsp;&nbsp; display.remove( myTrumps[1] ) &nbsp;&nbsp;&nbsp; myTrumps[1] = nil &nbsp;&nbsp;&nbsp; table.remove( myTrumps, 1) end

I can’t see your RemoveTrump() function but since  you’re passing in “self”, then RemoveTrump() has a reference to the object to remove.

local function RemoveTrump( trump ) &nbsp;&nbsp;&nbsp;&nbsp; local thisTrump = trump.id -- we added the array index to the table above &nbsp;&nbsp;&nbsp;&nbsp; display.remove( myTrumps[thisTrump] ) &nbsp;&nbsp;&nbsp;&nbsp; myTrumps[thisTrump] = nil &nbsp;&nbsp;&nbsp;&nbsp; table.remove( myTrumps, thisTrump ) end

or something similar (code untested and could have typos!)

Thank you so much almost everything works the trumps go away and everything. But the touch function doesn’t work. I think it’s the way u wrote it. Usually the myTrump:touch is green but when I added the #myTrump it got messed up. And there were some typos but I fixed them. For example you forgot the ={}

Don’t get hung up on the syntax highlighting, at least not here in the the forums. As for the errors in the code, I don’t have your app so I can’t really test it. I do the best I can while typing it in, but that’s why I put this disclaimer at the bottom: (code untested and could have typos!)

My recommendation at this point would be to:

  1. Put some prints in the touch function to dump out the event table and see what you’re getting.

  2. Instead of using “self”, use event.target.

Rob

[lua]

local function removeTrumps(obj)

    obj:removeSelf()

    

    trumps=trumps-1

     --[[local thisTrump=myTrump.id

    display.remove(myTrump[thisTrump])

    myTrump[thisTrump]=nil

    table.remove(myTrump,thisTrump)]]–

   

    if (timeLeft~=false) then 

    if(trumps==0) then

    timer.cancel(gameTimer)

    gameOver(“winner”)

    

    elseif(trumps<=40) then

   

    gameOver(“notbad”)

    elseif(trumps>=31) then

   

    gameOver(“loser”)

    end

    

    end

    end

    

    

    local function startGame()

    myTrump[#myTrump+1]=display.newImageRect(“tp.png”,25,25)

    myTrump[#myTrump].x=Random(50,_W-50)

    myTrump[#myTrump].y=(_H+10)

    myTrump[#myTrump].id=#myTrump

    physics.addBody(myTrump[#myTrump],“dynamic”, {density=.1, friction=0, bounce=.9, radius=9})

    

    

     --TOUCH FUNCTION FIX IT

       function onTouch(event)

    

    if(timeLeft~=false) then

    

    if (playerReady==true) then

    if(event.phase==“ended”) then

    removeTrumps(self)

   

    

    end

    end

    end

    end

    --if i put onTouch then removetrumps is ? if i leave it as #myTrump it just doenst recognize the touch

    myTrump[#myTrump]:addEventListener(“touch”, onTouch)

    trumps=trumps+1

    

    if(trumps==numTrumps) then

    gameTimer=timer.performWithDelay(1000,countDown,totalTime)

    else

    

    playerReady=false

    end

    end[/lua]

So here is my problem. I dont know how to properly set the onTouch event listener to the table object #myTrump and im trying to make it so when you click the image it disappears but its not working right. The way it is right now it talks about obj (in the removeTrumps function)being a nil value. How do i fix this? The objects aren’t disappearing.

On line forty two do

removeTrumps(event.target)