Hey all
Trying to learn to work with physics as the next step. Messing with a random little program but it’s bugging out when I change game states. Pretty much a static ball in center of screen and another ball controlled by device gravity, when the 2 balls collide, the gameState changes, but it’s bugging out when this happens. I imagine I need to remove the physics.body and then reset it when I press play again. Kind of lost on how to do that.
Any tips would be appreciated.
Thanks
This is the code I’m working with.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
-- Use this function to perform your initial setup
function setup()
playerX=WIDTH/6
playerY=HEIGHT/1.2
goalX = WIDTH/2
goalY = HEIGHT/2
ball = physics.body(CIRCLE,18)
ball.x = playerX
ball.y = playerY
ball.restitution = 0.5
--ball.info="A"
ball2 = physics.body(CIRCLE,9)
ball2.type = STATIC
ball2.x = goalX
ball2.y = goalY
ball2.restitution = 0
--ball2.info="B"
wall1 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
wall2 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
wall3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
wall4 = physics.body(EDGE, vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
wall1.type = STATIC
wall2.type = STATIC
wall3.type = STATIC
wall4.type = STATIC
wall1.restitution = 0.5
wall2.restitution = 0.5
wall3.restitution = 0.5
wall4.restitution = 0.5
fontSize(96)
fill(255, 0, 0, 255)
gameState=0
end
function touched(t)
if gameState==0 and t.state==BEGAN then
gameState=1
end
if gameState==2 and t.state==ENDED then
gameState=0
end
end
-- This function gets called once every frame
function draw()
if gameState==0 then
text("PLAY",WIDTH/2,HEIGHT/2)
end
if gameState==1 then
physics.gravity(Gravity)
background(0,0,0,255)
ellipse(ball.x,ball.y,ball.radius*2)
ellipse(ball2.x,ball2.y,ball2.radius*2)
if ball:testOverlap(ball2) then
gameState=2
end
end
if gameState==2 then
text("Try Again",WIDTH/2,HEIGHT/2)
end
end