Remember that your game is like a movie, going by frame by frame as seconds go by. You can use this to your advantage, not only in creating collisions and explosions, but for all types of effects in your game. Basically, you can create a countdown timer and have events take place before, during, or after a specific time. To do this, we will use the Millisecs() command. Before I start describing this command, it’s important to know that 1000 milliseconds is equal to 1 second. Blitz3D is always counting the time, and the Millisecs() command will tell you what the current time is. You can use this command to create a timer and schedule events to happen based on that time. Let’s use this command to create a ticking time bomb!
Start by opening the program called demo13-05.bb and save it with a new name. This file is simply five spheres. One is very big, which will be the bomb, and the other four are smaller circles hiding behind the sphere that will act as the shrapnel. What we are going to do is create a countdown timer, and after five seconds we’ll make the bomb disappear and send the shrapnel flying.
We need to start by creating the timer variable, and we will do this by adding the following code in bold. Enter this code outside of the game loop, right below the section where you created the shrapnel.
; This code creates a variable for the timer. timer=MilliSecs()
It is very important that this code remain outside of the game loop—in other words, before the line that reads While Not KeyDown (ESC_KEY); otherwise, the timer will continually reset as the game is running. So now we have a timer that has started counting every millisecond that goes by, which we have called timer. We could have given this timer any name since it is just a name we associated with the command. Now that we have a timer, we need to create some action to happen based on the timer. Add the following code:
; This code makes the program run While Not KeyDown( 1 ) ; This code creates the explosion If MilliSecs() < timer + 3000 ShowEntity bomb Else HideEntity bomb If MilliSecs() > timer + 3000 MoveEntity shrapnel, -.3,+.8,.15 If MilliSecs() > timer + 3000 MoveEntity shrapnel1, .3,-.5,.05 If MilliSecs() > timer + 3000 MoveEntity shrapnel2, -.1,+.5,-.35 If MilliSecs() > timer + 3000 MoveEntity shrapnel3, .3,0,.0 RenderWorld Flip Wend End
Run the program now. After three seconds, the bomb will explode. Now let’s look at the code that we created:
; This code makes the program run While Not KeyDown( 1 )
If MilliSecs() < timer + 3000 ShowEntity bomb Else HideEntity bomb—This line basically says that after three seconds have passed, hide the sphere. Here’s how it’s done: If the current time (MilliSecs()) is less than the timer plus three seconds (< timer + 3000), then show the bomb (ShowEntity bomb). Otherwise, hide the bomb (HideEntity bomb).
The rest of the code deals with the particles flying. It’s basically the same as the code above:
If MilliSecs() > timer + 3000 MoveEntity shrapnel, -.3,+.8,.15 If MilliSecs() > timer + 3000 MoveEntity shrapnel1, .3,-.5,.05 If MilliSecs() > timer + 3000 MoveEntity shrapnel2, -.1,+.5,-.35 If MilliSecs() > timer + 3000 MoveEntity shrapnel3, .3,0,.0
This code just says that after three seconds have passed, send the four spheres flying. The things you can do with timings are endless. As you’ve seen here, you can hide and move objects, but beyond that you can do just about anything.
3.149.28.185