Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sprite Drag Around Help

edited November 2015 in Suggestions Posts: 15

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 :)

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

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

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

    imageName=readImage("SpaceCute:Beetle Ship")
    
  • SimeonSimeon Admin Mod
    Posts: 5,024

    @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)

  • edited November 2015 Posts: 15

    @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! :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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

  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • dave1707dave1707 Mod
    Posts: 7,746

    @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.

  • edited November 2015 Posts: 15

    @Ignatz All good :) @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

Sign In or Register to comment.