Howdy, Stranger!

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

Moving an object via touch (but with a function)

edited November 19 in Questions Posts: 5
I know how do the touch attribute within the draw function, but how do I add it the touch attribute if I called a function within draw? For example:

```function draw()
drawCircle()
end

function drawCircle()
fill(100,100,100)
ellipse(WIDTH/2,HEIGHT/2,50,50)
end```

Where do I add the ```CurrentTouch.x, CurrentTouch.y``` In this instance? I thought it was just in the parentheses of ```drawCircle``` in the ```draw()``` function, but that didnt work. I added it to the function ```drawCircle```and tried adding it as parameters but it throws me an error. So idk what to do. Please help lol.

Comments

  • Posts: 1,547

    It’s not clear to me exactly what problem you’re having.

    Can you post the code that’s not working, that includes references to current touch?

  • dave1707dave1707 Mod
    Posts: 9,725

    @rockinspots Here’s an example that uses CurrentTouch and an example (commented) that uses the touched function. Even though CurrentTouch works, it’s better to learn about the touched function. The touched function gives you more control over the screen touches

    function setup()
        x,y=0,0    
    end
    
    function draw()
        background(147, 223, 210)
        x=CurrentTouch.x
        y=CurrentTouch.y
        drawCircle()
    end
    
    function drawCircle()
        fill(100,100,100)
        ellipse(x,y,50,50)
    end
    
    
    --[[
    function setup()
        x,y=0,0    
    end
    
    function draw()
        background(147, 223, 210)
        drawCircle()
    end
    
    function drawCircle()
        fill(100,100,100)
        ellipse(x,y,50,50)
    end
    
    function touched(t)
        if t.state==CHANGED then
            x=t.x
            y=t.y
        end
    end
    --]]
    
Sign In or Register to comment.