Apply a force to an object by tapping it?

So I have a basic object (just a ball)

local physics = require "physics" physics:start() physics.setGravity( 0, 0) local ball = display.newCircle( scene.perRunGroup, centerX, centerY - 150, 20 ) ball:setFillColor(0.5,0.5,0) physics.addBody( ball, "dynamic", { radius = 20 } )

I want the ball to shoot off in either North, South, East or West direction when you tap the ball, but never in the same direction twice in a row, for example, if you tap it and it goes North, the next tap will be in any other direction but north. Also is there a way that after each tap, the ball will increase in speed?

Cheers

-- set random math.randomseed( os.time() ) -- local physics = require "physics" physics:start() physics.setGravity( 0, 0) local ball = display.newCircle( display.contentCenterX, display.contentCenterY, 20 ) ball:setFillColor(0.5,0.5,0) physics.addBody( ball, "dynamic", { radius = 20 } ) local currentPos = "" local speedCur = 5 local speedInt = 5 ball:addEventListener( "tap", function ( ) -- 1= north, 2= south, 3= east, 4= west local myIndex if (currentPos == "") then myIndex = math.random( 1,4 ) elseif (currentPos == "north") then myIndex = math.random( 2,4 ) elseif (currentPos == "south") then myIndex = math.random( 2,4 ) if (myIndex == 2) then myIndex = 1 end elseif (currentPos == "east") then myIndex = math.random( 1,3 ) if (myIndex == 3) then myIndex = 4 end elseif (currentPos == "west") then myIndex = math.random( 1,3 ) end math.randomseed( os.time() ) if (myIndex== 1) then -- north ball:setLinearVelocity( 0, -speedCur, ball.x, ball.y ) currentPos = "north" elseif (myIndex== 2) then -- south ball:setLinearVelocity( 0, speedCur, ball.x, ball.y ) currentPos = "south" elseif (myIndex== 3) then -- east ball:setLinearVelocity( speedCur, 0, ball.x, ball.y ) currentPos = "east" elseif (myIndex== 4) then -- west ball:setLinearVelocity( -speedCur, 0, ball.x, ball.y ) currentPos = "west" end speedCur = speedCur + speedInt end )
-- set random math.randomseed( os.time() ) -- local physics = require "physics" physics:start() physics.setGravity( 0, 0) local ball = display.newCircle( display.contentCenterX, display.contentCenterY, 20 ) ball:setFillColor(0.5,0.5,0) physics.addBody( ball, "dynamic", { radius = 20 } ) local currentPos = "" local speedCur = 5 local speedInt = 5 ball:addEventListener( "tap", function ( ) -- 1= north, 2= south, 3= east, 4= west local myIndex if (currentPos == "") then myIndex = math.random( 1,4 ) elseif (currentPos == "north") then myIndex = math.random( 2,4 ) elseif (currentPos == "south") then myIndex = math.random( 2,4 ) if (myIndex == 2) then myIndex = 1 end elseif (currentPos == "east") then myIndex = math.random( 1,3 ) if (myIndex == 3) then myIndex = 4 end elseif (currentPos == "west") then myIndex = math.random( 1,3 ) end math.randomseed( os.time() ) if (myIndex== 1) then -- north ball:setLinearVelocity( 0, -speedCur, ball.x, ball.y ) currentPos = "north" elseif (myIndex== 2) then -- south ball:setLinearVelocity( 0, speedCur, ball.x, ball.y ) currentPos = "south" elseif (myIndex== 3) then -- east ball:setLinearVelocity( speedCur, 0, ball.x, ball.y ) currentPos = "east" elseif (myIndex== 4) then -- west ball:setLinearVelocity( -speedCur, 0, ball.x, ball.y ) currentPos = "west" end speedCur = speedCur + speedInt end )