how to collect items in a game?

I try to make something, like a mario game (let’s say :-P). The point is, the player collects coins on the screen.

I use sensors for the coins and they get collected, counted and removed, but my problem is, that they get removed as soon as my player just touches the rand of the coin.

I want them to be collected, if the player actually “cover” the coin, but I don’t want it to be at the exact same x-y coordinate, just near it, so it looks like, the player actually run over it.

Has anybody a good idea, how to do this?
Is there a sample for this? [import]uid: 6587 topic_id: 5931 reply_id: 305931[/import]

Nice… I like this. [import]uid: 12455 topic_id: 5931 reply_id: 20752[/import]

I optimized the second code a bit. [import]uid: 6587 topic_id: 5931 reply_id: 20768[/import]

Because my previous solution was for a single object and I wanted to have more coins on the screen collectible, which also give the player score points, we had to come up with something else.

now I have this:

[code]

–COINS ANIMATIONS/COLLECT START

– SpriteSheet (using SpriteGrabber)
local coinSprites=grabber.grabSheet(“coin”)

– Coins array
local coins = {}
isLiving = {}
for i=1,3 do – Nr of Coins
isLiving[i] = 1
coins[i] = coinSprites:grabSprite(“coin”,false,{ coin={1,19,100,0}})
coins[i]:playClip(“coin”)
localGroup:insert(coins[i])
end

local function coinCollect(event)
for i=1, 3 do – Nr of Coins
if isLiving[i] == 1 then
if player.x > coins[i].x -10 and player.x < coins[i].x +10
and player.y > coins[i].y -10 and player.y < coins[i].y +10 then
coins[i]:removeSelf()
coins[i] = nil
myScore:addPoint(10) – using own score.lua
myScore:update()
isLiving[i] = 0
end
end
end
end

Runtime:addEventListener( “enterFrame”, coinCollect )

– Where to place the Coins
coins[1]:show(144,144)
coins[2]:show(272,144)
coins[3]:show(400,144)


–COINS ANIMATIONS/COLLECT END

[/code] [import]uid: 6587 topic_id: 5931 reply_id: 20649[/import]

Ok, I solved this.

Maybe not cool, but it works :stuck_out_tongue:

coinCollected = false  
  
local coin = display.newImage ("images/coin.png" 0,0)  
coin.x = 232  
coin.y = 112  
coin.myName = "coin"  
physics.addBody( coin, {isSensor = true} )  
localGroup:insert(coin)  
player = display.newImage ("images/player.png",0,0)  
physics.addBody(player)  
player.x = 48  
player.y = 144  
player.myName = "player"  
localGroup:insert(player)  
-----------------------------------------  
-- COLLECT COIN START  
-----------------------------------------  
  
local function onCollision( self, event )  
 if self.myName == "coin" and event.other.myName=="player" then  
 coinCollected = true   
 end  
end  
coin.collision = onCollision  
coin:addEventListener( "collision", coin )  
local function coinCollect(event)  
if coinCollected == true and player.x \> coin.x -10 and player.x \< coin.x +10  
and player.y \> coin.y -10 and player.y \< coin.y +10 then  
 coin:removeSelf()  
 myScore:addPoint(10)  
 myScore:update()  
 coinCollected = false  
 end  
end  
Runtime:addEventListener( "enterFrame", coinCollect )  
  
-----------------------------------------  
-- COLLECT COIN END  
-----------------------------------------  

EDIT:
Some feedback would be cool…
Or other (maybe more PRO) solutions to be able to compare and learn. [import]uid: 6587 topic_id: 5931 reply_id: 20490[/import]