ERROR: attempt to perform arithmetic on local 'beginPos' (a nil value)

Hi guys,

I’ve been trying to get this little piece of code to work but without any luck, this is the code:

local function turnCircle(event) local beginPos if event.phase == "began" then beginPos = level.rotation print(beginPos) elseif event.phase == "moved" then level.rotation = beginPos -(event.x - event.xStart) end return true end

It stores the begin rotation of the object and stores that in beginPos. But I keep getting the following error: 

ERROR: attempt to perform arithmetic on local ‘beginPos’ (a nil value)

The error points to the variable beginPos used in the “moved” if condition. I don’t get how it could be nil, I clearly declared it in the correct scope and I assign a value to it in the “began” condition.

Any thoughts?

Kind regards

Bram

You need to declare beginPos outside of and somewhere above the function. Currently, each time the listener is triggered, a new local variable called beginPos is set up. It does not persist beyond the scope of turnCircle.

I’d make this change:

local function turnCircle(event) local beginPos if event.phase == "began" then level.beginPos = level.rotation print(level.beginPos) elseif( level.beginPos and event.phase == "moved" ) then level.rotation = level.beginPos - (event.x - event.xStart) elseif( event.phase == "ended" ) then level.beginPos = nil end return true end

Notice:

  1. I store ‘beginPos’ as a field on the the variable on the level object.
  2. I check that it exists before I use it.  Why?  Just in case you get a “moved” without a “began”. Not seeing the rest of the code I can’t be sure whether this can happen in your case or not.
  3. I clear the variable on ended so I only operate on one that was created in ‘began’

PS - This calculation looks backwards on second glance:

level.beginPos - (event.x - event.xStart)

I think it should be:

level.beginPos + (event.x - event.xStart)

Hi Rob,

thanks that did the trick. I’d already tried Nick’s solution but it didn’t check the existence of the variable and that gave me some errors.

Thanks!

You need to declare beginPos outside of and somewhere above the function. Currently, each time the listener is triggered, a new local variable called beginPos is set up. It does not persist beyond the scope of turnCircle.

I’d make this change:

local function turnCircle(event) local beginPos if event.phase == "began" then level.beginPos = level.rotation print(level.beginPos) elseif( level.beginPos and event.phase == "moved" ) then level.rotation = level.beginPos - (event.x - event.xStart) elseif( event.phase == "ended" ) then level.beginPos = nil end return true end

Notice:

  1. I store ‘beginPos’ as a field on the the variable on the level object.
  2. I check that it exists before I use it.  Why?  Just in case you get a “moved” without a “began”. Not seeing the rest of the code I can’t be sure whether this can happen in your case or not.
  3. I clear the variable on ended so I only operate on one that was created in ‘began’

PS - This calculation looks backwards on second glance:

level.beginPos - (event.x - event.xStart)

I think it should be:

level.beginPos + (event.x - event.xStart)

Hi Rob,

thanks that did the trick. I’d already tried Nick’s solution but it didn’t check the existence of the variable and that gave me some errors.

Thanks!