When touching the tip of my main object in my game I get the error: Attempt to perform arithmetic on field 'touchOffsetX' (a nil value)?

This is basically the last and final piece of the puzzle I need to finish the game I’m trying to make. So I used the template code from the corona spaceship tutorial and made modifications and everything is working except sometimes I get the error: Attempt to perform arithmetic on field ‘touchOffsetX’ (a nil value). I’ve noticed that this only happens when the object that is drag-able is touched near it’s edge, otherwise everything usually works fine. Here’s the piece of code having the problem:

local function dragCircle( event ) local circle = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the circle display.currentStage:setFocus( circle ) circle.touchOffsetX = event.x - circle.x circle.touchOffsetY = event.y - circle.y elseif ("moved" == phase) then circle.x = event.x - circle.touchOffsetX circle.y = event.y - circle.touchOffsetY elseif ("ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus(nil) end return true end

Here’s some code on the actual object (the circle):

circle = display.newImageRect( mainGroup, "circle.png", 15, 15) circle.myName = "circle" physics.addBody(circle, {isSensor=true, radius=7.5}) circle.gravityScale = 0 circle.x = display.contentCenterX circle.y = display.contentHeight-25

and the event listener located in the scene:show:

circle:addEventListener("touch", dragCircle)

I tried a few solutions like changing the radius, but I just don’t get why touching the outer edge of the circle would cause this error, assuming that is the cause, unless I’m totally wrong. Thanks again in advance!

I think your touch listener is wrong, this should work better:

local function dragCircle( event ) local circle = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( circle, event.id ) circle.isFocus = true circle.touchOffsetX = event.x - circle.x circle.touchOffsetY = event.y - circle.y elseif( circle.isFocus ) then if ("moved" == phase) then circle.x = event.x - circle.touchOffsetX circle.y = event.y - circle.touchOffsetY elseif ("ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( circle, nil ) circle.isFocus = false end end return true end

Having said that, using a local listener would be way better.

local function onTouch( self, event ) local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( self, event.id ) self.isFocus = true self.touchOffsetX = event.x - self.x self.touchOffsetY = event.y - self.y elseif( self.isFocus ) then if ("moved" == phase) then self.x = event.x - self.touchOffsetX self.y = event.y - self.touchOffsetY elseif ("ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( self, nil ) self.isFocus = false end end return true end

circle.touch = onTouch circle:addEventListener("touch")

Roaminggamer you’ve helped me once again, thanks!! :smiley:

I think your touch listener is wrong, this should work better:

local function dragCircle( event ) local circle = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( circle, event.id ) circle.isFocus = true circle.touchOffsetX = event.x - circle.x circle.touchOffsetY = event.y - circle.y elseif( circle.isFocus ) then if ("moved" == phase) then circle.x = event.x - circle.touchOffsetX circle.y = event.y - circle.touchOffsetY elseif ("ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( circle, nil ) circle.isFocus = false end end return true end

Having said that, using a local listener would be way better.

local function onTouch( self, event ) local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( self, event.id ) self.isFocus = true self.touchOffsetX = event.x - self.x self.touchOffsetY = event.y - self.y elseif( self.isFocus ) then if ("moved" == phase) then self.x = event.x - self.touchOffsetX self.y = event.y - self.touchOffsetY elseif ("ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( self, nil ) self.isFocus = false end end return true end

circle.touch = onTouch circle:addEventListener("touch")

Roaminggamer you’ve helped me once again, thanks!! :smiley: