Lesson 18

advertisement
Lesson 18
Time, Functions, and Converting Numbers to Strings
Pre-Requisites: Lesson 14
Topics:
1. Time
2. Converting numbers to strings
Introduction:
Right now, we can drive our car around the track and the game will tell us we've won the race.
The problem is that it isn't very challenging. You can take 20 minutes to finish and nothing will
be different. It would be nice to have a way to see how well we did. One way a lot of racing
games do this is to use a timer. You can see how long it took you to run the track and use the
time to gauge how well you did. We're going to add a timer to our game.
Setting up the Existing Object and Event:
To display the timer, we’ll use an object that’s already in the world, a 3D text object called
"timer". It starts out invisible, so the first thing we need to do is make it visible.
In "world.my first method", drag the object "timer", drop it BELOW camera.followCar
and select "timer set opacity to 1".
Now when the world starts, the text for the timer will show, but we still need to update it with the
right time. For this, we've added a method called "timer.setTime". Since it always needs to show
the correct time, we'll need it to run constantly, just like we did for drive already. We've created a
"when the world starts" event and made it call "setTimer", but we'll need to enable the event
before it will work.
Enable the timer reset event: Scroll to the "when the world starts do timer.setTime" event,
right-click it, and click "enable".
Displaying the Correct Time:
Now we need to use that method to set our timer. We'll need to use a function for this. With
"world" selected in the object tree, if you click the "functions" tab below the object tree you'll see
a list of functions Alice gives us. If you scroll about halfway down, you'll see functions for
"time". The "time elapsed" function looks like it would be perfect.
Fill in the timer.setTime method: In timer.setTime, drag the local variable "time" into the
infinite loop and drop it. Select any initial value. Select world in the object tree, click the
functions tab and drag "time elapsed" over the value for "time" in the infinite loop.
This will store the time since the world started, but we still need to update the text for the timer.
Add a default assignment of timer.text to the method: Select the timer object and click
properties. Drag the "text" property into the infinite loop below the set value command and drop
it. Set the value to "default string".
Now, if we try to drag the "time" variable over "default string" to set the value, we can see that it
won't work. Alice stores numbers and strings of characters differently, so we need a way of
converting a number to a string. Luckily, Alice provides a function for this. If we go through the
list of functions with "world" selected in the object tree, what looks like it would help? ("what as
a string"). We can use the function "what as a string" to convert something to a string. Let's use
that to make our 3D text show the correct time.
Get the string value of the time variable: Select "world" in the object tree and click the
functions tab. Drag "what as a string" over "default string" and drop it. Select "expressions ->
time" for the value.
Fixing the Timer Display:
Now, let's click play and see what we've got. [Play the world for a few seconds.] It looks like we
have two problems. First, our timer skips a lot. It isn't updating constantly, it's jumping in steps.
Second, it's really long. We probably don't need nearly that many decimal places. To fix the first
problem, we just need to change the duration.
Change the duration to 0: Click "more..." next to the "set text" command. Select "duration -> 0
seconds".
That will make our timer update constantly, but it's still too long. We can shorten it by rounding
off the time to the nearest second. For that, we'll use a function Alice provides called "round".
You'll find it under "advanced math" in the world functions tab, near the bottom of the list.
Round the timer value: Drag the local variable "time" from the top to between the first "time
set value" and "timer set text" and drop it. Select "expressions -> time" for the value. From the
"world" functions tab, drag "round a" over the value "time" and drop it to set the value to "round
time".
Now if we click play we'll see that we only have one decimal place, which is always 0.
Unfortunately, Alice doesn't give us the ability to keep just a few decimal places, but this will
give the player an idea of how they did.
Download