Collision and spawning

Hello ,

I somehow managed to make coin patterns in my game but I’m having a hard time spawning them. 

Here’s my code :

coinPatterns[1] = { {1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0}, {0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,1,0}, {0,0,0,0,0,0,0,0,1}, } coinPattern = coinPatterns[1]; local function genCoin(coinPattern) if gamePaused == false then for i = 1, 9 do for j = 1, 9 do if coinPattern[i][j] == 1 then coin = display.newImageRect(COINGROUP,"assets/images/coin.png", \_H/15, \_H/ coin.x = \_W + j\*(\_H/15) coin.y = (i\*\_H/15) coin.myName = "coin" coin.id = "coin" coin.isSensor = true physics.addBody( coin, "static") coin:toBack() end end end end function onCollision(event) local platform = event.other if platform.myName == "character" then event.contact.isEnabled = false coin.isVisible=false currentCoins = currentCoins + 1 end end coin:addEventListener("collision", onCollision) transition.to(COINGROUP,{time=6000,x = -\_W \* 2}) end

I also tried 

local function globalCollision(event) if event.object1.myName == "character" and event.object2.id == "coin" then print("Character Coin Collision") end end Runtime:addEventListener("collision",globalCollision)

But it just doesn’t collide. Maybe I’m missing something, but my other collisions like character and bottom wall is working . 

Firstly, I don’t see where you are calling genCoin. Maybe you left it out for brevity?

Anyway, in genCoin, you are looping through i and j and each time setting the new coin you create to the global variable “coin”. After you come out of the loops you create the collision listener and then add it to coin, but coin is only the last object you set it to, which will be coinPattern[9][9].

So only the one coin is actually getting the collision listener added.

Have a look at this and it should helpf with a few things:

local physics = require "physics" physics.start() local \_W, \_H = 100, 100 local gamePaused = false local coins = {} local coinPatterns = {} coinPatterns[1] = { {1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0}, {0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,1,0}, {0,0,0,0,0,0,0,0,1}, } local currentPattern = coinPatterns[1] local function onCollision(event) print("collision detected") end local function genCoin(coinPattern) if gamePaused == false then for i = 1, 9 do for j = 1, 9 do if coinPattern[i][j] == 1 then local c = (i-1)\*9 + j coins[c] = display.newRect( \_W + j\*(\_H/15), i\*\_H/15, \_H/15, \_H/15) coins[c]:setFillColor(1, 0, 0) coins[c].myName = "coin" coins[c].id = "coin" physics.addBody( coins[c], "static") coins[c].isSensor = true coins[c]:toBack() coins[c]:addEventListener("collision", onCollision) end end end end end genCoin(currentPattern)

If you are struggling with scope and the distinction between local and global variables I suggest you read the tutorial very carefully:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Get a good grasp of it now and it will save you a lot of trouble in the future.

Firstly, I don’t see where you are calling genCoin. Maybe you left it out for brevity?

Anyway, in genCoin, you are looping through i and j and each time setting the new coin you create to the global variable “coin”. After you come out of the loops you create the collision listener and then add it to coin, but coin is only the last object you set it to, which will be coinPattern[9][9].

So only the one coin is actually getting the collision listener added.

Have a look at this and it should helpf with a few things:

local physics = require "physics" physics.start() local \_W, \_H = 100, 100 local gamePaused = false local coins = {} local coinPatterns = {} coinPatterns[1] = { {1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0}, {0,0,0,0,1,0,0,0,0}, {0,0,0,0,0,1,0,0,0}, {0,0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,1,0}, {0,0,0,0,0,0,0,0,1}, } local currentPattern = coinPatterns[1] local function onCollision(event) print("collision detected") end local function genCoin(coinPattern) if gamePaused == false then for i = 1, 9 do for j = 1, 9 do if coinPattern[i][j] == 1 then local c = (i-1)\*9 + j coins[c] = display.newRect( \_W + j\*(\_H/15), i\*\_H/15, \_H/15, \_H/15) coins[c]:setFillColor(1, 0, 0) coins[c].myName = "coin" coins[c].id = "coin" physics.addBody( coins[c], "static") coins[c].isSensor = true coins[c]:toBack() coins[c]:addEventListener("collision", onCollision) end end end end end genCoin(currentPattern)

If you are struggling with scope and the distinction between local and global variables I suggest you read the tutorial very carefully:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Get a good grasp of it now and it will save you a lot of trouble in the future.