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.
-
package{ //The begin of an .as file
-
import flash.display.MovieClip; //import some libraries
-
import flash.events.Event;
-
import flash.events.KeyboardEvent;
-
-
public class Script extends MovieClip{ // start the script
-
-
private var level:Array = new Array(); // create an empty level array
-
-
private var Map_data:Data = new Data; // create a version of the Data.as
-
-
private var tiles:Array = new Array(); // create an array for the tiles
-
-
public function Script(){ // the init (will only be runned once)
-
BuildMap(); // create map
-
}
-
-
private function BuildMap(){
-
Map_data.Setup(); // setup data from extern file
-
-
level = Map_data.level1; // get data from extern file
-
-
for(var t = 0; t < level.length; t++){ //a simple for loop
-
for(var u = 0; u < level[t].length; u++){ //" "
-
if(level[t][u] != 0){ //if the data is not null
-
var new_tile:platform_tile = new platform_tile; //than build a tile
-
-
addChild(new_tile); //put it on the screen
-
-
new_tile.gotoAndStop(1); //give it the right frame
-
new_tile.x = u * 25; //give it coordinates
-
new_tile.y = t * 25;
-
-
tiles.push(new_tile); //put it in an array
-
}
-
}
-
}
-
}
-
}
-
}
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.
-
package{
-
-
public class Data{
-
-
public var level1:Array = new Array();
-
public function Setup(){
-
level1 = [
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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],
-
[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]
-
]
-
}
-
}
-
}
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.
![[del.icio.us]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/google.png)
![[StumbleUpon]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/email.png)


June 14th, 2008 at 7:14 pm
The source contains a lot more features than you will see in the tutorial.
Brart(Bart)
June 17th, 2008 at 12:14 am
[...] 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. [...]
August 12th, 2008 at 12:50 am
This is really useful, thanks for posting!
September 2nd, 2008 at 11:12 am
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