Accelerating an object

My intent in the code below was to touch the right side of the screen and have the ship object’s velocity slowly increase (accelerate) until it reaches a max velocity of 200. That velocity would be held until the touch event ends.

What is happening is that when the screen is touched, the ship/screen freezes, the print statement counts off the velocity from 1 - 200, and only after vx reaches 200 does the ship/screen unfreeze and the ships moves at 200.

Maybe I don’t understand exactly how the While-Loop works? Can someone help me with a method for gradually increasing the speed of my ship up to a defined maximum?

[lua]local onScreenTouch = function( event )

if event.phase == “began” then
if event.x > halfW then
vx, vy = ship:getLinearVelocity( )
while vx ~= 200 do
ship:setLinearVelocity( vx, vy )
vx = vx + 1
print("velocityX : "…vx)
end
else
ship:setLinearVelocity( -100, vy )
end

elseif event.phase == “ended” then
ship:setLinearVelocity( 0, vy )

end
end[/lua] [import]uid: 146966 topic_id: 30959 reply_id: 330959[/import]

Loops will execute once per frame, so that goes up to 200 really, really fast - what you’d want to do is something like this;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

halfW = display.contentWidth / 2
print (halfW)

local ship = display.newCircle( 160, 240, 20 )
ship:setFillColor(0, 0, 0)
physics.addBody(ship)

local onScreenTouch = function( event )

if event.phase == “began” then
if event.x > halfW then
vx, vy = ship:getLinearVelocity( )
shipTimer = timer.performWithDelay(10, function()
ship:setLinearVelocity( vx, vy )
vx = vx + 1
print("velocityX : "…vx)
end, 200)
else
ship:setLinearVelocity( -100, vy )
end

elseif event.phase == “ended” then
ship:setLinearVelocity( 0, vy )
if shipTimer then timer.cancel(shipTimer) shipTimer = nil end
end
end

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor(255,255,255)
bg:toBack()
bg:addEventListener(“touch”, onScreenTouch)[/lua]

That is plug and play so give it a go.

Peach :slight_smile: [import]uid: 52491 topic_id: 30959 reply_id: 123860[/import]

Thanks Peach!

After much playing around I have a deceleration code somewhat working:

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

halfW = display.contentWidth / 2
print (halfW)

local ship = display.newCircle( 160, 240, 20 )
ship:setFillColor(0, 0, 0)
physics.addBody(ship)

local onScreenTouch = function( event )

if event.phase == “began” then
print(“began!”)
if event.x > halfW then
vx, vy = ship:getLinearVelocity( )
shipTimer = timer.performWithDelay(10, function()
ship:setLinearVelocity( vx, vy )
vx = vx + 10
if vx > 200 then vx = 200 end
print("velocityX : "…vx)
end, 20)
if shipTimer2 then timer.cancel(shipTimer2) shipTimer = nil end
else
ship:setLinearVelocity( -100, vy )
if shipTimer2 then timer.cancel(shipTimer2) shipTimer = nil end
end

elseif event.phase == “ended” then
print(“ended!”)
vx, vy = ship:getLinearVelocity( )
shipTimer2 = timer.performWithDelay( 300, function() vx=vx/1.5 ship:setLinearVelocity( vx, vy ) if vx < 15 then vx = 0 end print("velocityX2 : " …vx) end, 8)
if shipTimer then timer.cancel(shipTimer) shipTimer = nil end
print("velocityX3 : " …vx)

end
end

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor(255,255,255)
bg:toBack()
bg:addEventListener(“touch”, onScreenTouch)[/lua] [import]uid: 146966 topic_id: 30959 reply_id: 123952[/import]

Loops will execute once per frame, so that goes up to 200 really, really fast - what you’d want to do is something like this;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

halfW = display.contentWidth / 2
print (halfW)

local ship = display.newCircle( 160, 240, 20 )
ship:setFillColor(0, 0, 0)
physics.addBody(ship)

local onScreenTouch = function( event )

if event.phase == “began” then
if event.x > halfW then
vx, vy = ship:getLinearVelocity( )
shipTimer = timer.performWithDelay(10, function()
ship:setLinearVelocity( vx, vy )
vx = vx + 1
print("velocityX : "…vx)
end, 200)
else
ship:setLinearVelocity( -100, vy )
end

elseif event.phase == “ended” then
ship:setLinearVelocity( 0, vy )
if shipTimer then timer.cancel(shipTimer) shipTimer = nil end
end
end

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor(255,255,255)
bg:toBack()
bg:addEventListener(“touch”, onScreenTouch)[/lua]

That is plug and play so give it a go.

Peach :slight_smile: [import]uid: 52491 topic_id: 30959 reply_id: 123860[/import]

Thanks Peach!

After much playing around I have a deceleration code somewhat working:

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

halfW = display.contentWidth / 2
print (halfW)

local ship = display.newCircle( 160, 240, 20 )
ship:setFillColor(0, 0, 0)
physics.addBody(ship)

local onScreenTouch = function( event )

if event.phase == “began” then
print(“began!”)
if event.x > halfW then
vx, vy = ship:getLinearVelocity( )
shipTimer = timer.performWithDelay(10, function()
ship:setLinearVelocity( vx, vy )
vx = vx + 10
if vx > 200 then vx = 200 end
print("velocityX : "…vx)
end, 20)
if shipTimer2 then timer.cancel(shipTimer2) shipTimer = nil end
else
ship:setLinearVelocity( -100, vy )
if shipTimer2 then timer.cancel(shipTimer2) shipTimer = nil end
end

elseif event.phase == “ended” then
print(“ended!”)
vx, vy = ship:getLinearVelocity( )
shipTimer2 = timer.performWithDelay( 300, function() vx=vx/1.5 ship:setLinearVelocity( vx, vy ) if vx < 15 then vx = 0 end print("velocityX2 : " …vx) end, 8)
if shipTimer then timer.cancel(shipTimer) shipTimer = nil end
print("velocityX3 : " …vx)

end
end

local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor(255,255,255)
bg:toBack()
bg:addEventListener(“touch”, onScreenTouch)[/lua] [import]uid: 146966 topic_id: 30959 reply_id: 123952[/import]