-- 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 )