Henok
Henok

Reputation: 29

rotating a physics object in lua

The problem I am having is rotating an object and moving on to the next scene in lua. I have a function that returns delta time as following:

local runtime = 0
local function getDeltaTime()
   local temp = system.getTimer()  
   local dt = (temp-runtime) / (1000/200)
   runtime = temp
   return dt
end

Then inside my createScene, I have frameUpdate() function as following:

local function frameUpdate()


     local dt = getDeltaTime()

    wood5.rotation = wood5.rotation -(1*dt)
    wood6.rotation = wood6.rotation -(1*dt)

end
Runtime:addEventListener( "enterFrame", frameUpdate )

wood5 and wood6 are initialized above with rotation values 90 and 0 respectively.

The issue here is when I am switching scenes using my "next scene" button. The two woods would rotate fine, but as soon as I hit the "next", "back", or "reset" button, it shows me an error and says "attempt to perform arithmetic on field 'rotation' (a nil value)" I tried printing the values of wood5.rotation and wood5.rotation, they were 90 and 0 the first time, but they became huge negative numbers later on. Something like :

-3430.9887695313
-3520.9877929688

. .etc

Upvotes: 2

Views: 2085

Answers (1)

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

Before scene change, call:

Runtime:removeEventListener( "enterFrame", frameUpdate )

and inside your function, check for the existance of the object as:

if(wood5~=nil)then
wood5.rotation = wood5.rotation -(1*dt)
end

Keep Coding.................... :)

Upvotes: 2

Related Questions