Code help

Hello everyone,
Just trying to understand Codea better but would like some clarification on the following code.


-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
end
function touched(touch)
   if touch.state == BEGAN then
       print("tapped")
       ellipse(touch.x,touch.y,100,100)
   end
end

-- This function gets called once every frame
function draw()
    -- This sets the background color to black
  background(0, 0, 0)
    -- Do your drawing here
end

I get the print statement to work fine, and can only get the ellipses to show if I comment out setting the background to black but the ellipses are affected by the refresh as expected. What I am trying to accomplish is basically to spawn an entity at my each of my single touch locations. If I use CurrentTouch, it will create the first one when I touch but just moves it to the next touch position.

Thanks for helping

Edit: Sorry the code looks bad. What is the proper way to show code in the forum? I checked the FAQ but didn’t see anything
Fixed the formatting, Thanks

The setup() function is called only once, when the program is started. touched() is never called unless you call it. Draw() is called several times a second, over and over.

Just call the code from the draw() function -after- the background statement. Background erases what’s currently on the screen.

So something like


function touched(touch)

       If touch.state == BEGAN then

                print(“tapped”)

                ellipse(touch.x, touch.y, 100, 100)

        end

end



function draw()

        background(0, 0, 0)

        touched(CurrentTouch)

end

< pre > < \ pre> without the spaces to format in the forum.

But as I used to make money reading hex dump, the touch and draw loop are independent and will overlay each other. Have the touch loop set a flag for the draw loop to draw.

Thanks for the info. Ipda41001, could you give me an example of using a flag to keep the entitys on the screen in the draw loop whenever and wherever I touch the screen.

Mark, thanks for the tip but it still overwrites my entity in the draw loop unless I keep touching when I use the CurrentTouch

Absolutely, because you’ve told it to draw the ellipse only on the begin of a new touch. If you want the ellipse to stay on the screen, you’ll need to save the location and redraw it each cycle.

the touched routeline is a weird thing. @ruilov said it gets called, you @Mark say the opposite. @Mark s solution helped me, but when i started using multitouch @ruilov helped me by commenting out the “touched(currenttouch)” statements and added multitouch statements, the multitouch worked. im confused now.

Maxiking16, it does get called but if you want you can call it yourself too. It takes a touch as an argument so you can pass currenttouch to it if you’d like. But note that even if you call it yourself, it will still get called by codea every time a touch happens.

Mark suggested calling it above so that the ellipse instruction would get called when draw is executed. as ipda41001 said you could accomplish the same thing by keeping track of the entities in touch(), say with a table, and drawing them latter, but would be a little more complicated and perhaps just calling touch() is simpler if you dont need to deal with multitouches

The routine touched is called every time that there is a touch event (a touch starting, moving, or ending (there’s a “cancel” state as well, but I don’t know how it gets called). So if you want to do something with each touch, that’s the place to do it.

What Mark (probably) means when he says that touched is that if you write a class then the draw and touched methods of that class don’t get called automatically; they have to be called from within the main routines.

The basic program flow is:

  1. Read all files (tabs), executing all the code (most of which just defines functions so doesn’t actually do anything).
  2. Execute the function setup.
  3. Enter an infinite loop with the steps:
    1. Execute the function draw.
    2. For each new touch event, execute the function touched with argument the touch event.

My recommendation is to keep draw stuff in draw and touch stuff in touched. So if you want to draw something based on touches, gather the information in touched but use it in draw.

I’m not totally sure what you want, but the following modification of your code might be closer (incidentally, for code blocks on the forum, put ~~~ on the line before and after; it’s better than `

...

because dangerous characters such as<and&` are correctly handled.):

- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    touches = {}
end

function touched(touch)
   if touch.state == BEGAN then
       print("tapped")
       table.insert(touches,{touch.x,touch.y})
   end
end

-- This function gets called once every frame
function draw()
    -- This sets the background color to black
  background(0, 0, 0)
    -- Do your drawing here
   for k,v in ipairs(touches) do
      ellipse(v[1],v[2],100,100)
   done
end

Andrew, that’s exactly what I was trying to do! Thanks to everyone that’s helped explain this!

Thank you @Andrew_Stacey and @ruilov !! I found a Bug in my Code now. Very helpful information :slight_smile: