Terrain Scrolling
(Terrain scrolling requires previous tutorials: Detect Angle, Game Gun).
In this tutorial, the Game Gun is used as simply one more piece of terrain that can be scrolled.

All this was done in a 20K ".swf" file:
The final game is much larger -- around 350K. That is only because of all the high quality graphics I introduced instead of flat colors for terrain and game pieces. I purposely got rid of all that so I could see just how small the code was at this point.

Wait 10 seconds for terrain scrolling to start...

Actual Flash code is in green.

Variables
Here are the extra variables on the main timeline that are needed for the terrain scrolling...

Comment: ----------------------------------------------
Comment: terrain variables
Comment: ----------------------------------------------
Set Variable: "scrheight" = 450
Comment: tmov=terrain scroll speed
Set Variable: "tmov" = 3
Set Variable: "twidth" = 9
Set Variable: "theight" = 9

These are some simple variables to set up how wide our terrain is. The terrain data variables below are 9 objects wide. 

Comment: NOTE: because all items are "LIVE" (they want to run down your screen) I have to tell the original items (the ones you see on the pasteboard) to "stop". When duplicated, movie clips automatically (a) become visible even if their original wasn't and (b) play.
Set Variable: "n" = 32
Loop While (n<=128)
Begin Tell Target (Chr(n))
Comment: cheat: because all my movie clips have a single character as their name and are between 32 and 128 on the ASCII chart I can simply tell ALL those clips to STOP (whether they exist or not).
Stop
End Tell Target
Set Variable: "n" = n+1
End Loop

These are some terrain data. Each alpha/numeric character represents a specific square of terrain. If you look on the pasteboard on the main timeline of the "FLA" you will see a whole bunch of different terrain pieces. Each one is a movie clip named with a single character... the "terrain controller" checks the text variables below to see what terrain it shold display. It then grabs the movie clip with the same name as the character it finds in the terrain data, duplicates it, and puts it in the correct spot at the top of the scrolling area.

The variable "last_terrain" is used by the terrain controller so it knows when to either (stop) or (as in this sample) start over at the beginning again. You can see how easy it would be to add in new objects that you create, edit existing terrain, or make the terrain wider or narrower. The more terrain you have on your screen the slower it will scroll. The more complicated your terrain is the slower it will scroll.

EDITING HINT: You can make your original text in a text editing program that will allow you to set the font to "Courier" or some other fixed-width font. That way, letters like "i" and "x" will be the same width, making the whole 9 character (or however many you have) line up correctly, and easier to edit.

Comment: ----------------------------------------------
Comment: TERRAIN DATA
Comment: ----------------------------------------------
Set Variable: "last_terrain" = 50
Set Variable: "t50" = "000000000"
Set Variable: "t49" = "000000000"
Set Variable: "t48" = "00000Z000"
Set Variable: "t47" = "000000000"
Set Variable: "t46" = "000000000"
Set Variable: "t45" = "AG0000000"
Set Variable: "t44" = "D00000000"
Set Variable: "t43" = "000061300"
Set Variable: "t42" = "001051400"
Set Variable: "t41" = "001000000"
Set Variable: "t40" = "001300530"
Set Variable: "t39" = "005100010"
Set Variable: "t38" = "300500010"
Set Variable: "t37" = "130000000"
Set Variable: "t36" = "J51300006"
Set Variable: "t35" = "I61400061"
Set Variable: "t34" = "114000611"
Set Variable: "t33" = "140006114"
Set Variable: "t32" = "40006AA4G"
Set Variable: "t31" = "00005AA30"
Set Variable: "t30" = "000005113"
Set Variable: "t29" = "000000711"
Set Variable: "t28" = "000000051"
Set Variable: "t27" = "0I00I0007"
Set Variable: "t26" = "07G620000"
Set Variable: "t25" = "070520000"
Set Variable: "t24" = "0J0000000"
Set Variable: "t23" = "000000000"
Set Variable: "t22" = "000000000"
Set Variable: "t21" = "000071200"
Set Variable: "t20" = "000005400"
Set Variable: "t19" = "000000000"
Set Variable: "t18" = "000CE0000"
Set Variable: "t17" = "000AA0000"
Set Variable: "t16" = "000BD0000"
Set Variable: "t15" = "68F00000I"
Set Variable: "t14" = "11F0000CA"
Set Variable: "t13" = "140000611"
Set Variable: "t12" = "400000511"
Set Variable: "t11" = "000000005"
Set Variable: "t10" = "000000000"
Set Variable: "t9" = "000063000"
Set Variable: "t8" = "006112300"
Set Variable: "t7" = "0054G5400"
Set Variable: "t6" = "000000000"
Set Variable: "t5" = "001000000"
Set Variable: "t4" = "000111000"
Set Variable: "t3" = "100000010"
Set Variable: "t2" = "000010000"
Set Variable: "t1" = "001000010"

Terrain Scrolling Code

There is an object on the main timeline called "terrain controller". This object -- you guessed it -- controls all the terrain. That is, it decides when to duplicate terrain and where to put it. Each terrain piece then automatically lives out its little life scrolling down the screen, then disappearing, all by itself.

Comment: the marker will tell us when to draw the next row of terrain
If (GetProperty("../marker",_y)>45)
Go to and Play ("draw_next")
End If

The marker simply moves itself down the screen exactly as a terrain piece does....
Since all the terrain pieces are exact squares, 50 pixels wide, and /:tmov is "5", once the maker is beyond 45 we reset it to zero (that happens later in the terrain controller). It is essentially just another terrain piece, but with no graphics. I should have done this with variables for tidyness' sake. It should have read "If Marker's Y position is greater than (50-/:tmov) Then..." but it does not. The majority of the code in this piece is tidy, but this is something that could be cleaned up.

Here is all the "marker" does to move itself...

Set Property ("", Y Position) = GetProperty("",_y)+/:tmov

Now let's look at the frame "draw_next" (as mentioned a few lines of code above...)

Comment: NOTE: important! Every object on the screen must have a unique "depth". If a second object is duplicated and its depth is set to the same as an existing movie clip, then the original one vanishes and the new one takes its place. There will never be more than 1000 terrain items at one time. I therefore start the terrain depth IDs at 2000 and keep it between 2000 and 3000. You should keep track of what objects (user laser shots, gun laser shots, terrain pieces) are in which range of "depths".

We increase "row" -- which we will use to cunsult the list of terrain data variables. Since in Flash undeclared variables are considered to have a value of "0" the first time this next line ever runs, it increases "row" to a value of 1.

Set Variable: "row" = row+1
Set Variable: "column" = 1
Comment: ----------------------------------------------
Comment: row > maxterrain?
Comment: ----------------------------------------------
Comment: MORE than 1 level?..........
Comment: here is where you would, rather than reset the row counter... you would (a) reset the row counter and (b) switch the variable name from "t" to "t2" or something like that -- and create a whole new terrain variable set all starting with "t2".
If (row>/:last_terrain)
Set Variable: "row" = 1
End If

For each row (horizontal area, 9 squares long), there is a total of 9 items. (..:twidth is set to 9).

Loop While (column<=..:twidth)
Comment: ----------------------------------------------
Comment: work on terrain
Comment: ----------------------------------------------
Set Variable: "item" = Substring (Eval("/:t"&row),column,1)

We read the first terrain variable defined on the main timeline. It looks like this: Set Variable t1="001000010"
If you look on the main pasteboard, there is a movie clip called "1" that is a blue square. There is no movie clip called "0". I use "0" to represent free space -- in other words, nothing will get duplicated. 

Comment: SPECIAL CASE: I did not program the gun concept well. Perhaps in a later revision. Currently, you can only have one gun on the screen at a time. And the gun is a special terrain object because It must have a very specific name -- "gun". That way all the other code works. ie: the user laser shots (EXTRA NOTE: there are no laser shots in this tutorial, but if there were, they'd need a specific object name to check ie: "gun") keep checking to see if they have hit something called "gun". This won't work if it has the name "t"&tnum. etc. 
If (Ord(item)=71)
Comment: 71 = "G" -- the gun
Set Variable: "name" = "gun"

Else
Set Variable: "name" = "t"&tnum

Here we assigned each item about to be duplicated its name. Since the gun is the only character that needs a special name (in this demo it does not...) the rest of the characters simply get a unique name.


End If
If (Ord(item)<>48)

Since there is no movie clip called "0" I could just let the code run anyways and it would work. But the extra CPU time to run the code can be avoided here. So I do.

Comment: 48 = "0" (no terrain) -- no use wasting CPU power to set up nothing.
Comment: ----------------------------------------------
Comment: place terrain
Comment: ----------------------------------------------
Set Variable: "tnum" = tnum+1
If (tnum>2999)
Set Variable: "tnum" = 2500
End If

Here we recycled terrain names.

If (Ord(item)<>71)
Duplicate Movie Clip ("../"&item, name, tnum)
Set Property ("../"&name, X Position) = 50*(column-1)
Set Property ("../"&name, Y Position) = 0
Else
Set Property ("../"&name, X Position) = 50*(column-1)+25
Set Property ("../"&name, Y Position) = 25
End If
End If

We continue doing these things until we've reached the end of the column. Then, we reset the marker (which we then wait for until it is in the correct spot to start placing terrain squares again.)

Set Variable: "column" = column+1
End Loop
Set Property ("../marker", Y Position) = 0
Go to and Play ("wait")

Terrain Pieces

Remember, terrain pieces each move themselves down the screen.
When they go beyond  the height of the screen, they vanish.

Set Property ("", Y Position) = GetProperty("",_y)+/:tmov
If (GetProperty("",_y)>/:scrheight)
Remove Movie Clip ("")
End If

: )
by: