Skip to main content

I've read other answers with the same title but to no avail. My problem looks simple but I can't figure out a way to deal with. Just started LUA a few days ago. Here, it prints "nam" so there is collision. However, display.remove(apple) does not work. And the removeSelf() gives an error that says "attempt to index global 'apple' (a nil value)". The only thing I want to happen to apple at collision is for it to disappear.

         function appleCollision(self, event)
    if event.phase == "began" then
        print("nam")
        --display.remove( apple )
        apple:removeSelf()
        apple = nil
    end
end


local apple = display.newImageRect( "apple.png", 65, 85 )
apple.x, apple.y = 460, -100
apple.rotation = 15
apple:addEventListener ( "collision", apple )
apple.collision = appleCollision
physics.addBody( apple, { density=1.0, friction=0.3, bounce=0.3 } )
function appleCollision(self, event)
  if event.phase == "began" then
    print("nam")
    --display.remove( apple )
    apple:removeSelf()
    apple = nil
  end
end


local apple = display.newImageRect( "apple.png", 65, 85 )
apple.x, apple.y = 460, -100
apple.rotation = 15
apple:addEventListener ( "collision", apple )
apple.collision = appleCollision
physics.addBody( apple, { density=1.0, friction=0.3, bounce=0.3 } )

I've read other answers with the same title but to no avail. My problem looks simple but I can't figure out a way to deal with. Just started LUA a few days ago. Here, it prints "nam" so there is collision. However, display.remove(apple) does not work. And the removeSelf() gives an error that says "attempt to index global 'apple' (a nil value)". The only thing I want to happen to apple at collision is for it to disappear.

         function appleCollision(self, event)
    if event.phase == "began" then
        print("nam")
        --display.remove( apple )
        apple:removeSelf()
        apple = nil
    end
end


local apple = display.newImageRect( "apple.png", 65, 85 )
apple.x, apple.y = 460, -100
apple.rotation = 15
apple:addEventListener ( "collision", apple )
apple.collision = appleCollision
physics.addBody( apple, { density=1.0, friction=0.3, bounce=0.3 } )

I've read other answers with the same title but to no avail. My problem looks simple but I can't figure out a way to deal with. Just started LUA a few days ago. Here, it prints "nam" so there is collision. However, display.remove(apple) does not work. And the removeSelf() gives an error that says "attempt to index global 'apple' (a nil value)". The only thing I want to happen to apple at collision is for it to disappear.

function appleCollision(self, event)
  if event.phase == "began" then
    print("nam")
    --display.remove( apple )
    apple:removeSelf()
    apple = nil
  end
end


local apple = display.newImageRect( "apple.png", 65, 85 )
apple.x, apple.y = 460, -100
apple.rotation = 15
apple:addEventListener ( "collision", apple )
apple.collision = appleCollision
physics.addBody( apple, { density=1.0, friction=0.3, bounce=0.3 } )
Source Link

LUA attempt to index global nil value

I've read other answers with the same title but to no avail. My problem looks simple but I can't figure out a way to deal with. Just started LUA a few days ago. Here, it prints "nam" so there is collision. However, display.remove(apple) does not work. And the removeSelf() gives an error that says "attempt to index global 'apple' (a nil value)". The only thing I want to happen to apple at collision is for it to disappear.

         function appleCollision(self, event)
    if event.phase == "began" then
        print("nam")
        --display.remove( apple )
        apple:removeSelf()
        apple = nil
    end
end


local apple = display.newImageRect( "apple.png", 65, 85 )
apple.x, apple.y = 460, -100
apple.rotation = 15
apple:addEventListener ( "collision", apple )
apple.collision = appleCollision
physics.addBody( apple, { density=1.0, friction=0.3, bounce=0.3 } )