Strange behaviour, maybe a bug

Hi all

I’ve spent all day trying to find a bug in my code and I’ve come to the conclusion that there can’t be one, yet this glitch persists.

I’m rotating a wheel in a continuous loop.

local rotWheel,wheelTransition local resWheel = function() wheel.rotation = 0 transition.cancel(wheelTransition) wheelTransition = nil rotWheel() end rotWheel = function() wheelTransition = transition.to( wheel, { rotation=359, time=8000, onComplete = resWheel }) end rotWheel()

The wheel happily goes round and round all day.

But, for some odd reason the wheel stops when a totally unconnected collision occurs elsewhere.

I’ve checked over and over the code, none of the variables in the above code are used anywhere else (on purpose) so I’m perplexed.

I put in some print statements and the rotWheel function just stops being called for no reason.

I thought maybe because I’m starting and pausing the physics engine that might be the cause so I commented out that code and it made no difference.

Has anybody seen this before or helped someone else with this and can offer me some advice?

I’m using Windows build 2014.2511

Hi @QuizMaster,

One thing I should ask is, why are you rotating the wheel by a transition if it’s a physics object? Why not just set it rotating via physics and never worry about the transitions?

Brent

Hi Brent

The thing is, I don’t need to detect collisions on the wheel hence the transition.

I started commenting out my code line by line and found the culprits but it makes no sense.

Here’s one of them.

for i = 1, ballTran do if ballTransition[i] ~= nil then transition.cancel(ballTransition[i]) ballTransition[i] = nil end end ballTransition = {}

I’m clearing the memory here because the ball transition is called repeatedly.

This is the loop so you can see why it’s best to clear ballTransition table, it would get very big very quickly.

rotBall = function() if trayHit == 0 then ang = ang - .2 br = br - 1 if br == 120 then physics.start() end ballTransition[ballTran] = transition.to( ball, { x = cx + br \* math.cos(ang), y = cy + br \* math.sin(ang), time=50 }) ballTran = ballTran + 1 else if ballTimer then timer.cancel( ballTimer ) ballTimer = nil end end end ballTimer = timer.performWithDelay( 50, rotBall, -1 ) ball:addEventListener( "collision", ball )

So to me it appeared that the collision was triggering the error but in fact it was clearing the ballTransition table.

When I don’t clear the table I don’t get the error with the wheel stopping all by itself.

I’m still confused how two unconnected transitions can affect each other like this.

@quizmaster,

The way you’ve written your (first) sample, it is very open to issues.  Try this re-write:

local onComplete = function( self ) self.rotation = 0 wheelTransition = transition.to( self, { rotation=360, time=8000, onComplete = self }) end wheel.onComplete = onComplete wheelTransition = transition.to( wheel, { rotation=360, time=8000, onComplete = wheel })

This way you know the right object is responding to and being manipulated by the ‘onComplete’ event.  It is much tighter.  Also, no need to cancel the transition in onComplete.  It is done.

PS - Notice I made it 360, not 359.  This will be smoother at slow speeds.

@roaminggamer

Thanks, I implemented your code and it works a treat.

Thanks again.

Hi @QuizMaster,

One thing I should ask is, why are you rotating the wheel by a transition if it’s a physics object? Why not just set it rotating via physics and never worry about the transitions?

Brent

Hi Brent

The thing is, I don’t need to detect collisions on the wheel hence the transition.

I started commenting out my code line by line and found the culprits but it makes no sense.

Here’s one of them.

for i = 1, ballTran do if ballTransition[i] ~= nil then transition.cancel(ballTransition[i]) ballTransition[i] = nil end end ballTransition = {}

I’m clearing the memory here because the ball transition is called repeatedly.

This is the loop so you can see why it’s best to clear ballTransition table, it would get very big very quickly.

rotBall = function() if trayHit == 0 then ang = ang - .2 br = br - 1 if br == 120 then physics.start() end ballTransition[ballTran] = transition.to( ball, { x = cx + br \* math.cos(ang), y = cy + br \* math.sin(ang), time=50 }) ballTran = ballTran + 1 else if ballTimer then timer.cancel( ballTimer ) ballTimer = nil end end end ballTimer = timer.performWithDelay( 50, rotBall, -1 ) ball:addEventListener( "collision", ball )

So to me it appeared that the collision was triggering the error but in fact it was clearing the ballTransition table.

When I don’t clear the table I don’t get the error with the wheel stopping all by itself.

I’m still confused how two unconnected transitions can affect each other like this.

@quizmaster,

The way you’ve written your (first) sample, it is very open to issues.  Try this re-write:

local onComplete = function( self ) self.rotation = 0 wheelTransition = transition.to( self, { rotation=360, time=8000, onComplete = self }) end wheel.onComplete = onComplete wheelTransition = transition.to( wheel, { rotation=360, time=8000, onComplete = wheel })

This way you know the right object is responding to and being manipulated by the ‘onComplete’ event.  It is much tighter.  Also, no need to cancel the transition in onComplete.  It is done.

PS - Notice I made it 360, not 359.  This will be smoother at slow speeds.

@roaminggamer

Thanks, I implemented your code and it works a treat.

Thanks again.