Hello I'm new to codea and coding ing General but I'm having trouble with the touch function here


-- lesson 4
-- created by: Joseph Funchion
-- created on: 1/8/2015
-- created for: practice
-- this program displays an image on screen and lets user move it with finger

    --global variables
    imagename =("SpaceCute:Rocketship")
    imagesize =vec2(spriteSize(imagename))
    imagePosition = vec2()

-- Use this function to perform your initial setup
function setup()
    --set up display and drawing functions
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    noFill()
    noSmooth()
    noStroke()
    pushStyle()
    
    imageposition = vec2( WIDTH/2, HEIGHT/2)
    end
    
-- This function gets called once every frame
function touched(touch)
    end
    
    --local variables
    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
     -- 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", 540, 412, 1024, 764)
    sprite(imagename, imagePosition.x, imagePosition.y)
    end

Use three ~s to format code on the forums, otherwise it’s just a bunch of confusing jumbled-up lines.

~~~

Your code here

~~~

Anyway, what’s your problem? It doesn’t look like there’s anything wrong to me (just by looking at the code, though, I can’t copy/run it because it isn’t formatted)

You have an “end” statement directly after where the function touched() starts.

I added the 3~'s for you.

The problem is that you have an end statement immediately after the “function touched” statement. Move this to the end of the code you want inside the function.

It would help enormously if you indent your code neatly, then you would see problems like this.

@Funchion Another error you have is

local currenttouchposition=vec2(touch.x,touch.y)

currenttouchposition should be currenttouchPosition

You have to be careful when using upper/lower case variables.

@Funchion I took your code and cut it down just to give you an idea of how little code it actually takes to do what you’re trying to do.

EDIT: changed original code and reposted it here.


-- created by: Joseph Funchion
-- created on: 1/8/2015
-- created for: practice
-- this program displays an image on screen and lets user move it with finger

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    image=readImage("SpaceCute:Rocketship")
    imageSize=vec2(image.width,image.height)
    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", 540, 412, 1024, 764)
    sprite(image, imagePosition.x, imagePosition.y)
end

Thank you everyone for the help and thank you Dave For fixing my top post. I fixed the two things mentioned earlier the extra end and the misspelling but now the sprite is all the way in the bottom left corner mostly off screen and can’t be moved anywhere

-- created by: Joseph Funchion
-- created on: 1/8/2015
-- created for: practice
-- this program displays an image on screen and lets user move it with finger

--global variables
imagename =("SpaceCute:Rocketship")
imagesize =vec2(spriteSize(imagename))
imagePosition = vec2()

-- Use this function to perform your initial setup
function setup()
    --set up display and drawing functions
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    noFill()
    noSmooth()
    noStroke()
    pushStyle()
    
    imageposition = vec2( WIDTH/2, HEIGHT/2)
end

-- This function gets called once every frame
function touched(touch)
    
    
    --local variables
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", 540, 412, 1024, 764)
        sprite(imagename, imagePosition.x, imagePosition.y)
    end

I have done it the simpler way but I’m trying to get it so the sprite only moves when being touched I want to touch other parts of the screen and the sprite should stay there

imagesize has a value of 0,0. Also, check the upper/lower case of imageposition.

I did notice the spelling but what do you mean by image has a value of 0,0

@Funchion If you put a print statement after the line of code

imagesize=vec2(spriteSize(image name))
print(imagesize)

you’ll get a value of 0,0.

I modified your code and reposted it in my example code above.

@dave1707 - actually, I tried that line and it worked (I got non zero results). Go figure.

@Ignatz I get values of 0,0. If I move those lines down into the function setup() and add the print statement, then I get correct values ( 256,183).

ah yes, I missed that subtlety, I had put them in setup

it may not work outside of setup because OpenGL is not configured at that point