It looks like you're new here. If you want to get involved, click one of these buttons!

There are two different circles.

If the user clicks at circle 1, codea plays a sound

If the user clicks at circle 2, codea plays another sound.

```
function draw()
ellipse(200,100,100,100)
fill(100,255,0,255)
ellipse(200,100,100,100)
fill(18,0,255,255)
if touch.ellipse
then
sound.shoot
else
sound.jump
end
```

can you correct this code please?

## Comments

Codea does not know what the ellipse is. I'm unfamiliar with using ellipses, as I've only used them once, but you would have to tell touch

@AnnSophie Try this. You had both ellipses in the same spot, so I moved one of them.

@dave1707 Can you explain your code please?

I think the code is to difficult for children.

I search little games for learning the if structure... (Children)

@AnnSophie I think your trying to make a button, correct? It would be easier if it was a rectangle or a square

Example:

if math.abs(touch.x-xCoordinateOfButton)<WidthOfButton/2 and math.abs(touch.y-yCoordinateOfButton)<HeightOfButton/2

then

print("Works")

end

Here is an explanation: we are checking the absolute value of the touch.x and the x coordinate of the button are within the Width of the shape, same for the y.

@AnnSophie I added comments to the code after I first posted it, but I'll go into more detail anyways. background clears the screen and set the screen color to the value specified. fill sets the color that will fill the ellipse. ellipse draws a circle or ellipse at the x,y coordinates. If the size values are equal or if just one size is specified, a circle will be drawn. If the size values are different, then an ellipse will be drawn. The touched function checks if and where the screen is touched. The state variable has 3 states, BEGAN, MOVING, ENDED. Code can be written based on if your finger first touches the screen (BEGAN), you finger is moving on the screen(MOVING), or you lifted you finger from the screen (ENDED). Setting v1 to a vec2 variable of where the screen was touched ( t.x , t.y) allows me to calculate how far from the center of the circle the screen was touched (v1:dist). The dist function basically does the calculation sqrt of (x^2+y^2). If the result is less than that radius of the circles, then the screen was touched inside of the circle. So depending on which circle you touch, a different sound will be heard. I'm not sure if Codea is easy enough for children who have no experience with programming. On the other hand, if they are taught the basics and start to understand how things work, then maybe they'll like it. Otherwise, it can be very frustrating.

AnnSophie - I have some experience of teaching Excel VBA, which also has a language and a screen to put things on (the worksheet).

I found the best way was not to teach both at once, but to start with the language (ie no pictures, just printing out text results), and when they can do simple programs, then start with pictures. Even for myself, when I learned Codea, I started with Lua first.

It is like learning music. Before you can play anything, you need to learn some basic skills and some simple rules.

I'll have a think about it and get back to you. The problem, of course, is that learning the language first is not very exciting, if you thought you were going to be drawing pictures.

@Ignazts Perhaps they could make some text adventure role playing games in lua? When I was learning lua, That kept me entertained longer then you would imagine.

@AnnSophie @dave1707 posted this a while ago:

Detecting if a coordinate is in a circle is hard. I'm still trying to figure this out... (^ in Lua is essentially math.pow(a, b))

Here's some simple code to check if a point is in a circle

Give it the point you are checking, the center of the circle, and the radius of the circle

It will return true or false

@JakAttak - no need to do the trig, just use the built in vec2:dist method to work out the length of the vector between 2 points:

;-)

@andymac3d But still, if you want to see the actual code and maths in that function...

@SkyTheCoder In your above post you said "Detecting if a coordinate is in a circle is hard. I'm still trying to figure this out...". The formula that I gave for an ellipse (see below) also works for a circle. A circle is just an ellipse with the "a" and "b" lengths being equal to the radius. Or do you mean that you're trying to figure out why the below formula works at detecting a point being inside.

@SkyTheCoder - I know what your saying. However, I do think its a shame to 're-invent the wheel' sometimes if there is a built in function that is faster and a more elegant solution to a problem.

This is certainly the case when optimising for speed and/or simplicity. :-D

@dave1707 I know that it works for ellipses and circles, what I meant by that is that I'm confused by the maths there a little bit... I need to visualize it, not just see the math... So I wrote a program using that technique to draw an image of a circle, with the brightness being the distance. (Something cool: replace ^2 with ^3 or ^4 to get a round rect.)