Two questions

Hi there,
I am new to codea, and I would like to ask two questions.

  1. How can I make a wait() or sleep() function, like as delaying?
  2. Why does when I make an interactive draw thingy, does it flash?
    Here is the code for that:

So, what happens is, when I drag on the screen, it makes squares or circles, but they flash, or appear to follow eachother, however, when I record this, they don’t flash or follow each other, and are much more stable and smooth.

And also, relating to two, how can I make it where I draw, and as I draw, the shapes leave no gaps?

  1. Nothing like this yet.
  2. Put a background(0, 0, 0) in the beginning of your draw function, this clears the screen every frame.

@YaYa

Your code doesn’t show so I can’t comment on it. Here is a simple program of a circle following your finger as you drag it on the screen. As for a wait() or sleep() function, what you would use it for would determine how it’s written. Normally you really don’t need one.


function setup()
    displayMode(FULLSCREEN)
end

function draw()
    background(40, 40, 50)
    ellipse(CurrentTouch.x,CurrentTouch.y,100,100)    
end

ha ha you fired faster than me Jordan! Yaya you cannot do a sleep, but you can start an action after the given time is elapsed, it is about the same, but a bit more complex to implement. The following code is an example i just wrote for you (and works[edit: just changed the draw, there was a bug i hadn’t noticed, cause it was workinf apparently fine):


--# Main
-- sleep-wait


function setup()
    sleep = Sleep()   -- create a sleep object to manage your wait
    sleep:doAfter(2,Hello)
end

function Hello()
    print("Hello")
    sleep:doAfter(2,Hello)    -- to start again (not necessary)
end

-- This function gets called once every frame
function draw()
    sleep:draw()
end


--# Sleep
Sleep = class()

function Sleep:init()
end

function Sleep:doAfter(t,callback)
    self.callback = callback    -- saves the callback
    self.t0 = ElapsedTime + t -- will save time to run
    self.active = true    -- to run only once
end

function Sleep:draw()
    if self.active then
    if ElapsedTime>self.t0 then 
        self.active = false    -- to run only once
        self.callback()  -- do the action
    end
    end
end

Details: i create a special Sleep class to manage the sleep object, because it will memorize the necessary data, via the self.whatever = something statements. Then i have to create a sleep object in setup. Then the function i want to start has to de defined: here it is Hello(). Then i can set the wait action: sleep:doAfter to run Hello() after 2s. Last thing: in the draw() i call sleep:draw() which doesn’t draw in this case but checks if it’s time to fire. Look carefully at the syntax for ‘.’ ‘:’ and ‘()’ it is a bit tricky…

@YaYa

Here is my above program modified to draw the circle at random x,y locations. The program will wait if you tap the screen and resume when you tap it again. As I said earlier, depending on how you want to wait or sleep will depend on what you’re doing and how you want to do it.


function setup()
    displayMode(FULLSCREEN)
    wait=false
end

function draw()
    background(40, 40, 50)
    if wait==false then    -- change x,y locations only if not waiting
        x=math.random(WIDTH)
        y=math.random(HEIGHT)
    end
    ellipse(x,y,100,100)    -- always draw circle at last x,y location
end

function touched(t)
    if t.state==BEGAN then
        wait=not wait    -- toggle wait true or false
    end
end

Sorry, I forgot to add the code.
Let me rephrase.
I want this code to stop making flashing shapes.

iparameter("Shape",0, 1,1)
iparameter("Size",1,5,1)

shapes = {
[1] = "Square",
[2] = "Circle"
}

function TranslateShape(val)
    for i,v in pairs(shapes) do
        if i == val then
            return v
        end
    end 
end

function draw()
    if string.sub(tostring(CurrentTouch.state),1,1) == 0 or CurrentTouch.state == 1 then
        if TranslateShape(Shape) == "Square" then
            rect(CurrentTouch.x,CurrentTouch.y,Size*10,Size*10)
        else
            ellipse(CurrentTouch.x,CurrentTouch.y,Size*10,Size*10)
        end
    end 
end

(I cleaned up the formatting in the original code post and deleted the second. Hope that’s okay.)

Fail, I’m new to this forum, what’s the bbcode?

.@YaYa, That’s what we are doing, and if you post code please format it correctly. If you want to post code, put three tilde characters (~) at the beginning and end of your code.

iparameter("Shape",0, 1,1) 
iparameter("Size",1,5,1)
Size10 = 10
shapes = { [1] = "Square", [2] = "Circle" }
--backingMode(RETAINED) -- Uncomment this and delete the background(0, 0, 0, 0) in draw to see a different effect
background(0, 0, 0, 255)
function TranslateShape(val) 
    for i,v in pairs(shapes) do 
        if i == val then 
            return v 
        end 
    end
end

function draw()
    background(0, 0, 0, 255)
    if string.sub(tostring(CurrentTouch.state),1,1) == 0 or CurrentTouch.state == 1 then 
        if TranslateShape(Shape) == "Square" then 
            rect(CurrentTouch.x,CurrentTouch.y,Size10,Size10) 
        else 
            ellipse(CurrentTouch.x,CurrentTouch.y,Size10,Size10) 
        end 
    end 
end

```