i want to create a bag of coins. Example mario bros; when you touch a coin, the coins text increase on 1 and disappears, help please!!
[import]uid: 138440 topic_id: 27302 reply_id: 327302[/import]
For the touching part look at collision detection (if you struggle check out Corona For Newbies Part 4) and for updating the score see here; http://corona.techority.com/2012/01/30/a-simple-score-system/
Peach
[import]uid: 52491 topic_id: 27302 reply_id: 110976[/import]
Hi, sorry for take too long to respond
i already create something like you propose.
but the issue i have is that i need to create an object for each coin because when i try to duplicate the same, the function do not work so how i can use like a âforâ to create a lot of coins that works separately?
[lua]âadding the counter for the blood
local bloodcoin = 0
local blood = display.newText(âBlodd:â, 400, 10, ânative.systemFontâ, 35)
localGroup:insert(blood)
local bloddText = display.newText(bloodcoin, 500, 10, ânative.systemFontâ, 35)
localGroup:insert(bloddText)
âadding the blood image
local bloodImage = display.newCircle(300, 600, 10)
physics.addBody(bloodImage,âstaticâ,{radius=10,density=0})
localGroup:insert(bloodImage)
local bloodImage2 = display.newCircle(370, 590, 10)
physics.addBody(bloodImage2,âstaticâ,{radius=10})
localGroup:insert(bloodImage2)
bloodImage.preCollision = function(self)
bloodcoin = bloodcoin +1
bloddText.text= bloodcoin
self:removeSelf()
end
bloodImage2.preCollision = function(self)
bloodcoin = bloodcoin +1
bloddText.text= bloodcoin
self:removeSelf()
end
bloodImage:addEventListener(âpreCollisionâ, bloodImage)
bloodImage2:addEventListener(âpreCollisionâ, bloodImage2)
[import]uid: 138440 topic_id: 27302 reply_id: 111689[/import]
Plug and play code for portrait iPhone will show you how to spawn the coins in for loop and have all of them work for collision - will need modifying as isnât based on your code but should show you how to get it all working smoothly;
[lua]require ( âphysicsâ )
physics.start()
physics.setGravity( 0, 0 )
local player = display.newCircle( 160, 240, 40 )
player:setFillColor(255, 0, 0)
physics.addBody(player, âstaticâ)
local coin = {}
for i = 1, 10 do
coin[i] = display.newCircle(math.random(1,320), math.random(1,480), 10)
physics.addBody(coin[i])
coin[i].myName = âcoinâ
end
local function playerCollision(event)
if event.other.myName == âcoinâ then
print âCollected coinâ
event.other:removeSelf()
end
end
player:addEventListener(âcollisionâ, playerCollision)
local function movePlayer(event)
player.x, player.y = event.x, event.y
end
player:addEventListener(âtouchâ, movePlayer)[/lua]
Peach
[import]uid: 52491 topic_id: 27302 reply_id: 111914[/import]