Howdy, Stranger!

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

Touch: object

edited October 2013 in Questions Posts: 14

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()
if touch.ellipse

can you correct this code please?



  • edited October 2013 Posts: 577

    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

     if touch.x and touch.y ==  area = π x radius(2)
    --Code here
  • dave1707dave1707 Mod
    edited October 2013 Posts: 8,740

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

    function draw()      background(40,40,50)    -- clear the screen     fill(100,255,0,255)     -- set fill color     ellipse(200,100,100,100)    -- draw circle     fill(18,0,255,255)    -- set fill color     ellipse(100,200,100,100) -- draw circle end function touched(t)     if t.state==BEGAN then    -- screen was touched         v1=vec2(t.x,t.y)    -- set v1 to vec2 of touched point         if v1:dist(vec2(200,100))<50 then    -- check if v1 inside circle             sound(SOUND_JUMP, 26258)    -- play sound         end         if v1:dist(vec2(100,200))<50 then    -- check if v1 inside circle             sound(SOUND_SHOOT, 26258)    -- play sound         end     end     end
  • @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)

  • Posts: 577

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


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

    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.

  • dave1707dave1707 Mod
    Posts: 8,740

    @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.

  • IgnatzIgnatz Mod
    edited October 2013 Posts: 5,396

    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.

  • Posts: 577

    @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.

  • edited October 2013 Posts: 1,976

    @AnnSophie @dave1707 posted this a while ago:

    function setup() parameter.integer("a",1,400,200) parameter.integer("b",1,400,100) end function draw() background(40, 40, 50) xc=250 -- center of ellipse x axis yc=400 -- center of ellipse y axis fill(255) ellipse(xc,yc,a*2,b*2) -- draw ellipse x=CurrentTouch.x -- touched x value y=CurrentTouch.y -- touched y value -- determine if touched value is inside ellipse fill(255,0,0) if (y-yc)^2/b^2+(x-xc)^2/a^2 <= 1 then text("inside",xc,yc) end end

    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))

  • edited October 2013 Posts: 2,043

    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

    function inCircle(p, c, r)    -- p is the point, c is the center of the circle, and r is the radius
        local dist2 = (p.x - c.x)^2 + (p.y - c.y)^2
        return (dist2 < r^2)
  • Posts: 398

    @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:

    function inCircle(p,c,r)
        return (c:dist(p) < r)


  • edited October 2013 Posts: 1,976

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

  • dave1707dave1707 Mod
    Posts: 8,740

    @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.

        -- determine if touched value is inside ellipse
        if (y-yc)^2/b^2+(x-xc)^2/a^2 <= 1 then 
  • Posts: 398

    @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

  • Posts: 1,976

    @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.)

Sign In or Register to comment.