Runtime / remove object help?

I have a runtime event that moves an object:

local rotationDirection = 1
local _W = display.contentWidth / 2
local _H = display.contentHeight / 2

local function moveObject ( event )
if(object.x > _W - 100) then
object.x = object.x + 1 * rotationDirection
end

if(object.x >= _W + 100) then
rotationDirection = -1
elseif (object.x <= _W - 100) then
rotationDirection = 1
end
end
** I want to collide with the object and add 1 to the level count but when I try to remove the object or remove the runtime listener I get an error and the program crashes.

**Can someone help me with this?

local function collide1 ( event )
if event.phase == “began” then
print(“Collision 1 occuring”)
levelCount = levelCount + 1
Runtime:removeEventListener(“enterFrame”, moveObject)
object:removeSelf()
end
end

Runtime:addEventListener(“enterFrame”, moveObject) [import]uid: 42126 topic_id: 8751 reply_id: 308751[/import]

What is your error?

I’m trying to understand your enterFrame and it’s a little confusing to me.

[lua]-- put your collision here
local function collide1 ( event )
if event.phase == “began” then
print(“Collision 1 occuring”)
levelCount = levelCount + 1

Runtime:removeEventListener(“enterFrame”, moveObject)
object:removeSelf()
end
end

local function moveObject ( event )
– you probably don’t need this one…
if(object.x > _W - 100) then
object.x = object.x + 1 * rotationDirection
end

– instead do this
object.x = object.x + rotationDirection

if(object.x >= _W + 100) then
rotationDirection = -1
elseif (object.x <= _W - 100) then
rotationDirection = 1
end
end

Runtime:addEventListener(“enterFrame”, moveObject)
[import]uid: 12455 topic_id: 8751 reply_id: 31920[/import]

This is the error that I get when I try this

attempt to perform arithmetic on field ‘x’ (a nil value) [import]uid: 42126 topic_id: 8751 reply_id: 31933[/import]

ok. I finally got some time to work it out and this is a basic solution I came up with for you. Every game is a little different and demands different approaches so I hope this give you an idea.

[lua]physics = require (“physics”)
physics.start()
– define your objects ahead of time
local image1
local image2
local image3

– put your collision here
local onCollide = function (event)

if event.phase == “began” then
image1.isAlive = false
event.other:removeSelf()
image1:removeSelf()
Runtime:removeEventListener(“enterFrame”, moveOb)
end
end

– create your object here, the object that will receive the
– collision call must go after the collision event

– this is the green circle
image1 = display.newCircle(0,0,50)
image1.x = 160
image1.y = 100
image1.isAlive = true
image1:setFillColor(140,140,0,255)
image1.objectName = “image1”
image1.collision = onCollide
physics.addBody(image1, “static”, {density = 0.1})
image1:addEventListener(“collision”, onCollide)

– this is the purple box
image3 = display.newRect(0,0,50,50)
image3.x = 160
image3.y = 300
image3:setFillColor(255,0,200,255)
physics.addBody(image3, “dynamic”, {density = 0.1})

– this is the blue rectangle
image2 = display.newRect(0,0,320,40)
image2.x = 160
image2.y = 340
image2:setFillColor(0,255,255,255)
image2.objectName = “image2”
physics.addBody(image2, “static”, {density = 1})

– Note: the purple box is dynamic, but the circle is static
– I used the enterframe to move the circle manually just to give
– you can example

local moveOb = function ()
local speed = 2

if image1.isAlive then

image1.y = image1.y + speed
if image1.y > 360 then
image1.isAlive = false
image1:removeSelf()
Runtime:removeEventListener(“enterFrame”, moveOb)
end
end
end

Runtime:addEventListener(“enterFrame”, moveOb)
[import]uid: 12455 topic_id: 8751 reply_id: 31962[/import]