Humanities Home page   Office of Digital Humanities
Back     BYU LiveCode Lessons Gateway

Game Loop Demo Stack

In a previous lesson we looked at the send message in time command. This allows you to "queue up" a message to be executed at a specified time in the future. By having a handler call itself repeatedly using send ... in time, a technique called recursion, you can set up loops that can repeat indefinitely without negatively impacting program performance. Loops like this make it possible to set up games that require constant motion, such as scrolling or moving objects.

Follow these instructions to implement a "game loop" in a stack. A game loop allows you to set up a handler that gets called repeatedly and automatically in the background. Follow these steps to see a game loop in action.

  1. Create new Stack "blastoff"
  2. rocket-noflame rocket-flame
  3. Import 2 images--rocket with and without flame. These images are in the GameLoop folder in the lesson_materials/Time_and_timing folder on the server.
  4. Create button "rocket"
  5. Note image ids, hide images
  6. Paste stack script "game loop" framework into stack script
  7. Create card script. Copy and paste card script.
  8. Start and Stop buttons
  9. Start button script:

       on mouseUp
          startLoop
       end mouseUp
    

    Stop button script:

       on mouseUp
          stopLoop
       end mouseUp
    

Once you have gone through this process to build the stack, test it by doing this:

  1. The preOpenStack handler kicks off the game loop, so you have to run preOpenStack once to start the game loop. An easy way to do this is to simply save the stack, close it, and open it again.
  2. Now click the start button.
  3. Hold down the mouse button to make the rocket fly upward; release the mouse button and the rocket will fall back down to earth.
  4. Once you confirm it's working in the LiveCode IDE, test it on a simulator or device.

Back     BYU LiveCode Lessons Gateway
Maintained by Devin Asay.
Copyright © 2005 Brigham Young University.
This page last updated on March 12, 2020 09:19:05.