Hello everybody, i’m Michael and new here, and new to corona and programming at all.
so excuse me if my question or code is kind of strange.
so, i’m programming a small jump and run game.
i made a collectible coin script which is in it’s own file “coins.lua”
in that file i have a collision listener that adds 1 point to the coin count and removes the coinobject and sets it to nil. as long as the coin collides with the hero.
i require and instantiate those coins at my level1.lua script (what is my main script after the menu, i’m using the storyboard template)
visually, all works very nice. the coins do what they shall do and dissappear after the “hero” collected (collided with) them. but when i check if they are still existing from within level1.lua, with a print(coin1) or a if(coin1)then thisandthat end, after i collected them i have proof that they still exist.
i found a little workaround that i dont really like by removing them in level1.lua instead of coins.lua, but i would prefer to keep it all in the coins script, due to reusability.
here is the code (without the workaround):
coins.lua
[lua]local physics = require “physics”
local sheetInfo = require(“sprites.coins_sheet@2x”)
local ImageSheet = graphics.newImageSheet( “sprites/coins_sheet@2x.png”, sheetInfo:getSheet() )
local sequenceData = require(“sprites.coins_sheet_sequences”)
local sound = audio.loadSound (“sounds/coins.wav”)
local Coins = {}
local function coinCollecting(self, event)
if (event.other.isHero == true) then
if (event.phase == “began”) then
audio.play (sound)
coinscollected = coinscollected + self.worth
–works visually, but in level1.lua the table will still exist, with all of it’s variables and values.
self:removeEventListener(“collision”)
self:removeSelf()
self = nil
end
end
end
function Coins.newCoin(spawnX, spawnY)
–objSprite
local obj = display.newSprite( ImageSheet, sequenceData)
obj:setSequence( “coin_loop” )
obj:play()
obj.x = spawnX
obj.y = spawnY
physics.addBody( obj,“static”, { friction=0, density=0, bounce=0, radius=25 } )
obj.isFixedRotation = true
obj.isSensor = true
obj.worth = 1
obj.collision = coinCollecting
obj:addEventListener(“collision”)
return obj
end
return Coins[/lua]
here is the part that bugs me in
level1.lua
[lua]local Coins = require(“coins”)
local coin = {}
local function testcoins()
coin[1] = Coins.newCoin(200,320)
–a for loop just in case i add more coins, what i propably will do some day
for a = 1, #coin , 1 do
gameworld:insert( coin[a] )
end
end
–i call checkAfterCollecting() with a touch on a button, just to test if the coin still exists
–because visually it doesnt, it dissappeared as i want it to.
local function checkAfterCollecting()
print(coin[1]) --will tell me the table
print(coin[1].worth) --will still tell me the worth of the coin
end[/lua]
i simply dont understand it.
if i print the table from within level1.lua and within the collisionlistener of coins.lua, i even see that it’s the same table.
i tried to figure this out my own.
but all i was coming up with was a really not nice looking workaround. and besides that, i would just simply like to understand what is wrong. so that i understand corona a bit more, and won’t run in similar problems in the future.
thanks a lot in advance,
and thanks for the many times you allready helped me without knowing, by discussing script problems and solutions on this forum.
cheers,
Michael [import]uid: 11133 topic_id: 32758 reply_id: 332758[/import]