Code not working

When you touch the screen the ball is supposed to move where you touched, however this is not working.
Help appreciated

Code:


function setup()
physics.gravity(vec2(0,-1000))
    
touch=1

 
b1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))   
b2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) 
b3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    
        
block=physics.body(CIRCLE,40) 
block.x=WIDTH/2
block.y=40
blockx=block.x
blocky=block.y


end    
    function draw()
    
    background(0, 175, 255, 255)
    
    if touch> WIDTH/2 then  
        blockx=blockx+4
    end
    sprite("Documents:slime",blockx,blocky,80,80)
    blockx=block.x
blocky=block.y 
        
    
    
  end      
function touched(touch)
    if touch.state==BEGAN then
        block.linearVelocity=(vec2(0,600))
    end
    touch=touch.x
    end

Use three ~s around your code to make it format correctly, otherwise it’s just a big blob of code like it is right now.

~~~

Your code here

~~~

However, if you want to set its position, try replacing your touched function with this:

function touched(touch)
    block.x = touch.x
    block.y = touch.y
end

That works, but I would rather have the ball follow where you touched then just teleporting.

Added 3 ~'s to the code for formatting. @Conor_R12 If you want the ball to follow where you touch, then you’re going to have to learn how to use tables.

@Conor_R12 Maybe something like this?

function touched(touch)
    block.x = block.x * 0.9 + touch.x * 0.1
    block.y = block.y * 0.9 + touch.y * 0.1
end

It might look a little confusing, but it basically mixes the two values together, 90% the ball’s position, and 10% the touch’s position (getting closer every time you move your finger)

@Conor_R12 Try this.


displayMode(FULLSCREEN)

function setup()
    x=WIDTH/2
    y=HEIGHT/2
    dx,dy=0,0
end

function draw()
    background(0)     
    fill(255)
    text("Drag your finger around the screen",WIDTH/2,HEIGHT-50)
    x=x+dx/100
    y=y+dy/100
    ellipse(x,y,20,20)
end

function touched(t)
    if t.state==MOVING then
        dx=t.x-x
        dy=t.y-y
    end
end

Actually, I hate to say it, but maybe you shouldn’t use the touched function at all, and should put this line of code in draw:

block:applyForce(vec2(CurrentTouch.x - block.x, CurrentTouch.y - block.y))

@SkyTheCoder Why do you hate to say that?

@warspyking Because usually you should avoid CurrentTouch at all costs, but in some rare cases, like here, it would actually be simpler.

@SkyTheCoder why do you want to avoid current touch?

Very buggy (IMO) @jrohanian

CurrentTouch works very similar to the function touched(). The major difference is CurrentTouch doesn’t give an “id” value, which means that you can’t use CurrentTouch to control multiple items at the same time. Other than that, you can do pretty much the same thing with CurrentTouch as with touched(). If you only have one item that you’re controlling, then both CurrentTouch and touched() will work equally well.

@jrohanian People tend to get confused about them, there are a few odd things that can occur. For example, the value is a touch in the bottom left corner before you even press the screen. Their state can be BEGAN for multiple frames. They don’t go away when you lift your finger. They don’t support multitouch, meaning it’ll use the most recent touch (which will make the coordinates jump across the screen in 1 frame), etc. the list goes on and on.

@SkyTheCoder The proper use of CurrentTouch is just like using the function touched(). When a program starts, the “state” of CurrentTouch is CANCELLED. When the screen is touched, it’s state is BEGAN, when moving its state is MOVING, and when you lift your finger, it’s state is ENDED. As I said above, CurrentTouch is like touched(), except you can’t use “id” since it doesn’t have one.

@SkyTheCoder , Yeah, i’ve noticed this. I just consider it useful for different things. Sometimes it can cut down a chunk of code and use a few less variables.
BEGAN lasting multiple frames is the only thing that really cuts back on its potential, though.