Step by step AS3 translation of Creation of a platform game with Flash – step 1

This seems to be the month of AS3 conversions.

Good, this means AS3 community is growing.

Today we’ll see how Bart de Boer translated Creation of a platform game with Flash – step 1 into AS3.

You are invited to read Creation of a platform game with Flash – step 1 to know all theory behind this engine.

All files have a main class called Script.as and the level stored in the Data.as file.

Like all good coders, Bart commented the code for our understanding.

You can also find more informations at this page in the forum

Level creation

Script.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|
 
- BuildMap()		load and create the level
 
 
extern
 
- Setup()			create levels 
 
*/
 
 
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 coördinate
						new_tile.y = t * 25;								//"				   "
 
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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]
					 ]
		}
	}
}

The Player

Script.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|
 
- create_hero()		creates hero as the var "Hero"
 
- update_hero()		check collision an move
 
- BuildMap()		load and create the level
 
 
extern
 
- Setup()			create levels 
 
*/
 
 
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 const gravity:int = 1;
	private const max_speed:int = 8;
 
	private const start_x:int = 50;
	private const start_y:int = 50;
 
	private var y_speed:int;
 
	private var level:Array = new Array();
	private var tiles:Array = new Array();
 
	private var Map_data:Data = new Data;				// create a version of the Data.as
 
	private var Hero:hero = new hero;
 
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
		}
 
		private function main(even:Event){
			update_hero();
		}
 
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
 
		private function update_hero(){
			y_speed += gravity;
 
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
 
			Hero.y += y_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
				}
			}
		}
 
 
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
 
		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++){
				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);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
 
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as remains the same

The moving player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|
 
- create_hero()		creates hero as the var "Hero"
 
- update_hero()		check collision an move
 
- BuildMap()		load and create the level
 
 
extern
 
- Setup()			create levels 
 
*/
 
 
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 const gravity:int = 1;
	private const max_speed:int = 8;
 
	private const start_x:int = 50;
	private const start_y:int = 50;
 
	private var y_speed:int;
	private var x_speed:int;
 
	private var walkspeed:int = 4;
 
	private var left:Boolean;
	private var up:Boolean;
	private var right:Boolean;
	private var space:Boolean;
 
	private var level:Array = new Array();
	private var tiles:Array = new Array();
 
	private var Map_data:Data = new Data;				// create a version of the Data.as
 
	private var Hero:hero = new hero;
 
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
 
		private function main(event:Event){
			update_hero();
		}
 
		private function key_down(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = true;
			}
			if(event.keyCode == 38){
				up = true;
			}
			if(event.keyCode == 39){
				right = true;
			}
		}
 
		private function key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
			}
		}
 
 
 
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
 
		private function update_hero(){
			y_speed += gravity;
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
 
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
 
 
 
			Hero.y += y_speed;
			Hero.x += x_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x,Hero.y - 10,true)){
					Hero.y += 1;
				}
				while(tiles[t].hitTestPoint(Hero.x + 5,Hero.y,true)){
					Hero.x -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x - 5,Hero.y,true)){
					Hero.x += 1;
				}
			}
			x_speed = 0;
		}
 
 
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
 
		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++){
				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);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
 
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as remains the same

The jumping player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|
 
- create_hero()		creates hero as the var "Hero"
 
- update_hero()		check collision an move
 
- BuildMap()		load and create the level
 
 
extern
 
- Setup()			create levels 
 
*/
 
 
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 const gravity:int = 1;
	private const max_speed:int = 8;
 
	private const walkspeed:int = 4;
	private const jumpspeed:int = 20;
 
	private const start_x:int = 50;
	private const start_y:int = 50;
 
	private var y_speed:int;
	private var x_speed:int;
 
	private var left:Boolean;
	private var up:Boolean;
	private var right:Boolean;
	private var space:Boolean;
	private var can_jump:Boolean;
 
	private var level:Array = new Array();
	private var tiles:Array = new Array();
 
	private var Map_data:Data = new Data;				// create a version of the Data.as
 
	private var Hero:hero = new hero;
 
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
 
		private function main(event:Event){
			update_hero();
		}
 
		private function key_down(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = true;
			}
			if(event.keyCode == 38){
				up = true;
			}
			if(event.keyCode == 39){
				right = true;
			}
		}
 
		private function key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
			}
		}
 
 
 
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
 
		private function update_hero(){
			y_speed += gravity;
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
			if(up && can_jump){
				y_speed -= jumpspeed;
				can_jump = false;
			}
 
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
 
 
			Hero.y += y_speed;
			Hero.x += x_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
					if(y_speed >= 0){									//my try to dont let it jump out of stage without changing the hittest
						can_jump = true;
					}
				}
				while(tiles[t].hitTestPoint(Hero.x,Hero.y - 10,true)){
					Hero.y += 1;
					y_speed = 0;
				}
				while(tiles[t].hitTestPoint(Hero.x + 5,Hero.y,true)){
					Hero.x -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x - 5,Hero.y,true)){
					Hero.x += 1;
				}
			}
			x_speed = 0;
		}
 
 
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
 
		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++){
				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);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
 
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

And Data.as remains the same

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

18 Responses to “Step by step AS3 translation of Creation of a platform game with Flash – step 1”

  1. David on June 14th, 2008 11:33 am

    Very interesting
    the same bug of beiung able to jump in midair if you walk off a platform still applies here

  2. brart on June 14th, 2008 12:24 pm

    forgot to fix :(

    In part 2 there will be no bugs anymore… and a prefect collision!!!

    brart

  3. joe on June 15th, 2008 6:00 am

    where do u download or buy flash version AS3

  4. joe on June 15th, 2008 6:01 am

    plz reply my comment

  5. houen on June 15th, 2008 3:47 pm

    @joe – http://www.adobe.com for Flash CS3 w. AS3 or the Flex SDK w. AS3

    BTW – Google is your friend ;o)

  6. brart on June 15th, 2008 9:12 pm

    Just download the “adobe flash cs3″ trail from adobe.

  7. jai on June 16th, 2008 11:57 am

    oh, perhaps i should migrate this month too
    *go with the flow*

  8. Brart on June 17th, 2008 1:55 pm

    It’s a lot faster.

    Don’t forget to check the second version of this platform tutorial at http://www.Frozenhadock.com/, because at the moment of writing it isn’t yet released at this site.

    Any problems with this script? Any one needs a better explanation?

    Brart

  9. Junxron on June 19th, 2008 4:16 pm

    This may sound noob.. but, where do you create the script object? I mean there must be somewhere you need to create a script object? Thanks…

    -Junxuan-

  10. Thousand blades on June 20th, 2008 10:22 pm

    @ Junxron

    Script object is created automatically when you run your game(constructor function is called then too), you don’t need to create it anywhere in the code.

    It’s sort of a main class of a .fla document.

  11. Alo on July 30th, 2008 7:11 pm

    Hi!
    I’m a very italian newbbie in the world of game make in Flash. Sorry for my elementary english.
    I’ve a question. If I want insert a new tile sprite, how I made?
    I create a second tile named platform_tile2, but i dont know how insert in Data.as (i think its numeber 2?) and script.as

  12. Jay on September 2nd, 2008 12:27 pm

    Hi Emanuele very good Tutorial, but i have a problem with this. Iam using flashDevelop for Developing in AS3. how can i build the Map with flashDevelop? how i put the picture into Array and using the rest of your code..? i even don’t have Flash CS3 (to expensive for me!) to build a picture and put it in the frame.. Thanks

  13. Deimos on September 12th, 2008 9:13 pm

    Hello,

    I am quite new to flash CS3 but there is something I need to ask you:
    How do you “unite” both *.as files (in your case “Data” and “Script”) so that classes defined on one can be instanced on another?
    I want to implement my own game engine, no ofense, your’s great, but I wanna make sure I can understand all of my code and to do that I have to write it myself :) besides, I wanna make it top-view for an RPG game :3 (my dream game!!)

    In my case I have the .fla file named “tileStuffs”, the main code in an .as called tileStuffs and custom classes defined in another file called “customClasses.as” now, each (FRIKEN TIME) I try to instantiate one of my custom classes (like “Item” or “Tile”) I get the 1046 error code: “Type was not found or was not a compile-time constant: Tile.”

    Can you pleaaase tell me how did you do that with Data.as??

    PS: I tried using #include “customClasses.as” but then it said that packages cannot be nested.

    Thank you in advance and Best Regards

    -Deimos

Leave a Reply




Trackbacks

  1. AS3.0 tutorial - Platform creation tutorial 1 - OOP | FrozenHaddock on June 14th, 2008 1:43 pm

    [...] 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 [...]

  2. Step by step AS3 translation of Creation of a platform game with Flash - step 1.5 : Emanuele Feronato - italian geek and PROgrammer on June 22nd, 2008 9:36 am

    [...] Posted by Emanuele Feronato on 06/22/08 in Actionscript 3, Flash, Game design, Users contributions According to Bart de Boer, this is the step 1.5 of the Step by step AS3 translation of Creation of a platform game with Flash. [...]

  3. Step by step AS3 translation of Creation of a platform game with Flash - step 1.5 : Emanuele Feronato - italian geek and PROgrammer on June 22nd, 2008 9:36 am

    [...] According to Bart de Boer, this is the step 1.5 of the Step by step AS3 translation of Creation of a platform game with Flash. [...]

  4. pixelplacement.com » Step by step AS3 OOP platform game engine on August 14th, 2008 9:43 pm

    [...] Read it here. [...]

  5. Adobe Flash CS3- AS3 and AS2 game tutorials roundup | Lemlinh.com on August 19th, 2008 6:30 pm

    [...] According to Bart de Boer, this is the step 1.5 of the Step by step AS3 translation of Creation of a platform game with Flash. [...]

flash games company