Well, I guess I invited this week's subject with my comment, "We'll move on to new subjects next week (unless any questions pop up)." Questions That Have Popped Up
Please note the "unless questions pop up" part. Again, a reader has sent an excellent question. As usual, I am more than happy to dedicate a column to answering questions put forth by any reader. I also think that other readers will find this week's tutorial interesting as it is based on two previous tutorials.
The question posed to me was:
"Since we have covered changing the color of sprites using the Tools window, and we've covered the basic use of the random commands in Director, can we use the random commands to randomly change the color of a sprite?"The answer to this question is yes, but the Lingo may not look like you'd expect. As always, especially in Director, there is more than one way to perform any task. This week, we'll look at the way I would suggest (and did so in my reply to this question), and show an example that uses Lingo to randomly change the colors of a sprite. As we did in the first tutorial, we will use the simply shocking logo for the subject of our Shockwave.
Steps for using Lingo to randomly change the color of a sprite
Step 1: The first thing we want to do is import a Pict (or BMP) file to use as the sprite in our Shockwave. It is important that you use a one color sprite for this Shockwave, and that the color be Black if possible.Step 2: Set the size of the stage to the dimensions of your Pict (or BMP) file. This will help to reduce the size of your final Shockwave. Now place the Pict (or BMP) you imported onto the stage.
Step 3: Put the following Lingo script into the frame script at frame 1 in the score.
on enterFrame
set the foreColor of sprite 1 to (2 + random(250))
updatestage
on exitFrame
go to the frame
endThis script will cause the sprite to change color randomly in our Shockwave, as seen in the example below:
Now, let's discuss in a little more detail how the Lingo works. Most of the Lingo in this script has been covered in previous tutorials, so we'll just look at the two major lines that cause the color to randomly change in our project.
set the foreColor of sprite 1 to (2 + random(250))
This is the line of the script that actually changes the forecolor of the sprite. The "2" in this line represents the number of the color that will be used as a starting point. The "+ random" tells our script that we will add some other number to the "2" to generate a random number that represents the color to be used when changing the color of the sprite. The "250" tells the script to pick a number between 1 and 250 to use as the number to be added to the "2".
This means you can control your random color effects by changing the "2" to a number that represents a color you choose to start with, and then changing the "250" to a smaller number. For example, the movie below shows the effect when both the "2" and the "250" are changed to 25:
updatestage
This is a simple command, but because we haven't previously covered it, I'll give a short explanation. This Lingo command updates the stage. It seems simple, but many changes to the stage that are controlled by Lingo may not playback properly if this command is left out. Try deleting it from your tutorial project and see what happens.I hope this has been useful to those of you who wondered about randomly changing the color of sprites.
Next week, we will cover some new Shockwave materials. Until then, feel free to contact me if you need any Shockwave questions, or questions about this column, answered.