Touch State Moving

Hi, hoping someone can advise on this code. I was following a tutorial on YouTube, everything was going so well until everything stopped working and I can’t proceed to the next lesson. I have gone over and over the syntax and can’t find an error anywhere. Its a simple thing, a Sprite on the screen that only moves when it is actually touched. Everything worked fine up until the touch.state == MOVING code was added, I know the tutorial is using an older version on Codea so I’m not sure if the problem is there. Any advice would be helpful, thanks in advance.

-- Lesson4
-- Displays an image on the screen and lets the user move it around

-- Global variables

    imageName = "Space Art:Green Ship"
    imageSize = vec2(spriteSize(imageName))
    imagePosition = vec2()
-- Use this function to perform your initial setup
function setup()
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    noFill()
    noStroke()
    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
-- This function gets called once every frame
function draw()
    
    -- Local variables

    -- This sets a dark background color 
    background(0, 0, 0, 255)
    
    sprite("SpaceCute:Background", 512, 384, 1024, 768)
    sprite(imageName, imagePosition.x, imagePosition.y)
end

Check your calculations in the MOVING statement. I don’t have time right now to do it myself. Your touch.x and y should be within the bounds of the Sprite.

@Cracken - put these lines inside the setup function, because graphic commands don’t work outside of functions.

    imageName = "Space Art:Green Ship"
    imageSize = vec2(spriteSize(imageName))

Thankyou Ignatz, that fixed it, thankyou for fast responses as well.

@Cracken I see you got it fixed already. One thing you should do, and everyone else too, is to put print statements in your code to verify the values you think you should have are what you actually get. By doing that you’ll be able to fix a lot of problems and not have to wait for someone else.

it works if the movement is’nt too fast otherwise you lose the object, i don’t know why

You must replace 2 by 1 in

f( (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

A better solution is this. No matter how fast you move your finger, the Sprite will follow until you lift your finger. @hpsoft The reason you lost the Sprite was the MOVING statement always checked if your touch was within the bounds of the Sprite. If you moved your finger too fast, you moved out of the Sprite boundary. By using 1 instead of 2, you increased the boundary size, but you could still go outside the boundary. What I have below doesn’t loose the Sprite no matter how fast you move your finger because I only check the bounds at BEGAN.

function touched(touch)
    local currentTouchPosition = vec2(touch.x, touch.y)
    if touch.state == BEGAN 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
            move=true
        end
    end
    if touch.state == MOVING and move then
        imagePosition = currentTouchPosition
    end
    if touch.state == ENDED then
        move=false
    end
end

@dave1707 - your solution to effectively give “focus” to the initial item is also the best way to implement a horizontal / vertical slider control. Just saying… :slight_smile:

@TechDojo I probably wouldn’t use this type of routine for a slider, but other people might. For sliders, I would only want control as long as my finger was within the slider boundary. Using this type of routine, once you have control of the slider your finger could be anywhere on the screen and still control it.

IMHO - That’s actually a better UX, for example if the slider is adjusting some kind of value that’s close by - you can hold, slide and move your finger out of the way to allow more accurate placement or to see the desired effect without obscuring the display

There is also a trick where, the further you get away from the slider, the finer-grain input you get, so you can fine-tune the values.

@RonJeffries - that’s a neat trick, really subtle as well - I’ll look to implement that next time I create a slider style control