I’ve been working on this game for some time and I can’t seem to make it so when I touch one of the balls it give me a point. So far I only get a point when the balls touch each other. How can I fix this…I’m really new to this language so I have no real idea of what to do.
function setup()
x=10
y=100
radius=30
speedX=20
speedY=25
x2=100
y2=350
radius2=30
speedX2=20
speedY2=25
score=0
end
function draw()
distance=math.sqrt((x-x2)^2+(y-y2)^2)
if distance<=(radius+radius2) then
speedX=-speedX
speedX2=-speedX2
speedY=-speedY
speedY2=-speedY2
score=score+1
print("point",score)
function collide(contact)
if contact.state == BEGAN then
print("HIT!")
end
end
end
background(255, 90, 0, 255)
fill(0, 52, 255, 255)
x=x+speedX
if x-radius<0 or x+radius>=WIDTH then
speedX=-speedX
end
y=y+speedY
if y-radius<0 or y+radius>=HEIGHT then
speedY=-speedY
end
ellipse(x,y,radius*2)
fill(98, 255, 0, 255)
ellipse(x2,y2,radius2*2)
x2=x2+speedX2
if x2-radius2<0 or x2+radius2>=WIDTH then
speedX2=-speedX2
end
y2=y2+speedY2
if y2-radius2<0 or y2+radius2>=HEIGHT then
speedY2=-speedY2
end
end
This is what I have so far
@KatzNDA First, when you post code, put 3~s on a line before and after your code so it show properly. Click on the gear icon at the upper right of your code and select edit and look at the ~'s I added. Second, the function collide should be by itself and not in the draw function. Third, if you want to touch the balls, you need to use the touched function. See the build in reference for its use.
@KatzNDA - have you read any tutorials on Lua (the language behind Codea)? Do you know the main Lua functions, and how tables work?
If not, you aren’t ready to program a game. You can’t learn a programming language from scratch, by making a game. And we don’t have time to hold your hand every time you have a problem which you could have avoided by doing some reading.
When I started with Codea, I spent weeks just reading about it before I started making anything with it - and I already had programming experience.
This is for a class I don’t have a choice in the matter
@KatzNDA If that’s the case, then look thru some of the other posts for examples of code to see how the functions are used. The basic functions are setup(), draw(), touched(), collide().
@KatzNDA - ok, I’m not trying to be hard on you. We are happy to help fix problems, but it’s just that we have lots of kids who buy Codea and want to have an app on the store next week, and expect us to write most of the code for them.
I suggest if it’s for a class, that you say that at the beginning, because we’ll understand. You should also tell us what your assignment is.
Looking at your code
-
You have a collide object, but it only works with physics objects (you have none)
-
you need a touched function to catch finger touches. This may help you understand how to use it.
If this makes no sense to you, please ask your teacher to explain.