attempt to call method 'setLinearVelocity' (a nil value) Error

I’m trying to spawn in a simple rectangle object and just get it to move to the left but I can’t get Corona to recognize setLinearVelocity.  It keeps saying that it’s a nil value. I don’t known why that is.

local function createBarrier() local barrier = display.newRect(mainGroup, 0, 0, 20, 100) physics.addBody(barrier, "kinematic") barrier.myName = "barrier" barrier:setFillColor(0, 1, 0) barrier.x = display.contentWidth barrier.y = display.contentHeight - 270 barrier:setLinearVelocity( 50, 0) table.insert( barriers, barrier) end

was physics started by then?

I checked and realized my problem. I was calling this function right from the start via a timer.performWithDelay game loop, but I have it set to only start physics by tapping a ‘start’ button. I think I need to toggle the game loop timer on only after the start button is pressed.  Is there a way to toggle timers and listeners on and off?

Thanks

I recommend (strongly) that you start the physics engine at the very beginning of your app and only either pause/resume it or simply don’t apply it to objects.

Just trust me on this.

I did not realize that you could initialize the physics engine and then pause it.  After playing around with that, everything works (so far).  That was much easier than playing around with listeners.  Thanks!

I’ll mark this as solved.

was physics started by then?

I checked and realized my problem. I was calling this function right from the start via a timer.performWithDelay game loop, but I have it set to only start physics by tapping a ‘start’ button. I think I need to toggle the game loop timer on only after the start button is pressed.  Is there a way to toggle timers and listeners on and off?

Thanks

I recommend (strongly) that you start the physics engine at the very beginning of your app and only either pause/resume it or simply don’t apply it to objects.

Just trust me on this.

I did not realize that you could initialize the physics engine and then pause it.  After playing around with that, everything works (so far).  That was much easier than playing around with listeners.  Thanks!

I’ll mark this as solved.