[Resolved] onCollision issue

Why does this not work? I want when object1 collides with object2 (object2 is a table) it removes the item/element it collides with in the table. Can anyone help or point out what I am doing wrong? I get NO error, but the collision does not work

Thank you!

[lua] local function dropSeeds ()
local seeds = {}

for i = 1, 12 do
for j = 1, 10 do
seeds[i] = display.newImageRect(“assets/seed.png”, 12,12)
seeds[i].x = 30 + (i*20)
seeds[i].y = 350 - (j*20)
physics.addBody(seeds[i],“dynamic”, {density = 0.0, friction = 0.0, bounce = 0 } )
seeds[i].myName = “seeds[i]”;
seeds[i].isSensor = true;
–seeds[1].alpha = 0.2
end
end

end

dropSeeds ()

local function onCollision ( event )
if(event.object1.myName == “player1Avatar” and event.object2.myName == “seeds[i]”) then
event.object2:removeSelf();
seeds.alpha = 0.1
print (“Something please happen”)
end

end
Runtime:addEventListener(“collision”, onCollision)[/lua] [import]uid: 53149 topic_id: 32987 reply_id: 332987[/import]

Found the solution. Went about it a different way. [import]uid: 53149 topic_id: 32987 reply_id: 130969[/import]

Ran this using crate image from sample code, worked fine after moving seeds table outside of function. (Did get error on that one.)

Maybe worth looking at your player creation code.

Below is what I used which worked fine.

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )
–physics.setDrawMode ( “hybrid” )

local player = display.newImage(“crate.png”)
player.x, player.y = 160, 20
player.myName = “player1Avatar”
physics.addBody(player, “dynamic”)
player:applyForce(0,1)

local seeds = {}

local function dropSeeds ()
–local seeds = {}

for i = 1, 12 do
for j = 1, 10 do
seeds[i] = display.newImageRect(“crate.png”, 12,12)
seeds[i].x = 30 + (i*20)
seeds[i].y = 350 - (j*20)
physics.addBody(seeds[i],“dynamic”, {density = 0.0, friction = 0.0, bounce = 0 } )
seeds[i].myName = “seeds[i]”;
seeds[i].isSensor = true;
–seeds[1].alpha = 0.2
end
end

end

dropSeeds ()

local function onCollision ( event )
if(event.object1.myName == “player1Avatar” and event.object2.myName == “seeds[i]”) then
event.object2:removeSelf();
seeds.alpha = 0.1
print (“Something please happen”)
end

end
Runtime:addEventListener(“collision”, onCollision)[/lua]

Peach :slight_smile:

Edit: Written before above comment but posted after. Oops. Marking as resolved! [import]uid: 52491 topic_id: 32987 reply_id: 130970[/import]

(Oh and just FYI - “seeds[i]” as a string means all the seeds are called “seeds[i]” and is not the same as assigned each an individual name.) You may know this already but just wanted to post in case anyone is confused by this in the future. [import]uid: 52491 topic_id: 32987 reply_id: 130971[/import]

Found the solution. Went about it a different way. [import]uid: 53149 topic_id: 32987 reply_id: 130969[/import]

Ran this using crate image from sample code, worked fine after moving seeds table outside of function. (Did get error on that one.)

Maybe worth looking at your player creation code.

Below is what I used which worked fine.

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )
–physics.setDrawMode ( “hybrid” )

local player = display.newImage(“crate.png”)
player.x, player.y = 160, 20
player.myName = “player1Avatar”
physics.addBody(player, “dynamic”)
player:applyForce(0,1)

local seeds = {}

local function dropSeeds ()
–local seeds = {}

for i = 1, 12 do
for j = 1, 10 do
seeds[i] = display.newImageRect(“crate.png”, 12,12)
seeds[i].x = 30 + (i*20)
seeds[i].y = 350 - (j*20)
physics.addBody(seeds[i],“dynamic”, {density = 0.0, friction = 0.0, bounce = 0 } )
seeds[i].myName = “seeds[i]”;
seeds[i].isSensor = true;
–seeds[1].alpha = 0.2
end
end

end

dropSeeds ()

local function onCollision ( event )
if(event.object1.myName == “player1Avatar” and event.object2.myName == “seeds[i]”) then
event.object2:removeSelf();
seeds.alpha = 0.1
print (“Something please happen”)
end

end
Runtime:addEventListener(“collision”, onCollision)[/lua]

Peach :slight_smile:

Edit: Written before above comment but posted after. Oops. Marking as resolved! [import]uid: 52491 topic_id: 32987 reply_id: 130970[/import]

(Oh and just FYI - “seeds[i]” as a string means all the seeds are called “seeds[i]” and is not the same as assigned each an individual name.) You may know this already but just wanted to post in case anyone is confused by this in the future. [import]uid: 52491 topic_id: 32987 reply_id: 130971[/import]

@Peach - thanks for responding. I used a simpler method with the same result. I would post it, but I am one iPad at the mome. Thanks again.

Maybe you could answer this. Or maybe I need a fresh post. Here goes.

How do I get get a jerky or shaky screen in corona? Maybe shake for 3 seconds while vibrate goes off. Do I have to target a particular object or would it shake the entire screen? Almost like when an explosion goes off it shakes and vibrates things. Thanks Peach :slight_smile: [import]uid: 53149 topic_id: 32987 reply_id: 131154[/import]

@Peach - thanks for responding. I used a simpler method with the same result. I would post it, but I am one iPad at the mome. Thanks again.

Maybe you could answer this. Or maybe I need a fresh post. Here goes.

How do I get get a jerky or shaky screen in corona? Maybe shake for 3 seconds while vibrate goes off. Do I have to target a particular object or would it shake the entire screen? Almost like when an explosion goes off it shakes and vibrates things. Thanks Peach :slight_smile: [import]uid: 53149 topic_id: 32987 reply_id: 131154[/import]

You could have a group that is the “shaking” group and when your event that’s supposed to shake it happens, do some resetting of the position with a timer, something like this:
[lua]local shakeGroup=display.newGroup()
local sx, sy=shakeGroup.x, shakeGroup.y

local object1=display.newImage(“anImage.png”)
local object2=display.newImage(“anImage.png”)
local object3=display.newImage(“anImage.png”)
local object4=display.newImage(“anImage.png”)
local object5=display.newImage(“anImage.png”)
shakeGroup:insert(object1)
shakeGroup:insert(object2)
shakeGroup:insert(object3)
shakeGroup:insert(object4)
shakeGroup:insert(object5)

local function shake()
shakeGroup.x, shakeGroup.y=sx+math.random(-20, 20), sy+math.random(-20, 20)
end
local shakeTimer=timer.performWithDelay(30, shake, 50)
timer.performWithDelay(1500, function() if shakeTimer then timer.cancel(shakeTimer) end shakeGroup.x, shakeGroup.y=sx, sy end)[/lua]
[import]uid: 147322 topic_id: 32987 reply_id: 131218[/import]

You could have a group that is the “shaking” group and when your event that’s supposed to shake it happens, do some resetting of the position with a timer, something like this:
[lua]local shakeGroup=display.newGroup()
local sx, sy=shakeGroup.x, shakeGroup.y

local object1=display.newImage(“anImage.png”)
local object2=display.newImage(“anImage.png”)
local object3=display.newImage(“anImage.png”)
local object4=display.newImage(“anImage.png”)
local object5=display.newImage(“anImage.png”)
shakeGroup:insert(object1)
shakeGroup:insert(object2)
shakeGroup:insert(object3)
shakeGroup:insert(object4)
shakeGroup:insert(object5)

local function shake()
shakeGroup.x, shakeGroup.y=sx+math.random(-20, 20), sy+math.random(-20, 20)
end
local shakeTimer=timer.performWithDelay(30, shake, 50)
timer.performWithDelay(1500, function() if shakeTimer then timer.cancel(shakeTimer) end shakeGroup.x, shakeGroup.y=sx, sy end)[/lua]
[import]uid: 147322 topic_id: 32987 reply_id: 131218[/import]

Great post Caleb-

Re the vibrate part, be sure to add in system.vibrate()

Peach :slight_smile: [import]uid: 52491 topic_id: 32987 reply_id: 131311[/import]

Great post Caleb-

Re the vibrate part, be sure to add in system.vibrate()

Peach :slight_smile: [import]uid: 52491 topic_id: 32987 reply_id: 131311[/import]

@Caleb P thanks for the help. Sorry I’ve been working in Unity and totally forgot I had asked for help on this. This should do exactly what I’m after. [import]uid: 53149 topic_id: 32987 reply_id: 132083[/import]

@Caleb P thanks for the help. Sorry I’ve been working in Unity and totally forgot I had asked for help on this. This should do exactly what I’m after. [import]uid: 53149 topic_id: 32987 reply_id: 132083[/import]