Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

Create a Flash game like Talesworth – Step 4: One-way doors

In this 4th step we’ll cover one-way doors.

One-way doors are tiles the hero can walk only once, before they become unwalkable.

First, let me show you a fix to the script shown in step 3.

In the example, the loot disappears when the hero sees it, he does not have to touch it. Since I make the loot disappear setting loot_placed variable to false, now I set it that way only when the hero is on the same cell where the loot is placed.

And now, let’s talk about one-way doors.

When you have a lot of items on your map, such as doors, keys, enemies, traps and so on, I suggest you to create various layers of the same map. This will make your life easier and will allow you to add more and more stuff on the map without rewriting that much code.

That’s why I created anoter array, of the same size as map array, called itm. A value of 1 in itm array means there is an one-way door.

When the hero walks on a tile with an one-way door, the tile is set as unwalkable setting to 1 the value in the map array.

This is the source code:

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	public class talesworth5 extends Sprite {
		public var map,itm:Array=new Array();
		public var hero_pos:Array= new Array();
		public var wall:wall_mc;
		public var floor:floor_mc;
		public var hero:hero_mc;
		public var oneway:oneway_mc;
		public var key_pressed:int=0;
		public var hero_is_moving:Boolean=false;
		public var hero_x_dir:int=0;
		public var hero_y_dir:int=0;
		public var steps:int=4;
		public var tile_size:int=32;
		public var walk_dir:int=2;
		public var loot:loot_mc = new loot_mc();
		public var loot_placed:Boolean=false;
		public function talesworth5():void {
			map=[[0,1,1,1,1,1,1,1,1,1,1,1],[0,1,2,2,2,2,2,2,2,1,1,1,1],[0,1,1,1,1,2,1,1,2,1,1,2,1,1],[0,0,1,1,1,2,1,1,2,1,1,2,1,1],[0,0,0,1,1,2,1,1,2,1,1,2,1,1,1],[0,0,0,1,1,2,2,2,2,2,1,2,2,2,1],[0,0,1,1,1,2,1,1,1,2,1,2,1,1,1,1],[0,0,1,1,1,2,1,1,1,1,1,2,1,1,1,1],[0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,1],[0,0,1,2,1,1,2,1,1,1,1,1,1,1,2,1],[0,0,1,2,2,2,2,1,1,1,1,1,0,1,2,1],[0,0,1,1,1,1,1,1,1,1,1,0,0,1,1,1]];
			itm=[[0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
			hero_pos=[3,1];
			draw_map();
			place_hero();
			addChild(loot);
			stage.addEventListener(Event.ENTER_FRAME,on_enter_frame);
			stage.addEventListener(MouseEvent.CLICK,on_mouse_click);
		}
		public function draw_map():void {
			for (var i:int=0; i<map.length; i++) {
				for (var j:int=0; j<map[i].length; j++) {
					switch (map[i][j]) {
						case 1 :
							wall = new wall_mc();
							wall.y=i*tile_size;
							wall.x=j*tile_size;
							addChild(wall);
							break;
						case 2 :
							floor=new floor_mc();
							floor.name="floor_"+j+"_"+i;
							floor.y=i*tile_size;
							floor.x=j*tile_size;
							addChild(floor);
							break;
					}
					switch (itm[i][j]) {
						case 1 :
							oneway = new oneway_mc();
							oneway.y=i*tile_size;
							oneway.x=j*tile_size;
							oneway.alpha=0.5;
							addChild(oneway);
							break;
					}
				}
			}
		}
		public function place_hero():void {
			hero = new hero_mc();
			hero.x=tile_size*(hero_pos[0]);
			hero.y=tile_size*(hero_pos[1]);
			addChild(hero);
		}
		public function on_mouse_click(e:MouseEvent):void {
			if (map[loot.y/32][loot.x/32]==2) {
				loot_placed=true;
			}
		}
		public function on_enter_frame(e:Event):void {
			if (! loot_placed) {
				var mouse_tile_x:int=Math.floor(mouseX/tile_size);
				var mouse_tile_y=Math.floor(mouseY/tile_size);
				loot.x=mouse_tile_x*tile_size;
				loot.y=mouse_tile_y*tile_size;
			} else {
				if (! hero_is_moving) {
					// looking for the closest wall to the right
					var i:int=hero_pos[0];
					while (map[hero_pos[1]][i]==2) {
						i++;
						if (hero_pos[1]==loot.y/32&&i==loot.x/32) {
							walk_dir=2;
							break;
						}
					}
					// looking for the closest wall to the left
					i=hero_pos[0];
					while (map[hero_pos[1]][i]==2) {
						i--;
						if (hero_pos[1]==loot.y/32&&i==loot.x/32) {
							walk_dir=0;
							break;
						}
					}
					// looking for the closest wall going  up
					i=hero_pos[1];
					while (map[i][hero_pos[0]]==2) {
						i--;
						if (i==loot.y/32&&hero_pos[0]==loot.x/32) {
							walk_dir=1;
							break;
						}
					}
					// looking for the closest wall going down
					i=hero_pos[1];
					while (map[i][hero_pos[0]]==2) {
						i++;
						if (i==loot.y/32&&hero_pos[0]==loot.x/32) {
							walk_dir=3;
							break;
						}
					}
				}
			}
			switch (walk_dir) {
				case 0 :
					walk(-1,0);
					break;
				case 1 :
					walk(0,-1);
					break;
				case 2 :
					walk(1,0);
					break;
				case 3 :
					walk(0,1);
					break;
			}
			if (hero_is_moving) {
				hero.x+=tile_size/steps*hero_x_dir;
				hero.y+=tile_size/steps*hero_y_dir;
				if ((hero.x+hero.y)%tile_size==0) {
					hero_is_moving=false;
					hero_pos[0]+=hero_x_dir;
					hero_pos[1]+=hero_y_dir;
					if (hero_pos[0]==loot.x/32&&hero_pos[1]==loot.y/32) {
						loot_placed=false;
					}
					if (itm[hero_pos[1]][hero_pos[0]]==1) {
						map[hero_pos[1]][hero_pos[0]]=1;
						oneway.alpha=1;
					}
				}
			}
		}
		function walk(px,py):void {
			if (map[hero_pos[1]+py][hero_pos[0]+px]==2) {
				hero_is_moving=true;
				hero_x_dir=px;
				hero_y_dir=py;
			} else {
				switch (walk_dir) {
					case 0 :
						if (map[hero_pos[1]-1][hero_pos[0]]==2) {
							walk_dir=1;
						} else {
							if (map[hero_pos[1]+1][hero_pos[0]]==2) {
								walk_dir=3;
							} else {
								walk_dir=2;
							}
						}
						break;
					case 1 :
						if (map[hero_pos[1]][hero_pos[0]+1]==2) {
							walk_dir=2;
						} else {
							if (map[hero_pos[1]][hero_pos[0]-1]==2) {
								walk_dir=0;
							} else {
								walk_dir=3;
							}
						}
						break;
					case 2 :
						if (map[hero_pos[1]+1][hero_pos[0]]==2) {
							walk_dir=3;
						} else {
							if (map[hero_pos[1]-1][hero_pos[0]]==2) {
								walk_dir=1;
							} else {
								walk_dir=0;
							}
						}
						break;
					case 3 :
						if (map[hero_pos[1]][hero_pos[0]-1]==2) {
							walk_dir=0;
						} else {
							if (map[hero_pos[1]][hero_pos[0]+1]==2) {
								walk_dir=2;
							} else {
								walk_dir=1;
							}
						}
						break;
				}
			}
		}
	}
}

And this is the result:

Try to place loots to make the hero walk on the pink square, and see what happens when the square turns red.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.20 out of 5)
Loading ... Loading ...
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.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 3 comments

  1. Alexandre Colella

    on June 10, 2010 at 11:46 pm

    Hi Emanuelle! I’m from Brazil and I’m looking your blog!
    It’s a great form to learn Flash.
    I am beginner in Flash game develop and I’m learning a lot with you.
    Thanks a lot!!

  2. anlik

    on June 13, 2010 at 11:43 am

    Hi Emanuelle! Please visite:

    http://www.kongregate.com/games/athenaBianBian/3d-super-girl

    Can you publish a post about how to do this game?

  3. Awsome

    on March 14, 2011 at 6:46 pm

    Can you continue this tutorial.
    Like adding gates and keys and multiple lvls?