0

I am trying to create a simple app in codea on my ipad that displays an image and lets the user move it.I am able to display the image properly,but am not able to move it with my finger.

Here is my code for it.

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

How should I make it work?...Thanks in advance.

  • Is your function being called? Are you seeing `touch.state` being `MOVING`? Is `MOVING` a global variable or is it supposed to be a string (i.e. `"MOVING"`)? – Etan Reisner Nov 25 '15 at 20:57
  • the touched function gets executed 60 times/sec.MOVING is a variable pre defined by lua. – user5422891 Nov 26 '15 at 19:26
  • Ok. Is your `if` condition evaluating to true? What is `imagePosition`? This code isn't *doing* anything other then setting a variable. What should it be doing? – Etan Reisner Nov 27 '15 at 02:59
  • imagePosition and imageSize are both global variables. – user5422891 Nov 27 '15 at 07:16
  • Is assigning to `imagePosition` supposed to move the image automatically? Or do you need to actually move it with some function call(s)? – Etan Reisner Nov 27 '15 at 13:58

1 Answers1

0

I imagine by now you have found your answer but if you have not I hope this helps.

Not sure what you had going on in setup() or draw() but what I did was to define imageSize and imagePosition as vec2s and give them an initial value. I also added a simple image.

Besides a little formatting in touched() that code looked fine.

I hope the following code makes sense.

function setup()

-- Screen center
X = WIDTH/2
Y = HEIGHT/2

imageDims = 100  -- Define the image size

imagePosition = vec2(X,Y)  -- Define imagePosition and imageSize in setup() as vec2
imageSize     = vec2(imageDims,imageDims)    

end

function draw()

background(40, 40, 50)

sprite("Cargo Bot:Codea Icon",
    imagePosition.x,imagePosition.y,
    imageSize.x,imageSize.y)

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

Scotty
  • 1
  • 1