Friday, October 16, 2009

Side Scroller Tutorial part 3: Carrying Variables

In part 3 of this tutorial I'm going to teach you how to carry one variable from one scene to the next. It's actually pretty easy once you've gotten the hang of it.

1. Ok, first create a dynamic text box, in the instance name box type something like "lives."

2. Then in the box marked var, type the same word that you typed in the instance name box and add a "_1" to the end of it. For example, "lives_1"

3. Now on the main time line select the frame and click the Actions tab type,

stop ();

lives.text = 3;

4. Now test your movie and you'll see that the number 3 shows up in the dynamic text box. Note: It's important to remember to put ".text" after the name of your variable so that Flash will recognize it.

5. Create a new scene, on the stage create another dynamic text box and name it "lives" in the instance name box and in the var box name it "lives_2"

6. On the timeline select the frame and in the Actions tab type this,

stop ();

var lives_2 = lives_1;

This coding tells Flash to make "lives_2" equal to "lives_1"

7. Now test your movie. The first scene will say 3. Now on the tool bar press "play" to go to the next scene. you will see that it also says 3. Try changing the number in the first scene to make sure it's being carried over to the next. Note: This coding can only be used in a linear oriented game. (Or in other words, games where you can't revisit previous levels.) 

Other than carrying points from one level to the next you can also use it to display scores from previous levels in one place. Just make enough dynamic text boxes, label them in the var box and on the actions tab set them equal to the score you want them to show.
This is only the blog. If you want to go to the main web page click here