I really need help ! - function touched and vecs

Help I’m really stuck ! I am just trying to get the left circle to move the square Left and the right circle to move the square right

     function setup() 

print("Hello World!")

x = 400 
y = 400 

o = 200 
p = 400 

c = 300 
b = 300 

end 

      function draw() 

background(40, 40, 50) 

ellip = ellipse(x,y,50,50) 
ellip2 = ellipse(o,p,50,50) 

strokeWidth(5) 

rect(c,b,50,50) 

end 

   function touched(ellip) 

       if ellip.state==BEGAN then 
          v1=vec2(ellip.x,ellip.y)
          if v1:dist(vec2(x,y))<50 then 

           c = c + 20 

                      end 
             end   
     end 

  function touched(ellip2) 

     if ellip2.state==BEGAN then 
        v1=vec2(ellip2.o,ellip2.p) 
        if v1:dist(vec2(o,p))<50 then 

        c = c - 20 
  
                 end 
        end 
end

@ewan Here’s your code changed to work.


function setup() 
    x = 400 
    y = 400     
    o = 200 
    p = 400     
    c = 300 
    b = 300 
end 

function draw() 
    background(40, 40, 50)     
    ellip1 = ellipse(x,y,50,50) 
    ellip2 = ellipse(o,p,50,50)     
    strokeWidth(5)     
    rect(c,b,50,50) 
end 

function touched(t) 
    if t.state==BEGAN then 
        v1=vec2(t.x,t.y)    -- touch x,y position
        -- check left ellipse
        if v1:dist(vec2(o,p))<50 then 
           c = c - 20 
        end 
        -- check right ellipse
        if v1:dist(vec2(x,y))<50 then 
           c = c + 20 
        end 
    end   
end

Omg thanks that was so quick I just signed up for codea talk a few hours ago because I kept struggling.

@ewan - first, you are making things very difficult for yourself by not indenting your code properly.

The problem is that ellipse is not an object, it is a drawing command. You need to put your values in a table instead, as in

ellip=vec2(x,y)
ellip2=vec2(o,p)

--then you can draw with
ellipse(x,y,50,50)

--and handle touches with ONE touched function 
function touched(t)
    if vec2(t.x,t.y):dist(ellip)<50 then 
        --do something
    elseif vec2(t.x,t.y):dist(ellip2)<50 then
        --do something
    end
end

This post might help you understand more about how Codea draws
https://coolcodea.wordpress.com/2013/03/11/how-codea-draws/

@ewan You’ll find plenty of help here. One suggestion is to format your code to make it easier to read which will help you find errors as your code gets bigger. Compare your formatting to mine.

I struggled too when I started, so I wrote a few eBooks to help future beginners. You can find them here

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

@Ignatz You must have changed your code because I don’t see a table anywhere.

With all your help I learnt how to expand it so you can use up and down ! .Also I’m gonna check out your ebooks Ignatz ( I think I’m gonna need them ).


function setup()
 
    x = 700
    y = 100 
       
    o = 600
    p = 100 
        
    c = 300 
    b = 300
    
    r = 200
    l = 150
    
    g = 200
    h = 50  
end

function draw()

    background(40, 40, 50)     
    ellip1 = ellipse(x,y,50,50) 
    ellip2 = ellipse(o,p,50,50)     
    ellip3 = ellipse(r,l,50,50)
    ellip4 = ellipse(g,h,50,50)
    strokeWidth(5)     
    rect(c,b,50,50) 
end 

function touched(t) 
    if t.state==BEGAN then
 

       v1=vec2(t.x,t.y)    -- touch x,y position
        -- check left ellipse
        if v1:dist(vec2(o,p))<50 then 
           c = c - 20 
        end 
        -- check right ellipse
        if v1:dist(vec2(x,y))<50 then 
           c = c + 20 
        end
           
        if v1:dist(vec2(r,l))<50 then
            b = b + 20
        end
               
        if v1:dist(vec2(g,h))<50 then
            b = b - 20
        end
                    
    end   
end

@dave1707 - I meant vec2 of course, just testing you :wink:

And to add to the advice, when you post code:

~~~
Fence it in
~~~

Yeah i had trouble with that - i kept trying to post it but it wasn’t all blue and the lines were all over the place.

@ewan Nice going, you’re on your way to bigger and better things. A suggestion, your variables, x,y,o,p,r,l,g,h could have been x1,y1,x2,y2,x3,y3,x4,y4 to match your ellip names. As you write larger code, try to make your variable name more meaningful.

I pretty much picked random letters of the keyboard but I know what you mean

saying ellip1 = ellipse(x,y,50,50) doesn’t put anything in ellip1, so there is no point doing this

just say ellipse(x,y,50,50)