I'm can't move my image using touch.

I could move my image but it would snap to wherever my finger was on the screen. I tried to set up a hit-box but now I can’t move the image.


imagePosition = vec2()
imageName = "SpaceCute:Beetle Ship"
imageSize = vec2(spriteSize(imageName))
    
function setup()
    
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    noFill()
    noSmooth()
    noStroke()
    pushStyle()
    
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
end

function touched(touch)
    
    local currentTouchPosition = vec2(touch.x, touch.y)
    
    if (touch.state == BEGAN) then
        
    end
    
    if (touch.state == MOVING) then
        if( (imagePosition.x - imageSize.x/2) < currentTouchPosition.x and
            (imagePosition.x + imageSize.x/2) > currentTouchPosition.x and
            (imagePosition.y - imageSize.y/2) < currentTouchPosition.y and
            (imagePosition.y + imageSize.y/2) > currentTouchPosition.y ) then
            
            imagePosition = currentTouchPosition
        end 
    end
    
    if (touch.state == ENDED) then
        
    end
   
     
end

function draw()
    
    background(0, 0, 0, 255)
    
    sprite("SpaceCute:Background", 512, 384, 1024, 768)
    sprite(imageName, imagePosition.x, imagePosition.y)
end

@DarkPlasm When you post code, put 3~'s on a line before and after your code to format it correctly. Here’s a version of your code that I think does what you want.

EDIT: I added the 3~'s on you code above.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = "SpaceCute:Beetle Ship"
    dx,dy=0,0
end

function touched(touch)
    if (touch.state == MOVING) then
        dx=dx+touch.deltaX
        dy=dy+touch.deltaY
    end
end

function draw()
    background(0, 0, 0, 255)
    sprite("SpaceCute:Background", 512, 384, 1024, 768)
    sprite(imageName, imagePosition.x+dx, imagePosition.y+dy)
end

@dave1707 Thank you