Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Please help! I'm trying to spawn 5 balls one by one from the sky and have them disappear as soon as they hit the ground or when they hit another user-controlled object that's on the ground. The good thing is that I can spawn the balls successfully as intended, but when they hit the ground (or the other user-controlled object on the ground), they don't disappear. I've been going through a ton of sample code since the past 2 days but I can't figure out how to do it. The game does run, but the debug terminal gives me an error saying: runtime error - attempt to index global 'self' . Here's the source code:

local randomBall = function()
    ball = display.newImage( "hardball.png" )
    ball.x = math.random (30, 450); ball.y = -20
    physics.addBody( ball, { density=2.9, friction=0.5, bounce=0.7, radius=24 } )
    local function whenHit (event)
        if(event.phase == "began") then
            self:removeSelf()       
        end
    end
    ball:addEventListener("collision", whenHit)
end

timer.performWithDelay( 500, randomBall, 5 )
share|improve this question
I don't know corona, but it looks like your event handler lacks a parameter for self. Look at this example. – bummzack Aug 19 '11 at 7:27
@bummzack Thanks much for your reply. It looks like when multiple physics objects of the same kind are created within a single function, they cannot be destroyed from within the same function, perhaps because the system recognizes each of the objects as the same one. I implemented some of the code fromthe link you provided. What happens now is, when I test it by doing this: if (event.phase == "began") then print("The ball has collided now"). It DOES show me that the object HAS collided. But... – Sam Shaw Aug 19 '11 at 16:08
...when I replace the print command with object.removeSelf() or object.isVisible = false, other balls start disappearing instead of the ones that are colliding with the ground. So it is partially working, perhaps I'm making a very small mistake? (This comment is a continuation of the previous one. Sorry but my comment exceeded the max number of characters, so I had to split it into 2 comments). – Sam Shaw Aug 19 '11 at 16:08
Maybe update your code above so that we can see your current solution? – bummzack Aug 19 '11 at 16:13
@bummzack It looks like this now after I updated it: local randomBall = function() ball = display.newImage( "hardball.png" ) ball.x = math.random (30, 450); ball.y = -20 physics.addBody( ball, { density=2.9, friction=0.5, bounce=0.7, radius=24 } ) local function onLocalCollision( self, event ) if(event.phase == "began") then self:removeSelf() --The above line doesn't work even I do ball:removeSelf() end end ball.collision = onLocalCollision ball:addEventListener( "collision", ball ) end timer.performWithDelay(500, randomBall, 5) – Sam Shaw Aug 19 '11 at 20:23

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.