Move Phase Touch Error

Hi all, I am new to Corona and am trying to get a hang of why I am getting certain errors.

I have a game that involves dragging an object around the screen, but the touch does not have to occur on the object directly. After reading through other posts I am able to drag the object by directly touching it, but when I try to implement code to do so when touching a different part of the screen I get the error “attempt to perform arithmetic on field ‘markX’ (a nil value)”

Here is my code, which is set in the scene:create(event) space.

local ball = display.newCircle(WIDTH\*.2,HEIGHT\*.2,WIDTH/80) physics.addBody(ball,{density=1.0, friction=0.5, bounce=0.4, radius=WIDTH/80, filter = ballCollisionFilter}) local touchSensor = display.newRect(0,0,WIDTH,HEIGHT) touchSensor.isVisible = false touchSensor.isHitTestable = true local function ballTouch(event) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y elseif event.phase =="moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY ball.x, ball.y = x, y end return true end touchSensor:addEventListener("touch", ballTouch) 

I am not sure why this error occurs as the self.markX and self.markY variables work when the touch event is on the “ball” object itself.

Thanks in advance for any replies.

 local function ballTouch( self, event) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y elseif event.phase =="moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY ball.x, ball.y = x, y end return true end touchSensor.touch = ballTouch touchSensor:addEventListener("touch")

roaminggamer, thanks for the solution. Sorry for my slow response I created more issues on my own I had to resolve before I could implement your help.

 local function ballTouch( self, event) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y elseif event.phase =="moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY ball.x, ball.y = x, y end return true end touchSensor.touch = ballTouch touchSensor:addEventListener("touch")

roaminggamer, thanks for the solution. Sorry for my slow response I created more issues on my own I had to resolve before I could implement your help.