Basicly I have the code for the coin and it’s working perfectly, The only problem is how do i remove the coin that I touch? I tried adding a function with isVisible = false but that made them all disappear. Does anyone know how I can make just the one I touched disapear?
(sorry my previous post was a mistake)
How do you create your coins ? You have to use different names in order to target them and remove them.
Hey, So it would be like this…
local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) local wall = display.newRect( display.contentCenterX - 100, display.contentCenterY, 10, 100 ) physics.addBody( wall, "static" ) wall.myName = "wall" local coin = display.newCircle( display.contentCenterX, display.contentCenterY, 10 ) physics.addBody( coin, "dynamic", { radius = 10 } ) coin.gravityScale = 0 coin.myName = "coin" coin:setLinearVelocity( -100, 0 ) local function onCollision(event) if event.phase == "began" then if event.target.myName == "wall" and event.other.myName == "coin" then local removeOneCoin = event.other display.remove(removeOneCoin) end end end wall:addEventListener( "collision", onCollision )
You can put this sample into any main.lua …
Good Luck!
I’ve tested your exemple and the coin does seem to disappear properly. I’m not sure to understand your problem…
In your first post, you’re talking about more than one coin, right ?
My example isn’t working? If not here is a fully working example that i have tested and it working…
local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) local coins = {} local cCounter = 1 local coinTimer local wall = display.newRect( display.contentCenterX, display.contentCenterY, display.actualContentWidth, 30 ) physics.addBody( wall, "static" ) wall.myName = "wall" local function onCollision(event) if event.phase == "began" then if event.target.myName == "wall" and event.other.myName == "coin" then local removeOneCoin = event.other display.remove(removeOneCoin) end end end wall:addEventListener( "collision", onCollision ) local function spawnCoins() local fallCoins = math.random(display.contentWidth \* -0.2, display.contentWidth \* 1.2) coins[cCounter] = display.newCircle( fallCoins, display.contentCenterY - 500, 20 ) physics.addBody( coins[cCounter], "dynamic" ) coins[cCounter]:setLinearVelocity( 0, 300 ) coins[cCounter].myName = "coin" coins[cCounter].value = cCounter cCounter = cCounter + 1 end coinTimer = timer.performWithDelay( 500, spawnCoins, -1 )
This spawns many coins and removes them one by one on collision.
Good Luck!
Exactly what I was looking for! Works perfectly thanks!!
I just needed to make a couple adjustments, but it works perfectly!
Check out the newer sample if you’d like! I’m always glad to help!
Good Luck!
If you wanted to know what I did…
I just created the coin like this…
coining = display.newImageRect("coin.png", 80,80) coining.anchorX = 0.5 coining.anchorY = 0.5 coining.x = display.contentWidth + 100 physics.addBody(coining, "dynamic") coining.gravityScale = 0 coining.myName = "coin" coining.y = height + 1
Then modified your function
function removingthecoin(event) if event.phase == "began" then local removeOneCoin = event.target display.remove(removeOneCoin) end end
I already had the physics for my other thing so now when they collide the single coin gets removed, Thanks for your help!!
Sorry, I didn’t pay attention to your name SonicX278, I thought it was sprs8887 who was submitting his own code 
(sorry my previous post was a mistake)
How do you create your coins ? You have to use different names in order to target them and remove them.
Hey, So it would be like this…
local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) local wall = display.newRect( display.contentCenterX - 100, display.contentCenterY, 10, 100 ) physics.addBody( wall, "static" ) wall.myName = "wall" local coin = display.newCircle( display.contentCenterX, display.contentCenterY, 10 ) physics.addBody( coin, "dynamic", { radius = 10 } ) coin.gravityScale = 0 coin.myName = "coin" coin:setLinearVelocity( -100, 0 ) local function onCollision(event) if event.phase == "began" then if event.target.myName == "wall" and event.other.myName == "coin" then local removeOneCoin = event.other display.remove(removeOneCoin) end end end wall:addEventListener( "collision", onCollision )
You can put this sample into any main.lua …
Good Luck!
I’ve tested your exemple and the coin does seem to disappear properly. I’m not sure to understand your problem…
In your first post, you’re talking about more than one coin, right ?
My example isn’t working? If not here is a fully working example that i have tested and it working…
local physics = require("physics") physics.start() physics.setDrawMode( "hybrid" ) local coins = {} local cCounter = 1 local coinTimer local wall = display.newRect( display.contentCenterX, display.contentCenterY, display.actualContentWidth, 30 ) physics.addBody( wall, "static" ) wall.myName = "wall" local function onCollision(event) if event.phase == "began" then if event.target.myName == "wall" and event.other.myName == "coin" then local removeOneCoin = event.other display.remove(removeOneCoin) end end end wall:addEventListener( "collision", onCollision ) local function spawnCoins() local fallCoins = math.random(display.contentWidth \* -0.2, display.contentWidth \* 1.2) coins[cCounter] = display.newCircle( fallCoins, display.contentCenterY - 500, 20 ) physics.addBody( coins[cCounter], "dynamic" ) coins[cCounter]:setLinearVelocity( 0, 300 ) coins[cCounter].myName = "coin" coins[cCounter].value = cCounter cCounter = cCounter + 1 end coinTimer = timer.performWithDelay( 500, spawnCoins, -1 )
This spawns many coins and removes them one by one on collision.
Good Luck!
Exactly what I was looking for! Works perfectly thanks!!
I just needed to make a couple adjustments, but it works perfectly!
Check out the newer sample if you’d like! I’m always glad to help!
Good Luck!
If you wanted to know what I did…
I just created the coin like this…
coining = display.newImageRect("coin.png", 80,80) coining.anchorX = 0.5 coining.anchorY = 0.5 coining.x = display.contentWidth + 100 physics.addBody(coining, "dynamic") coining.gravityScale = 0 coining.myName = "coin" coining.y = height + 1
Then modified your function
function removingthecoin(event) if event.phase == "began" then local removeOneCoin = event.target display.remove(removeOneCoin) end end
I already had the physics for my other thing so now when they collide the single coin gets removed, Thanks for your help!!
Sorry, I didn’t pay attention to your name SonicX278, I thought it was sprs8887 who was submitting his own code 