coin pattern generation using tables

I tried making coin patterns using lua tables and a general method, then added all of them in a group. They seem to be working fine but I’m facing difficulty in spawning them.  These are the two ways I tried …

(This one is not actually my code … )

local coinPatterns={ line={{x=0,y=0},{x=25,y=0},{x=50,y=0}} } -- Create Individual Coin local function createCoin(x,y) local coin=display.newImageRect("images/coin.png",20,20) coin.anchorX,coin.anchorY=0,0 coin.y=y coin.x=x coin.id='coin' physics.addBody(coin,'static',{isSensor=true,radius=10}) return coin end -- Create a coin group local function createCoinGroup() local coinGroup=display.newGroup() for key,value in pairs(coinPatterns.line) do local coin=createCoin(value.x,value.y) coinGroup:insert(coin) end coinGroup.x=screenW coinGroup.y=50 transition.to(coinGroup,{time=2000,x=0-coinGroup.width,onComplete=function() coinGroup:removeSelf() coinGroup=nil end}) foregroundGroup:insert(coinGroup) end createCoinGroup()

after this hardly worked I tried a simple approach :

local function coinLine5() local coin1=display.newImage(COINGROUP,"coin.png") coin1.x=100 coin1.y=100 physics.addBody(coin1,"static",{density = 1, bounce = 0.1, friction = 2, radius = 20}) coin1.myName="coin1" local coin2=display.newImage(COINGROUP,"coin.png") coin2.x=120 coin2.y=100 physics.addBody(coin2,"static",{density = 1, bounce = 0.1, friction = 2, radius = 20}) coin2.myName="coin2" local coin3=display.newImage(COINGROUP,"coin.png") coin3.x=140 coin3.y=100 physics.addBody(coin3,"static",{density = 1, bounce = 0.1, friction = 2, radius = 20}) coin3.myName="coin3" local coin4=display.newImage(COINGROUP,"coin.png") coin4.x=160 coin4.y=100 physics.addBody(coin4,"static",{density = 1, bounce = 0.1, friction = 2, radius = 20}) coin4.myName="coin4" local coin5=display.newImage(COINGROUP,"coin.png") coin5.x=180 coin5.y=100 physics.addBody(coin5,"static",{density = 1, bounce = 0.1, friction = 2, radius = 20}) coin5.myName="coin5" COINGROUP.x=600 end coinLine5() local function moveCoins(self,event) if self.x \< -500 then Runtime:removeEventListener("enterFrame", COINGROUP) COINGROUP:removeSelf() COINGROUP=nil else self.x = self.x - objectSpeed end end COINGROUP.enterFrame=moveCoins

Same issue in both the methods … unable to spawn the coin … plz help … !!!

DRY

try this

local coinPatterns = { &nbsp;&nbsp; &nbsp;["Line"] = { {x = 0, y = 0},{x = 30, y = 0},{x = 60, y = 0}&nbsp; }, &nbsp;&nbsp; &nbsp;["Diagonal"] = { {x = 0, y = -50},{x = 30, y = 0 },{x = 60, y = 50}} } local function SpawnCoins (params) &nbsp;&nbsp; &nbsp;local tmp = {} &nbsp;&nbsp; &nbsp;tmp.rect = display.newRect(params.group, params.x, params.y, 10, 10 ) &nbsp;&nbsp; &nbsp;function tmp:destroy() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;display.remove(self.rect) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.rect = nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;function tmp:move(speed) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.rect:translate( -speed, 0 ) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;return tmp end local CoinGroup = display.newGroup() local CurCoins = {} for k,v in pairs( coinPatterns["Diagonal"] ) do &nbsp;&nbsp; &nbsp;CurCoins[k] = SpawnCoins({x = v.x,y = v.y, group = CoinGroup}) end CoinGroup.x = 320 CoinGroup.y = 240 Runtime:addEventListener( "enterFrame", function () &nbsp;&nbsp; &nbsp;for i = 1,#CurCoins do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CurCoins[i]:move(2) &nbsp;&nbsp; &nbsp;end end )

Thank you so much !!! Nice article on DRY …

The thing is I was able to create Coin groups using tables … the difficulty is in spawning them ( the collision function ) … In your code how do I refer to a specific coin ( rectangle ) in the collision function … I added the coins to physics body … how should i call the destroy function ?

-- destroy specific coins by using -- 1 is the coins index, so the first one CurCoins[1]:destroy()

btw if you are adding physics bodies make sure you do it within the SpawnCoin function

The problem is the event listener for colloison function is tmp:addEventListener(“collision”, onCollision)

so the function only checks for one specific coin and not the whole lot … and using CurCoins[i]:addEventListener(“collision”, onCollision)

is not permitted . So do I have to write seperate case for each element (coin) of the pattern ?

P.S. : I’m confused with the usage of collision function for the whole group i.e. complete CurCoins {}

nope use a table listener

here is a simple touch example make sure you place code similar to this within the SpawnCoin function

local function onTouch(self,event) -- the 'self' parameter contains a reference to the object -- it only applies to that peticular instance! &nbsp;&nbsp;&nbsp; print(self) end tmp.rect.touch = onTouch tmp.rect:addEventListener( "touch", tmp.rect )

if I’m not wrong touch is used when user touches the screen rite ?  I’m trying to spawn the coin when my jet passes through it or collides with it !!!

that was just an example, adapt the code to suit your needs

DRY

try this

local coinPatterns = { &nbsp;&nbsp; &nbsp;["Line"] = { {x = 0, y = 0},{x = 30, y = 0},{x = 60, y = 0}&nbsp; }, &nbsp;&nbsp; &nbsp;["Diagonal"] = { {x = 0, y = -50},{x = 30, y = 0 },{x = 60, y = 50}} } local function SpawnCoins (params) &nbsp;&nbsp; &nbsp;local tmp = {} &nbsp;&nbsp; &nbsp;tmp.rect = display.newRect(params.group, params.x, params.y, 10, 10 ) &nbsp;&nbsp; &nbsp;function tmp:destroy() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;display.remove(self.rect) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.rect = nil &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;function tmp:move(speed) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.rect:translate( -speed, 0 ) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;return tmp end local CoinGroup = display.newGroup() local CurCoins = {} for k,v in pairs( coinPatterns["Diagonal"] ) do &nbsp;&nbsp; &nbsp;CurCoins[k] = SpawnCoins({x = v.x,y = v.y, group = CoinGroup}) end CoinGroup.x = 320 CoinGroup.y = 240 Runtime:addEventListener( "enterFrame", function () &nbsp;&nbsp; &nbsp;for i = 1,#CurCoins do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CurCoins[i]:move(2) &nbsp;&nbsp; &nbsp;end end )

Thank you so much !!! Nice article on DRY …

The thing is I was able to create Coin groups using tables … the difficulty is in spawning them ( the collision function ) … In your code how do I refer to a specific coin ( rectangle ) in the collision function … I added the coins to physics body … how should i call the destroy function ?

-- destroy specific coins by using -- 1 is the coins index, so the first one CurCoins[1]:destroy()

btw if you are adding physics bodies make sure you do it within the SpawnCoin function

The problem is the event listener for colloison function is tmp:addEventListener(“collision”, onCollision)

so the function only checks for one specific coin and not the whole lot … and using CurCoins[i]:addEventListener(“collision”, onCollision)

is not permitted . So do I have to write seperate case for each element (coin) of the pattern ?

P.S. : I’m confused with the usage of collision function for the whole group i.e. complete CurCoins {}

nope use a table listener

here is a simple touch example make sure you place code similar to this within the SpawnCoin function

local function onTouch(self,event) -- the 'self' parameter contains a reference to the object -- it only applies to that peticular instance! &nbsp;&nbsp;&nbsp; print(self) end tmp.rect.touch = onTouch tmp.rect:addEventListener( "touch", tmp.rect )

if I’m not wrong touch is used when user touches the screen rite ?  I’m trying to spawn the coin when my jet passes through it or collides with it !!!

that was just an example, adapt the code to suit your needs