I’m trying to gave collision with object which has 3 elements, everything works when I check for “began” collision but “ended” fires on it’s own but when I check to register collision between two objects, nothing happen. If you have any ideas please let me know.
Here’s the code:
function newRock1()
local rock1 = display.newImageRect("art/rock1.png", 38, 20 )
rock1.myName = "ground"
rock1.myName2 = "obstacle"
physics.addBody( rock1, {density=rock1body.density, friction=rock1body.friction, bounce=rock1body.bounce, shape=rock1body},
{ isSensor = true, radius=20 },
{ isSensor = true, radius=32 })
local function delete( self, event )
if ( event.phase == "began" ) then
if event.selfElement == 1 and event.other.myName == "delete" then
print("obstacle removed")
self:removeEventListener( "collision", self )
self:removeSelf()
self = nil
elseif event.selfElement == 3 and event.other.myName == "player" then
rock1score = 1
print(rock1score)
elseif event.selfElement == 2 and event.other.myName == "player" then
rock1score = 2
print(rock1score)
elseif event.selfElement == 1 and event.other.myName == "player" then
rock1score = 0
print(rock1score)
end
elseif ( event.phase == "ended" ) then
if event.selfElement == 1 and event.other.myName == "player" then
print("Goodccc")
if rock1score == 1 then
print("Good")
score = score + 10
elseif rock1score == 2 then
print("great")
elseif rock1score == 0 then
print("miss")
end
end
end
end
rock1.collision = delete
rock1:addEventListener( "collision", rock1 )
return rock1;
end
when player collides with elements during “began” event all works fine, but at the “ended” nothing happening. The “ended” event fires on it’s own but nothing is happening between objects.
In fact, I just struggled with a similar problem in my game last night. I was not receiving proper collision events with a complex body. Then I realized that I had a “return true” at the end of one collision detection function which was killing/cancelling all further detection. Once I removed it, all collision events started working properly again, just that simple.
The humble “return” is both incredibly useful and potentially dangerous, especially in collisions. If you use it, remember that it outright ends the sensory detection in Corona. This is why you might be getting a “began” event but NOT an “ended” event. The “return” is preventing the ended event… at least that’s my guess.
Hi there,
cheers for the suggestions but unfortunately I have to return rock as it is being loaded from separate file. I hope this is a bug and it will get fixed fast as without that there is 10 times more work to make it working as I want it to.
This still might be core Corona behavior, not a bug. If you need to return “rock”, I think you can still do so. But then you must pull that internal “delete” function out and make it a separate function in “main.lua”, above whatever function calls “newRock1()” to (I assume) produce a rock?
This will (or should) allow you to return rock without exiting the entire function and possibly killing the collision detection. Also, it makes sense to have your collision function separate, not wrapped within the rock creation function.
I don’t guarantee this will work; it might be a bug indeed. But it’s worth a shot. Waiting for a bug fix could take a long time, unfortunately… bugs pile up and they must be logged, analyzed, and then prioritized by Ansca to fix.
And out of curiosity, is it absolutely necessary to load “rock” from a separate file? Maybe you have a very specific reason for this, but if all you’re doing is configuring a rock with a physics body, that could easily be a local function in “main.lua”.
you are checking for element involved in local collision to trigger print at event.phase == ended.
if you print out event.selfElement in the ended phase you will see it does not return a number corresponding to any element of your body compared to printing it in event.phase == began where the element numbers matches, which is why anything you have after
if event.selfElement == 1 and event.other.myName == “player” in event.phase == ended
does not work, the collision in the ended phase does not return the right element
I think there is a bug in complex body local collision in the ended phase [import]uid: 43696 topic_id: 10606 reply_id: 39105[/import]
I see what you mean, kofmes. This is quite possibly an issue/bug with *complex* bodies, not shape bodies. I didn’t inspect the code carefully enough to see what he’s doing here. I am definitely getting both “began” and “ended” phase detection with 1-polygon shapes, but I can’t vouch for bodies built from multiple polygons.
However, I will still caution others reading this to be careful with using “return true” in a collision function, as I outlined above.
Anyway, if a bug is not logged for this in the Bug Base already, who’s going to submit it?