Lunar Hotel Shuttle

graphic of rocket with engine turned off graphic of rocket with engine turned on

LiveCode Files for this Project

Getting Started

This is an early draft of the app titled "Lunar Hotel Shuttle" that Lloyd has in the Apple app store.

Here are a few quick notes:

Version 1 of the project uses a classic strategy for programming a simulation loop that accepts user actions while the simulation is running. During the workshop we will dissect the LiveCode file to understand how it works. Here is an illustration of how the simulation loop works:

On the card script there is a procedure called "MainEventLoop" that creates a loop. A second procedure called "moveRocket" is triggered during each loop. This loop continues as long as the global variable "gVarRun" is true. While the loop is executing, LiveCode is able to "listen" to any buttons (or other events) that might also be triggered. In this project, there are two buttons that the user can press. The first turns on and off the rocket's engine. The second button allows the user to "freeze" or "unfreeze" the simulation (this is useful when developing the app, but is something that would be hidden, or deleted, after the app is finished and ready to be distributed).

This looping structure is very powerful and can be the basis of any project where you want the user to be interacting in some way while something else is going on at the same time. I have built a much simpler project that uses the exact same structure in order to make it clearer how this all works. So, if you are at all confused at this point, I suggest you click here to check out "Catch a Number."

This is a classic example of a simulation in that the project simulates the effect of gravity on an object. On earth, the force of gravity is about 9.81 meters (about 32.2 ft) per second per second. This means that in free fall an object increases in speed by 9.81 meters per second every second. So, in five seconds, the object would be moving at a velocity of 49.05 meters per second. In 10 seconds, the object would be moving at a velocity of 98.1 meters per second. But, the rocket has an engine with a thrust greater than that of gravity. So, when turned on after a few seconds, the rocket will slow down and then start to move upwards. We refer to the rules that govern the simulation as the simulation's "underlying model," which in this case is based on the laws of physics. Here are some of the key variables that make the simulation work:

gVarGravity - the force of gravity in the simulation
gVarThrust - the force of the rocket's engine
varRockety - the speed of the rocket at any moment in time. gVarGravity is constantly being added to it to simulate gravity's "pull" downward. Likewise, when the engine is turned on gVarThrust is "pushing" the rocket upward.
varRocketLocation - the location of the rocket at any moment in time.

In version 1, there is script within the moveRocket procedure to check for the speed (varRockety) of the rocket. If it is too high (greater than 100), the simulation ends and the user is given feedback (and a speeding ticket).

During the workshop, we will make some modifications and add some additional scripts to create versions 2 and 3.

Version 2 Modification: Skinning the rocket to show when its engine is on

A useful modification would be to provide additional feedback to the user to indicate when the rocket's engine is on. An obvious thing to do would be to show a flame coming out of the rocket to indicate this. We can do this by modifying the script inside the the button "throttle" -- the two new lines of code are shown in bold:

 on mouseUp  
   global gVarThruster  
   if gVarThruster is false then  
    put true into gVarThruster  
    set the label of me to "On"  
    set the icon of button "rocket" to 1004  
   else  
    put false into gVarThruster  
    set the label of me to "Off"  
    set the icon of button "rocket" to 1003  
   end if  
 end mouseUp  

These lines correspond to the IDs of the two images in the substack (recall we used this same strategy in the Basketball Camp project).

Version 2 Modification: Adding Script to Accept the Space Bar as an Alternate Input

The simulation works fine as it is, but the user interaction is enhanced if we allow a keyboard entry to also trigger the button action of turning the engine on and off. The following script added to the card script accomplishes this:

 on keydown theKey  
   Global isReading  
   if isReading = 0 Then  
    if TheKey = " " Then  
      send mouseup to button "throttle"  
    end if  
   End If  
   if TheKey <> " " Then  
    pass keydown  
   End If  
 end keydown  

Basically, this script constantly "listens" to whether any keys are being pressed and specifically if the space bar is down. If it is, the scripts sends a "mouseup" trigger to the button "throttle," which is the button the player presses to turn the engine on and off.

Version 3 Modification: Adding Scripts to Check for Collisions

Finally, let's add some excitement to the simulation by adding a game component. We will check to see if there are any collisions between the rocket and two rectangles on the card, both of which are currently set to invisible. One is a green rectangle on the bottom labeled "landing pad," and the second is a pink rectangle on the top labeled "mothership."

First, open into the Application Browser (under Tools) and turn on the visibility of these two objects.

Then, add the following script to the bottom of the moveRocket procedure in the card script:

   //check for collisions  
   if intersect(button "rocket", graphic "landing pad") and varRockety < 20 then  
    put false into gVarRun  
    hide field "rocket engine label"  
    hide button "throttle"  
    hide button "Start/Stop"  
    show field "landed feedback"  
    show button "Try Again"  
   end if  
   if intersect(button "rocket", graphic "landing pad") and varRockety > 19 then  
    put false into gVarRun  
    hide field "rocket engine label"  
    hide button "throttle"  
    hide button "Start/Stop"  
    show field "crash feedback"  
    show button "Try Again"  
   end if  
   if intersect(button "rocket", graphic "mothership") then  
    put false into gVarRun  
    hide field "rocket engine label"  
    hide button "throttle"  
    hide button "Start/Stop"  
    show field "mothership feedback"  
    show button "Try Again"  
   end if  
 end moveRocket