Howdy, Stranger!

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

Help with Images & touch

After viewing a codea video ( ), Lesson 4.
It seems like I did something wrong...This lesson has to do with Loading an Image
and moving it around on the screen....Any help to tell me what I've done wrong is appreciated,
and as always, thanks in advance ....

-- Touch
-- Global Vaiables

imageName = “Card_Back_1” -- How come this is not in set-up ?
imageSize = vec2(spriteSize(imageName))
imagePosition = vec2()

function setup()
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULL_SCREEN)
noFill()
nosmooth()
noStroke()
spriteMode(CENTER)
imagePosition = vec2(WIDTH/2, HEIGHT/2)
end

function touched(touch)
-- this will contain where you touched on the screen
local currentTouchPosition = vec2(touch x, touch.y)
if (touch. == BEGAN) then
        end
-- This is the 4 side BoundingBox
if (touch. == 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. == ENDED) then
        end
end

function draw()
backGround(0,0,0,255)
sprite(imageName, imagePosition.x, imagePosition.y) 
end

Comments

  • dave1707dave1707 Mod
    Posts: 6,325

    Here’s an example of moving an image around the screen with touch.

    function setup()
        img=readImage("Planet Cute:Character Horn Girl")
        tx,ty=0,0
    end
    
    function draw()
        background(0)
        sprite(img,tx,ty)
    end
    
    function touched(t)
        if t.state==MOVING then
            tx,ty=t.x,t.y
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 6,325

    Here’s an example that moves the image only when you touch on the image.

    function setup()
        img=readImage("SpaceCute:Icon")
        tx,ty=WIDTH/2,HEIGHT/2
        w=img.width
        h=img.height
    end
    
    function draw()
        background(0)
        sprite(img,tx,ty)
    end
    
    function touched(t)
        if t.state==MOVING then
            if math.abs(t.x-tx)<w/2 and math.abs(t.y-ty)<h/2 then
                tx,ty=t.x,t.y
            end
        end
    end
    
  • em2em2
    Posts: 167

    @kendog400 the first two variables are not put in setup because they won't be changed.

    displayMode(FULL_SCREEN)

    FULL_SCREEN isn't a variable. Use FULLSCREEN.

    • Your background is not drawing—it's not spelled backGround, it's spelled background—note that variables and function names in most programming languages are case-sensitive.

    • Background doesn't need a fourth input for alpha. That doesn't matter.

    if (touch. == BEGAN) then

    • Why is the touch written with a dot at the end? Did you mean to put touch.state?

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

    • Can you see the error here?

    if( (imagePosition.x, - imageSize.x/2) < ...

    • Your if statement doesn't have a closing parenthesis. Lua doesn't require an opening parenthesis, so you can just remove it.

    I hope this fixes enough errors for you to adequately debug the rest on your own. Please read the reference if you are ever unsure about anything.

  • Posts: 57

    It looks like this was some sloppy programming work on my part. Thanks for the help em2, now I know to clean-up my act....

Sign In or Register to comment.