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 3: The loot

One of the main features of Talesworth is the hero keeps running according to his rules until he spots a loot. Then he changes its way to grab the loot. In part 1 I showed you how to make the hero walk like in the original game, and in part 2 I introduced the line of sight.

Now it’s time to place the loot and make the hero change his way. Greedy.

First, I have to say the movement routine I showed in step 1 is buggy. Not that buggy, it’s just in some circumstances the hero does not behave like in the original game.

So I changed the script. Now it works this way:

* The hero walk straight until he finds a wall
* He tries to turn right
* If he can’t turn right, he tries to turn left
* If he can’t turn left, he returns back

As for the loot, I scan in the four directions as showed in step 2, then if I find a loot in one direction, I simply set the hero direction according to loot position.

In this new script I simply created a new movieclip called loot_mc and made it work this way:

* You can only place the loot on a walkable tile (marked with 2)
* You can only place ONE loot
* Once you placed a lot, you can’t move it until the hero sees it
* When the hero sees the loot, he changes his direction

A bit different from the original game at the moment, but at this time I just want to create the loot engine. And it works… in real time… here it is the script:

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
package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	public class talesworth5 extends Sprite {
		public var map: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 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;
		// the loot movieclip
		public var loot:loot_mc = new loot_mc();
		// variable to determine whether the loot is placed on the map or not
		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]];
			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;
					}
				}
			}
		}
		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 {
			// when I click the mouse, I check if the tile I clicked on is a walkable tile
			if (map[loot.y/32][loot.x/32]==2) {
				// then, I place the loot
				loot_placed=true;
			}
		}
		public function on_enter_frame(e:Event):void {
			if (! loot_placed) {
				// if I did not place the loot, then move the loot according to mouse position
				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 the loot has been placed...
				if (! hero_is_moving) {
					// looking for the closest wall to the right, or for a loot
					var i:int=hero_pos[0];
					while (map[hero_pos[1]][i]==2) {
						i++;
						// if I find a loot on the right, then go to the right
						if (hero_pos[1]==loot.y/32&&i==loot.x/32) {
							walk_dir=2;
							loot_placed=false;
							break;
						}
					}
					// looking for the closest wall to the left, or for a loot
					i=hero_pos[0];
					while (map[hero_pos[1]][i]==2) {
						i--;
						// if I find a loot on the left, then go to the left
						if (hero_pos[1]==loot.y/32&&i==loot.x/32) {
							walk_dir=0;
							loot_placed=false;
							break;
						}
					}
					// looking for the closest wall going up, or for a loot
					i=hero_pos[1];
					while (map[i][hero_pos[0]]==2) {
						i--;
						// same thing as before, going up
						if (i==loot.y/32&&hero_pos[0]==loot.x/32) {
							walk_dir=1;
							loot_placed=false;
							break;
						}
					}
					// looking for the closest wall going down, or for a loot
					i=hero_pos[1];
					while (map[i][hero_pos[0]]==2) {
						i++;
						// same thing as before, going down
						if (i==loot.y/32&&hero_pos[0]==loot.x/32) {
							walk_dir=3;
							loot_placed=false;
							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;
				}
			}
		}
		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 {
				// new direction change routine
				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:

Play placing the loot with the mouse clicking on a tile in hero’s line of sight, and see how he changes direction. Then place the loot again.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 3.36 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 2 comments

  1. Monkios

    on June 7, 2010 at 3:13 pm

    When the hero sees the loot, it disappears.
    The hero doesn’t have to touch it.

  2. Create a Flash game like Talesworth – Step 4: One-way doors - Emanuele Feronato

    on June 10, 2010 at 6:03 pm

    [...] Create a Flash game like Talesworth – Step 3: The loot [...]