Morning all. I’ve written the code in Lua this morning and have it fully working for pretty much 85% of objects. Just gotta go work through each class and it’s done. Thank you all
Maybe you can try coroutine, this is the example code:
function setup()
pause = false
-- Create a new coroutine for the function f
co = coroutine.create(f)
box = { x = 10, y = 10,
width = 20, height = 20,
fillColor = color(24, 28, 202, 255) }
-- Here we can not use tween.
-- t = tween(10, box, {x = 1000, y = 1000})
end
-- Put all your draw code in f
function f()
while not pause do
background()
fontSize(40)
fill(0,255,0)
text(os.time(),WIDTH/2,HEIGHT/2)
fill(box.fillColor)
box.x, box.y = box.x+1,box.y+1
rect(box.x,box.y,box.width,box.height)
coroutine.yield()
end
end
function draw()
if not pause then
coroutine.resume(co)
end
end
function touched(t)
-- Click left screen to pause, click right screen to resume
if t.state == BEGAN and t.x >=WIDTH/2 then
pause = false
elseif t.state == BEGAN and t.x <WIDTH/2 then
pause = true
end
end
Using coroutine, good news is that you can control the flow exactly, bad news is that: 1) you need to control everything manually; 2) the performance will have to be affected.
Thanks @dave1707 and @binaryblues
The code I had in the end was a simple:
If pauser= false then
Which I would put around key parts in each class or functions that were increasing the game leveltimer - so when I hit a button for pause, pauser would be true.
It was dead easy in the end I was kicking myself!
Thanks
@Majormorgan I think it would be better to have a pause variable connected to the classes themselves. For example, if you want some things to stop (pause) and some others to continue there moving, you wouldn’t be able to, because it’s all connected to one variable. I think in the class, you should say for each object, if not self.paused then
, followed by the code to move the object. Then when you want to pause everything,
for i,v in ipairs(table_containing_all_game_objects) then
v.paused=true
end
Thanks @CamelCoder - that’s an awesome piece of code. I think when I come to make an update or new game I’ll use your code. Thanks!
@Majormorgan Instead of wrapping your code as you show above and what I show below, you can do something like what I show in the second and third part. You just have to add one line unless you want to do something special in a function when pauser is true.
-- Wrap the code like what you show above.
if pauser==false then
Code
Code
Code
end
-- Don't wrap the code. Just add a single pause line to return.
if pauser then return end
Code
Code
Code
-- Don't wrap the code. If running special pause code, then return.
if pauser then
pause code
pause code
return
end
Code
Code
Code
Thanks @dave1707 - so does return mean it goes back to the ‘if paused then’ part or back up to the top of then class?
It sounds cool. And could be something I use too.
The advantage I have with where the code is currently is when I pause you can still see the object on screen and it’s stopped too. This is because I wrap all the movement, shooting, hit detection and redrawing code within the ‘if pauser = true’ part. So it feels like a real paused moment.
That way, the drawing of the mesh is outside of that code enabling all the original on screen data like position, size, frame and art are drawn from the moment they were paused.
return means don’t execute anymore code in this function and to return to the calling function. Here’s a very simple example showing return.
displayMode(FULLSCREEN)
function setup()
xr,yr=200,400
xg,yg=100,600
xrv,yrv=3,3
xgv,ygv=2,2
cnt=0
end
function draw()
background(40, 40, 50)
showText()
updateRed()
updateGreen()
end
function showText()
-- show text
fill(255)
text("Tap screen to pause/unpause",WIDTH/2,HEIGHT-50)
text(cnt,WIDTH/2,HEIGHT/2)
if pause then return end -- code to pause
-- update count
cnt=cnt+1
end
function updateRed()
-- draw ellipse
fill(255,0,0)
ellipse(xr,yr,30)
if pause then return end -- code to pause
-- update ellipse position
xr=xr+xrv
yr=yr+yrv
if xr<0 or xr>WIDTH then
xrv=xrv*-1
end
if yr<0 or yr>HEIGHT then
yrv=yrv*-1
end
end
function updateGreen()
-- draw ellipse
fill(0,255,0)
ellipse(xg,yg,30)
if pause then return end -- code to pause
-- update ellipse position
xg=xg+xgv
yg=yg+ygv
if xg<0 or xg>WIDTH then
xgv=xgv*-1 end
if yg<0 or yg>HEIGHT then
ygv=ygv*-1
end
end
function touched(t)
if t.state==BEGAN then
pause=not pause
end
end
@Majormorgan Basically what return does is it exits out of the function. To exit out of an if statement, there are a couple ways you can do it.
First way is to setup a one-time loop
for i=0,0 do
if not pauser then
--Then to end the if statement
if shouldQuitIfStatement then break end
end
end
Another way to do it is like @dave1707 did above, which is to split it up into multiple functions, which is highly recommended.
Lastly, you can use Lua’s goto function. The syntax gets a bit weird in this one.
local bool=true
::beforeIf::
if bool then
if shouldQuitIfStatement then
bool=false
goto beforeIf
end
end