Hey everyone, I am a new programmer into a high school intro to programming class and I’m trying to program a basic game that has a little guy who jumps and tries to hit stars; when he hits the stars, he scores. My major issue is that the falling stars are physics bodies and when they reach the bottom they’re supposed to go back to the top, but the velocity keeps increasing so after a bit they are too fast to even see, is there a way to reset velocity? Another issue I’m having is making the stars “disappear” by going back to the top after they have collided with the character. Right now, when the character hits a star, they both go back to the top and I don’t know how to avoid that. Any help?? Here’s my code:
-- Final
-- Use this function to perform your initial setup
function setup()
c1=physics.body(CIRCLE, 50)
c1.position=vec2(WIDTH/2,HEIGHT/2)
c2=physics.body(CIRCLE,50)
c2.x=WIDTH-math.random(WIDTH/8,WIDTH/2+140)
c2.y=HEIGHT
c2.speed=3
c3=physics.body(CIRCLE,50)
c3.x=0+math.random(0,WIDTH)
c3.y=HEIGHT+350
float3=physics.body(CIRCLE,50)
float3.x=WIDTH
float3.y=HEIGHT+700
float4=physics.body(CIRCLE,50)
float4.x=0
float4.y=HEIGHT+1400
space1=math.random(WIDTH/8,WIDTH/2+140)
space2=math.random(WIDTH/8,WIDTH/2+140)
space3=math.random(WIDTH/8,WIDTH/2+140)
space4=math.random(WIDTH/8,WIDTH/2+140)
timer=0
gravity=8
score=0
end
function draw()
background(140, 223, 220, 93)
sprite("Platformer Art:Water",WIDTH/2,50,WIDTH+100,150)
sprite("Planet Cute:Character Horn Girl",c1.x,c1.y,100,200)
timer=timer+DeltaTime
if timer > 0 then sprite("SpaceCute:Star", c2.x, c2.y,100) end
if timer>1 then sprite("SpaceCute:Star",c3.x,c3.y,200) end
if timer>2 then sprite("Tyrian Remastered:Evil Head",float3.x, float3.y,50) end
if timer>3 then sprite("Tyrian Remastered:Evil Head",float4.x, float4.y,50) end
if c2.y<2 then c2.y=HEIGHT c2.x=WIDTH-math.random(0,WIDTH+50) end
if c3.y<2 then c3.y=HEIGHT c3.x=math.random(0,WIDTH) end
if float3.y<2 then float3Y=HEIGHT float3.x=math.random(0,WIDTH) end
if float4.y<2 then float4Y=HEIGHT float4.x=math.random(0,WIDTH) end
font("Copperplate-Bold") fontSize(100) fill(180, 0, 255, 255)
text(score,WIDTH/2,HEIGHT-100)
end
function collide(c)
if c.state==BEGAN then score=score+1 print(score) c2.y=HEIGHT end
if c.state==BEGAN then score=score+1 print(score) c3.y=HEIGHT end
end
function touched(touch)
if CurrentTouch.x>WIDTH/2 and CurrentTouch.y < HEIGHT then
c1.linearVelocity=vec2(150,75)
else c1.linearVelocity=vec2(-150,75)
end
end