Howdy, Stranger!

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

Touch problem ! help !

in Questions Posts: 5

Hi all !
I'm kinda new to the Codea and Lua.
I watched some tutorials made by Patrick Coxall. In lesson 4 he used this code to move the Beetle Ship with touch .

but when I use this code nothing happens the Beetle Ship won't react to touch.

I'm sure something is wrong about the IF statement but I don't know where !
if I put _imagePosition = currentTouchPosition _out of IF statement it will work but not as I wanted.

here is the Code (Pasted from my Codea):

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

function setup()
   supportedOrientations(LANDSCAPE_ANY)
   displayMode(FULLSCREEN)
   noFill()
   noSmooth()
   noStroke()
   pushStyle() 

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

function touched(touch)
    local currentTouchPosition = vec2(touch.x,touch.y)
    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
end

function draw()
   background(0, 0, 0, 255)
   sprite("SpaceCute:Background",512,384,1024,768)
   sprite(imageName,imagePosition.x,imagePosition.y)
end

Have a good day.

Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    @shem - that's the result of a tricky little change to the way Codea starts up. The graphics are not initialised until the code is compiled, which means the line that sets the value of imageSize doesn't work.

    Put the first three lines inside setup, and it should work fine.

    btw, if you want more explanations and tutorials, I've done a lot over here, with ebooks too.

  • Posts: 5

    Solved !
    Thank you Ignatz for quick response and those useful links.
    So, I have to read about new version changes.

  • IgnatzIgnatz Mod
    Posts: 5,396

    There aren't many version changes, I wouldn't worry about them. Just post problems as you find them.

Sign In or Register to comment.