drawing inside functions

Hi,
i tried to draw for example an ellipse inside functions. The background is set, but no drawing appears??
Here is a sample:

function setup()
    backingMode(RETAINED)
end

function draw()    
end

function touched(t)
    background(83, 46, 46, 255)
    fill(205, 36, 36, 255)
    ellipse(200,200,200,200)
end

```


what is my mistake??

Greetings, Stefan

I had similar, code that worked like yours before 1.5 doesn’t now.

You need to move it to draw (or a function called within draw)

eg (written without codea, probably got errors)

function setup()
  backingMode(RETAINED)
  touch = nil
end

function touched(t)
  if t.state ~= ENDED then
    touch = t
  else
    touch = nil
  end
end

function draw()
  if touch ~= nil then
    background(83,46,46,255)
    fill(205,36,36,255)
    ellipse(200,200,200,200)
  end
end

thank you for your quick reply, spacemonkey.
this makes things more complicated for me :frowning:

.@StefanK your code will work fine in version 1.5.1. I’ve just tested it. This version is coming very soon (should be submitted to Apple within a day or so).

i have found that it’s hinky to do any drawing outside of draw() or some direct function that draw calls. Calling drawing in touched isn’t reliable from my expereince; i set variables there and let draw() read those and do the hard work.

Also, there is a variable called CurrentTouch, which is a hack for the immediate touch; you proably could remove lots of code and simply ellipse() off of CurrentTouch.

And you definitely want to move the background() call into draw().

thanks again!
@Simeon: i would appreciate that and wait for the new version (congratulations for the last update, i´m a newbie and like the references now very much
@aciolino: you´re right, it is straighter to do drawings under draw(), but sometimes it is more like my own thinking, to have functions, which independently do things.

@StefanK, that’s fine, almost al of my test code is like that; I create a function and call it. I’d sggest you make a function that is clearly defined, such as “function DrawEllipse()” and call that from draw(). It’s really a bad idea to override touch().

Even though they are going to patch it in 1.5.2, I’d not depend on it to always work both because 1)it got broke at least once, and 2) it’s bad form and hard to re-analyze.