Attempt to perform arithmetic on a nil value

Hello everyone,
I recently posted about the error that I kept receiving and now i think i know the reason behind it but still i don’t know how to fix it.

So this is what i think the error is caused by:

The only time that i receive the error is when i drag the my mouse from the screen over the object that i want to move. So when the object is touched it is in the event.phase==moved section as the touch began before the object being touched. Therefore the code in the begin section where startMoveX is defined is not processed and so in the moved section it is nil.

In the codes below ive also indicated the line of the error.

I don’t know how to fix this error but i think that’s the reason. 

local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) audio.play (selectionSound) display.remove( rotationalert ) rotationalert = nil platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y elseif (event.phase == "moved") then -- The line below is the 392th line, where the error arises: platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) end return true end

I have also attached a picture of the error as well as a video of when I get the error.
 

Every help would be greatly appreciated.

How about putting this at the top of moved phase:

[lua]

if (platformTouched.startMoveX == nil) then

  platformTouched.startMoveX = platformTouched.x

  platformTouched.startMoveY = platformTouched.y

end

[/lua]

Alternatively you might want to disable dragging if the touch started away from the object by only doing the code in ‘moved’ phase if platformTouched.startMoveX ~= nil.

I also think you’ll need to reset startMoveX and startMoveY to nil in the ended phase.

Hi mparvareshnia80,

I slightly modified your code. It works on my machine on Win7:)

local function movePlatform( event )     platformTouched = event.target     if event.phase == "began" then         display.getCurrentStage():setFocus( platformTouched )         platformTouched.isFocus = true      audio.play ( selectionSound )      display.remove( rotationalert )      rotationalert = nil      platformTouched.startMoveX = platformTouched.x      platformTouched.startMoveY = platformTouched.y     elseif platformTouched.isFocus then         if event.phase == "moved" then             -- The line below is the 392th line, where the error arises:             platformTouched.x = ( event.x - event.xStart ) + platformTouched.startMoveX             platformTouched.y = ( event.y - event.yStart ) + platformTouched.startMoveY         elseif event.phase == "ended" or event.phase == "cancelled" then             display.getCurrentStage():setFocus( nil )             platformTouched.isFocus = false         end     end      return true end

Note: You need modify code if you want use multitouch. See second link from list below.

Read more :

Have a nice day:)

ldurniat

Thank you so much for the help.

My problem got solved thanks to you guys.

How about putting this at the top of moved phase:

[lua]

if (platformTouched.startMoveX == nil) then

  platformTouched.startMoveX = platformTouched.x

  platformTouched.startMoveY = platformTouched.y

end

[/lua]

Alternatively you might want to disable dragging if the touch started away from the object by only doing the code in ‘moved’ phase if platformTouched.startMoveX ~= nil.

I also think you’ll need to reset startMoveX and startMoveY to nil in the ended phase.

Hi mparvareshnia80,

I slightly modified your code. It works on my machine on Win7:)

local function movePlatform( event )     platformTouched = event.target     if event.phase == "began" then         display.getCurrentStage():setFocus( platformTouched )         platformTouched.isFocus = true      audio.play ( selectionSound )      display.remove( rotationalert )      rotationalert = nil      platformTouched.startMoveX = platformTouched.x      platformTouched.startMoveY = platformTouched.y     elseif platformTouched.isFocus then         if event.phase == "moved" then             -- The line below is the 392th line, where the error arises:             platformTouched.x = ( event.x - event.xStart ) + platformTouched.startMoveX             platformTouched.y = ( event.y - event.yStart ) + platformTouched.startMoveY         elseif event.phase == "ended" or event.phase == "cancelled" then             display.getCurrentStage():setFocus( nil )             platformTouched.isFocus = false         end     end      return true end

Note: You need modify code if you want use multitouch. See second link from list below.

Read more :

Have a nice day:)

ldurniat

Thank you so much for the help.

My problem got solved thanks to you guys.