Howdy, Stranger!

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

Help me with code! Please!

Can we do "Wait" in Codea? For example: the train goes, then stops, then waits 10 second, and then goes again. If it is possible, can you tell and show me? Please.

Comments

  • dave1707dave1707 Mod
    Posts: 7,923

    @AYKostarev If you use physics objects, you can use pause to stop them. But for normal code, there isn't a Wait function. But that doesn't mean you can't do a wait. Depending on what you're doing, you can write your code to allow a simulated wait. The draw() function runs 60 times per second and there's no way to stop it. That's the function that normally drives your code. Without knowing exactly what you want to do, I can't tell you how to do it. But here's a simple example that might help.

    function setup()
        x=WIDTH/2
        y=HEIGHT-100
        dy=1
    end
    
    function draw()
        background(0)
        fill(255)
        text("tap screen to stop/start",WIDTH/2,HEIGHT-50)
        ellipse(x,y,20)
        y=y-dy
    end
    
    function touched(t)
        if t.state==BEGAN then
            if dy==0 then
                dy=1
            else
                dy=0
            end
        end
    end
    
  • IgnatzIgnatz Mod
    edited December 2015 Posts: 5,396

    @AYKostarev - And here's a variation that waits 10 seconds whenever you touch the screen

    function setup()
        x=WIDTH/2
        y=HEIGHT-100
        dy=1
        pause=0
    end
    
    function draw()
        background(0)
        fill(255)
        text("tap screen to pause for 10 sec",WIDTH/2,HEIGHT-50)
        ellipse(x,y,20)
        if ElapsedTime>pause then y=y-dy end
    end
    
    function touched(t)
        if t.state==BEGAN then
            pause=ElapsedTime+10
        end
    end
    
  • Thank you

  • AnatolyAnatoly Mod
    edited December 2015 Posts: 877
    function draw()
    background(0)
    fill(255)
    stroke(100)
    if y == nil then y = 50 end
    strokeWidth(5.5)
    rect(WIDTH/2, y, 100, 25)
    -- Now the stops
    if countdown == nil then countdown = 0 end
    -- We begin!
    if y == 50 then
    countdown = countdown + 0.125
    elseif y == 100 then
    countdown = countdown + 0.05
    elseif y == 250 then
    countdown = countdown + 1
    else
    y = y + 1
    end
    -- The countdown restart
    if countdown == 10 then
    countdown = 0
    y = y + 1
    end
    -- Restarting the project
    if y == 750 then y = 50 end
    end
  • AnatolyAnatoly Mod
    edited December 2015 Posts: 877

    This is an example @AYKostarev. This example shows a way with only draw

  • dave1707dave1707 Mod
    Posts: 7,923

    @TokOut Putting everything in draw isn't a very good idea. You're executing code 60 times per second that doesn't need to be done over and over. The purpose of the setup() function is to execute code that only needs to be run 1 time. The code below that's in your program only needs to be run once and not 60 times per second. Also, it's not a good idea to compare a number to an exact value as in
    if countdown == 10 then when you're adding fractions to it. When you add .05 to countdown, on my iPad Air, countdown is never equal to 10 and the program doesn't do what you intend it to do. When countdown is supposed to be 10, the difference is actually -7.105427357601e-15 and the compare fails. In your program the == should be changed to >= so when countdown is equal to or greater than 10 then you add 1 to y.

    fill(255)
    stroke(100)
    y = 50
    strokeWidth(5.5)
    countdown = 0
    
Sign In or Register to comment.