Home‎ > ‎

Game Loops - Advanced

In a live action game, much is always going on - on the screen. A clock or timer is running, objects are moving across the screen, your player is moving, collisions are happening and sometimes even the landscape is moving. All of this could be happening without you doing anything.

The computer is running what is called a "Game Loop", constantly moving, checking and updating things on the screen and behind the scenes.  This code is always running, in the background. In that game loop, we add our code so that it gets checked and run too.


Creating a Game Loop in your Program

Here is what you have to do to put a Game Loop in your program:

1. Copy this code and put it into your "Stack" script - Stack Script

2. Copy this code and put it into your "Card" script.- Card Script
     Then edit it to add your events and actions

3. Create a Start Button, add this to its script (the object script)

    on mouseUp
       startGame
    end mouseUp


4. Create a Stop button, add this to its script (the object script
)

    on mouseUp
       stopGame
    end mouseUp


5. Create a button to move - name the button  "box"
     This is just the name that I used in the card script to move it. You can change it to whatever name that you wish.

6. Open the Card Inspector - name the card "Level1"
     This is just the name that I used in the card script to move the button "box". You can change it to whatever name that you wish.

That's it. It should work after you customize the code for your game

Notes:

1.
The game loop (in the card code) is run every 50 milliseconds when the screen is updated:

    on updateScreen 
       if sGameRunning is true then     
          moveBox
          --      moveTerrain
          --      moveEnemy
          --      detectCollisions
          --      updateScore     
       end if  
    end updateScreen

(The dashes "--" in the lines above comment out the lines so they do not run. Each line runs a different handler. Un-comment them one at a time when you add the handler and its code.

for instance:
if you have enemies that move across the screen, un-comment out that line - take out the dashes in front of it:
     
      moveEnemy

then add code below:

    on moveEnemy
       ..... add your code to move the enemy
       set the left of the button "enemy1" to the left of the button "enemy1" - 4
       if the right of the button "enemy1" < 0 then
            set the left of the button "enemy1" to the right of the card "level1"
       end if
    end moveEnemy
 

Un-comment the other ones (moveTerrain, detectCollisions and updateScore) as you add their handlers and their code

2. Using keysDown() function instead of arrowKeys messages
Instead of using the "arrowkeys" to move the "box" we now use the keysdown() function. This allows us to look for any key on the keyboard and not just the arrow-keys. 

  If your box is not moving, there may be too many messages being sent when you press the arrow keys. Try this:
change all the "=" to "includes" in the code to move the box (do it to every "if keysdown() =..." )

    on moveBox
       if keysdown() = 65362 then
           set the top of the button "box" to the top of the button "box" - 4
        ...

to 
    on moveBox
       if keysdown() includes 65362 then
           set the top of the button "box" to the top of the button "box" - 4
       ...

3. We added Start and Stop buttons code to allow the user to pause or stop the game. (Since the Game Loop is always running, we need a way to stop it. This is the way to stop the timers, enemies moving, scenery moving, etc)

4. We also took the opportunity to add code to speed up the game by taking advantage of hardware acceleration. (the line:    set the acceleratedRendering of this stack to true  )

5. Where the handler "on arrowKeys" detected just the arrowKeys, we now use the " on keysDown() " function for any key on the keyboard. To see the number for a key, add this line to your updateScreen code:
   
    put the keysdown() into msg

this will show the number of each key that you press as you press it, in the msgbox (This is one of the icons on the Edit Bar of LiveCode)

Take out that line of code when you have noted the numbers of all the keys that you need to handle.


NEXT - Moving Enemies
Comments