AS3.0 tutorial - Platform creation tutorial 1 - OOP

Today’s tutorial is on Actionscript 3, and it comes from Bart de Boer, who has written another tutorial for Emanuele Feronato which I’d suggest you check out too, you’ll find it here, and I’d say it’s a great starting point for learning AS3.

Now I’ll hand you over to Bart-

Let’s take a look at part one,

First how to setup an extern .as.
In flash CS3 go to “document class”, and fill in the name of the main file. In this tutorial it’s called “Script”.

Now flash will execute the code.

  1. package{  //The begin of an .as file
  2. import flash.display.MovieClip;  //import some libraries
  3. import flash.events.Event;
  4. import flash.events.KeyboardEvent;
  5.  
  6. public class Script extends MovieClip{  // start the script
  7.  
  8. private var level:Array = new Array();  // create an empty level array
  9.  
  10. private var Map_data:Data = new Data;  // create a version of the Data.as
  11.  
  12. private var tiles:Array = new Array();  // create an array for the tiles
  13.  
  14. public function Script(){  // the init (will only be runned once)
  15. BuildMap();  // create map
  16. }
  17.  
  18. private function BuildMap(){
  19. Map_data.Setup();  // setup data from extern file
  20.  
  21. level = Map_data.level1;  // get data from extern file
  22.  
  23. for(var t = 0; t < level.length; t++){  //a simple for loop
  24. for(var u = 0; u < level[t].length; u++){  //"               "
  25. if(level[t][u] != 0){  //if the data is not null
  26. var new_tile:platform_tile = new platform_tile;  //than build a tile
  27.  
  28. addChild(new_tile);  //put it on the screen
  29.  
  30. new_tile.gotoAndStop(1);  //give it the right frame
  31. new_tile.x = u * 25;  //give it coordinates
  32. new_tile.y = t * 25;
  33.  
  34. tiles.push(new_tile);  //put it in an array
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }

Package {
This is the first line in every extern as3.0 script.

import …
Imports the three most important flash libraries, in the first part we will only use one; “flash.display.MovieClip”.

public class Script extends MovieClip {
Let’s start the code

Now we add a two Arrays, level and tiles:
- The array “level” will contain the shape this level has.
- The array “tiles” will contain all the tiles of this level.

Now we add the 2de script. We calles it Map_data and it contains the data from the script “Data.as”.

All those variables are “private”, so we cann’t acces them from an other script.

public function Script {
This function will only be executed by the .fla ->”once”<-;

It only calls to the function “BuildMap”.

Let’s look at the function BuildMap. It starts with “Map_data.setup();”. It calls to a function in the 2de .as file.

level = Map_data.level1;
Gives the information from Map_data to level.

From now on there are only three things different than that from Emanuel;

1 - var new_tile:platform_tile = new platform_tile;
Same as the “.addMovieClip();” from as2.0.

2 - addChild(new_tile);
Let’s show the tiles on the screen.

3 - tiles.push(new_tile);
Put it in the tiles array, so we can add a hitTest in the next part.

Now we will look at the 2de actionscript file.

  1. package{
  2.  
  3. public class Data{
  4.  
  5. public var level1:Array = new Array();
  6. public function Setup(){
  7. level1 = [
  8. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  9. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  10. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  11. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  12. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  13. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
  14. [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  15. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  16. [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  17. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  18. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  19. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  20. [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  21. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  22. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  23. ]
  24. }
  25. }
  26. }

This tutorial is quite short…

When some script (Script.as) execute the function “setup_script” than it will create level one.

Brart

The source consists of 3 files.
- file 1 the .fla: only used for symbols
- file 2 main.as: the script itself
- file 3 Data.as: will contain the level-data

Download the source here.

———–

Keep an eye out for more tutorials by Bart here, and over on Emanuele Feronato! His first tutorial has persuaded me to jump into learning AS3, and I shall keep you updated on my progress over the summer.

-->

4 Responses to “AS3.0 tutorial - Platform creation tutorial 1 - OOP”

  1. brart(Bart) Says:

    The source contains a lot more features than you will see in the tutorial.

    Brart(Bart)

  2. AS3.0 tutorial - Platform creation tutorial 2 | FrozenHaddock Says:

    [...] AS3.0 tutorial - Platform creation tutorial 2 Published by Tazzydevil XIII on Jun 17th, 2007 in AS3, Games, Tutorials, User Contributions with No Comments Here’s part 2 of Bart de Boer’s translation of Emanuele Feronato’s platform game creation tutorial into AS3. For part one, see here. [...]

  3. megajosh2 Says:

    This is really useful, thanks for posting!

  4. Jay Says:

    Hi i wanna ask you something. what if iam using flashDevelop or flex to build the Map? how i put the picture into Array..? i hope you can answer it, cos i don’t have Flash CS3 to build a picture and put it in the frame.. Thanks

Leave a Reply