I need an object to remove self after another object touches it.
For example if an arrow hits a chicken then the chicken is dead and is removed from screen.
Thanks for all help [import]uid: 54001 topic_id: 9563 reply_id: 309563[/import]
I need an object to remove self after another object touches it.
For example if an arrow hits a chicken then the chicken is dead and is removed from screen.
Thanks for all help [import]uid: 54001 topic_id: 9563 reply_id: 309563[/import]
Set up a listener to remove the chicken on a collision. Something like this:
local function removeChicken( event )
if ( event.phase == "began" ) then
display.remove( event )
end
end
Set up the listener using the info here:
http://developer.anscamobile.com/content/game-edition-collision-detection [import]uid: 31262 topic_id: 9563 reply_id: 34952[/import]
@aaaron
let’s say the chicken was defined as
local chicken = display.newImage(“chicken.png”)
chicken.myName = “chick”
physics.addBody(chicken, “static”,
{density=0, shape=shape1},
{density=0, shape=shape2}
)
what if you want to remove the chicken physics body and then re-assign its name - local chicken -
to a new physics body with different body elements and picture? [import]uid: 43696 topic_id: 9563 reply_id: 34976[/import]
What if you simply remove the chicken then create it again in the same spot using a different image and assign different physics? I’m still pretty new at this but if you provide more info on what you are trying to do maybe someone else can chime in? [import]uid: 31262 topic_id: 9563 reply_id: 35002[/import]
So how is that code interacting with the chicken image?
I don’t see it.
The chicken is just supposed to be removed if hit by an arrow.
I just don’t know how to use the collision stuff.
[import]uid: 54001 topic_id: 9563 reply_id: 35012[/import]
Wait a minute, so is aaaron’s code right or wrong? [import]uid: 54001 topic_id: 9563 reply_id: 35038[/import]
you can try something like this:
note tested yet:
[lua]physics = require “physics”
physics.start()
local box1
local box2
local onCollide = function (self, event)
if event.phase == “began” then
– if you want to remove the bullet then add self:removeSelf
– self:removeSelf()
event.other:removeSelf()
end
end
box1 = display.newRect(0,0,50,50)
box1:setFillColor(200,50,0)
box1.x = 160
box1.y = 20
physics.addBody(box1, “dynamic”, {density = 1, bounce = 1})
box1.collision = onCollide
box1:addEventListener(“collision”, box1)
box2 = display.newRect(0,0,320,50)
box2:setFillColor(0,100,150)
box2.x = 160
box2.y = 380
physics.addBody(box2, “static”, {density = 1, bounce = 0.5})[/lua]
check out my game:
Touch Cannon – a **FREE** game
please rate
[import]uid: 12455 topic_id: 9563 reply_id: 35051[/import]
I’d also highly recommend looking at http://www.adrianherbez.net/classes/corona_demo/
That has all the answers! [import]uid: 45932 topic_id: 9563 reply_id: 35052[/import]
Sorry supercyclone77 I should have put up the full code so to be less confusing. Basically I am trying to say what teex84 is saying
local chicken= display.newImage( "chicken.png" )
--listener to remove chicken
local function removeChicken( event )
if ( event.phase == "began" ) then
display.remove( event )
end
end
--this is how the listener interacts with the chicken and knows to call removeChicken upon collision
chicken.collision = removeChicken
chicken:addEventListener("collision", chicken)
This is very simple code and will remove the chicken once anything collides with it including other chickens.
If you want to filter it out so the chicken disappears only with collisions with the arrow you will have to use something like what jmp909 says in this thread:
http://developer.anscamobile.com/forum/2011/02/21/detect-specific-object-collisions
[import]uid: 31262 topic_id: 9563 reply_id: 35075[/import]
Actually the main problem is that I’m using an array.
For example, coins…here is the code:
[lua]–Whats the issue?
local function spawnCoinz()
for i = 1, 40, 1 do
coine[i] = display.newImage(“coin.png”)
coine[i].x = 300
coine[i].y = 100
physics.addBody( coine[i], coineBody )
coins[i] = display.newImage(“Coin2.png”)
coins[i].x = 150
coins[i].y = 100
physics.addBody( coins[i], coinsBody )
end
end
local function removeCoin( event )
if ( event.phase == “began” ) then
display.remove( event )
end
end
coine[i].collision = removeCoin
coine[i]:addEventListener(“collision”, coine[i])
coins[i].collision = removeCoin
coins[i]:addEventListener(“collision”, coins[i])
[import]uid: 54001 topic_id: 9563 reply_id: 35129[/import]
. [import]uid: 43696 topic_id: 9563 reply_id: 35076[/import]
This should do it…
[code]
local mario – Lets say you want the coins to be removed when they collide with mario (which should be initialized earlier)
local function spawnCoinz()
– self refers to the coin
local function removeCoin( self, event )
if ( event.other == mario ) then
self:removeSelf()
end
end
for i = 1, 40, 1 do
coine[i] = display.newImage(“coin.png”)
coine[i].x = 300
coine[i].y = 100
physics.addBody( coine[i], coineBody )
coine[i].collision = removeCoin
coine[i]:addEventListener(“collision”, coine[i])
coins[i] = display.newImage(“Coin2.png”)
coins[i].x = 150
coins[i].y = 100
physics.addBody( coins[i], coinsBody )
coins[i].collision = removeCoin
coins[i]:addEventListener(“collision”, coins[i])
end
end
[/code] [import]uid: 13780 topic_id: 9563 reply_id: 35440[/import]