If I have text on the screen, How can I make it so that after, let’s say 3 seconds, it changes that text into different text?
Add a timer
You could take a look at the Terminal Text project I did. I set up some functionality that allows you to easily setup text on a delay and then detect when it is done. I also included a good example of a Sleep Timer that you could look at.
in setup()
counter = 0
in draw()
text1 = "First Text"
counter = counter + 1
if counter > 200 then
text1 = "Second Text"
else
text1 = "Last Text"
end
text (text1, WIDTH/2, HEIGHT/2)
@Inviso, your code never displays first text… I modified it so it goes first, second, last.
In setup()
counter = 0
In draw()
text1 = "First Text"
counter = counter + 1
if counter < 400 and counter > 200 then
text1 = "Second Text"
elseif counter > 400 then
text1 = "Last Text"
end
text (text1, WIDTH/2, HEIGHT/2)
In all these examples, the counter is incremented in the drawloop, so with a change of framerate there is a change of delay. I wrote this sample for some other discussion about creating a state machine, but it also demonstrates using a more accurate timer:
function setup()
print("touch to pause.")
secondsRemaining =30
gameState = "game"
prevTime = ElapsedTime
end
function draw()
if gameState == "game" then showGame()
elseif gameState == "paused" then showPause()
elseif gameState == "menu" then showMenu()
end
end
function showGame()
if (ElapsedTime - prevTime) >= 1 then
secondsRemaining = secondsRemaining - 1
prevTime = ElapsedTime
end
if (secondsRemaining) <= 0 then
gameState = "menu"
end
background(40, 40, 50)
fill (255)
fontSize(20)
text(secondsRemaining, WIDTH/2,HEIGHT/2)
end
function showPause()
background(255, 0, 0, 255)
fill(255, 255, 0, 255)
fontSize(20)
text("Paused. Touch to resume.", WIDTH/2,HEIGHT/2)
end
function showMenu()
background(100, 255, 0, 255)
fill(71, 0, 255, 255)
fontSize(20)
text("Menu Screen. Touch to start.", WIDTH/2,HEIGHT/2)
end
function touched(touch)
if touch.state == ENDED then
if gameState == "menu" then
--reset
secondsRemaining = 30
gameState = "game"
elseif gameState == "game" then
gameState = "paused"
elseif gameState == "paused" then
gameState = "game"
end
end
end
Sorry, there is more than just a timer here, but what you are looking for is in the showGame function. It uses the ElapsedTime to wait a certain amount of seconds before doing something.
@Tao yes…
I meant it like this:
if counter > 200 then
text1 = "Second Text"
elseif counter > 400 then
text1 = "Last Text"
end
But i forgot the elseif