Sprite Drag Around Help

Hey everyone. In my computer science class we’re suppose to write code that displays a sprite and the user can drag it with their finger. We’re suppose to follow our teacher’s tutorial but for some reason it doesn’t work for me. The code is the same as his. Any help would be appreciated :slight_smile:

-- This program displays a sprite and the user can move it with their finger 
-- global variable 
imageName = "SpaceCute:Beetle Ship"
imageSize = vec2(spriteSize(imageName))
imagePosition = vec2()
-- Use this function to perform your initial setup
function setup()
    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     
    sprite("SpaceCute:Background", 512, 384, 1024, 768)
    sprite(imageName, imagePosition.x, imagePosition.y)
end

Not sure why imageSize is returning zeros, but try this as the first line instead

imageName=readImage("SpaceCute:Beetle Ship")

@Zacharias looks like you found a bug in Codea. Thank you for posting your code!

The spriteSize function does not work until setup() has been run. So if you modify your code to declare your global variables in setup it should work:

-- Use this function to perform your initial setup
function setup()

    -- Global variables
    imageName = "SpaceCute:Beetle Ship"
    imageSize = vec2(spriteSize(imageName))
    imagePosition = vec2()

    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)

    noFill()
    noSmooth()
    noStroke()
    pushStyle()   

    imagePosition = vec2(WIDTH/2, HEIGHT/2)

end

I’ll look into why it’s not returning correct values prior to setup().

@Simeon Thanks! I’ll be sure to mention this to my teacher. (His tutorial is from 2013 so it [Codea] must of worked differently back then)

@Ignatz your suggestion works as well. Although my class has only just begun learning the language so I don’t wanna confuse my classmates with code we haven’t learned about yet. Still thanks a lot! :slight_smile:

@Zacharias - I suggest you search the forum (eg for ‘drag around’), as there are other people who have written this kind of program.

@Ignatz - Yeah I’m sorry I just figured posting my exact code and just not looking through the forum would be a quicker solution. If I have any other questions I’ll look through the forum first. :-S

Don’t worry, I’m not being critical, you did the right thing. I’m just saying if you want some ideas, you might find them here.

@Zacharias There’s 4 years of information on the forum. The coding and information range from basic to complex. I’ve been here for about 3 1/2 years and I’m still learning new things searching thru the forum.

@Ignatz All good :slight_smile: @dave1707 Thanks for the tip.

My class has this final project where we build an app within a group and I’ll be sure to come here for help