It looks like you're new here. If you want to get involved, click one of these buttons!
I am fairly new to programming and have been working on a small game which involves an ellipse moving around the screen and capturing smaller ones. The only problem I am having with it is keeping the ball on the screen. It does not involve gravity. Is there any way i can make invisible walls on the side of the screen to set a boundary.
function setup()
-- this creates the circle
c1 = physics.body(CIRCLE,70)
c1.x=400
c1.y=400
c1.type=STATIC
end
function draw()
background(147, 119, 170, 255)
--this is what is shown as the circle
ellipse(c1.x, c1.y, 100, 100)
strokeWidth(10)
-- Do your drawing here
end
function touched(touch)
--makes sure the circle only moves where you drag it
if touch.state == MOVING then
c1.x = touch.x
c1.y = touch.y
end
end
--not finished
Comments
This entire thing seems weird, how are you having trouble with it going off screen if it can only follow your finger?
I just want the circle to remain on screen and not have half of it not showing. I just want it to look more professional
c1.x = math.max(math.min(touch.x, WIDTH - c1.radius), c1.radius)
c1.y = math.max(math.min(touch.y, HEIGHT - c1.radius), c1.radius)
That should do it.
@newow Try this.
I see, well you will have to get rid of the
Because that would allow it to go through walls, for example, lets say you have a brick wall in the middle of the screen, and the ball is on one side of it. If you tap on the other side of the wall, the ball will magically teleport over there. So you want to "add force" to the physic body, here is a example:
Basically, we are testing where the finger is around the ball, and adding a little bit of force to make it slowly move. Now to answer your question, to make the walls you would do something like this:
Thanks
Hopefully I don't have anymore problems with this