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.
- Create new Stack "blastoff"
- Size to desired (e.g. match mobile screen size--portrait orientation)
- Set a background color if desired.
-
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.
- Create button "rocket"
- show name off, transparent, no borders, no auto hilite
- assign rocket image as icon
- resize button to show icon fully
- Note image ids, hide images
- Paste stack script "game loop" framework into stack script
- Create card script. Copy and paste card script.
- Start and Stop buttons
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:
- 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.
- Now click the start button.
- Hold down the mouse button to make the rocket fly upward; release the mouse button and the rocket will fall back down to earth.
- 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.