Create a game like Perfectionism with Flash – Step 5

In this step we have to manage row/column swapping that in the previous step caused the game to crash if there was nothing to swap.

Moreover, until now I updated only the screen, while the level array containing the real game status is not updated. I’ll manage everything in this step

Swapping elements – part 2

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
205
206
207
208
_root.attachMovie("grid", "grid", 1, {_x:90, _y:90});
_root.createEmptyMovieClip("rays", 2);
swapping = false;
from = -1;
to = -1;
horizontal_swap = false;
vertical_swap = false;
horizontal_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
vertical_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
horizontal_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
vertical_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
level = new Array(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, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
for (x=0; x<64; x++) {
	if (level[x] == 1) {
		grid.attachMovie("cell", "cell_"+x, grid.getNextHighestDepth(), {_x:(x%8)*40, _y:Math.floor(x/8)*40});
	}
	if (level[x] == 2) {
		at = grid.attachMovie("atom", "atom_"+x, grid.getNextHighestDepth(), {_x:(x%8)*40, _y:Math.floor(x/8)*40});
		at.pos = x;
		at.moving = 0;
		at.onEnterFrame = function() {
			if (this.moving == 0) {
				if (swapping) {
					if (horizontal_swap) {
						if ((this.pos>=from*8) and (this.pos<=from*8+7)) {
							this.moving = (to-from)*40;
						}
						if ((this.pos>=to*8) and (this.pos<=to*8+7)) {
							this.moving = (from-to)*40;
						}
					}
					if (vertical_swap) {
						for (x=0; x<8; x++) {
							if (this.pos == x*8+from) {
								this.moving = (to-from)*40;
							}
						}
						for (x=0; x<8; x++) {
							if (this.pos == x*8+to) {
								this.moving = (from-to)*40;
							}
						}
					}
				}
			} else {
				if (this.moving>0) {
					if (horizontal_swap) {
						this._y += 10;
					}
					if (vertical_swap) {
						this._x += 10;
					}
					this.moving -= 10;
				}
				if (this.moving<0) {
					if (horizontal_swap) {
						this._y -= 10;
					}
					if (vertical_swap) {
						this._x -= 10;
					}
					this.moving += 10;
				}
				if (this.moving == 0) {
					level[this.pos] -= 2;
					this.pos = Math.round(this._x/40)+Math.round(this._y/40)*8;
					level[this.pos] += 2;
					reset_swap();
				}
			}
		};
	}
}
for (x=1; x<=8; x++) {
	hs = grid.attachMovie("hswapper", "hswapper_"+x, grid.getNextHighestDepth(), {_x:-20, _y:40*x-20});
	hs.pos = x;
	hs.onEnterFrame = function() {
		if (!swapping) {
			if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
				this.gotoAndStop(2);
				horizontal_hover[this.pos-1] = 1;
				for (x=0; x<8; x++) {
					vertical_click[x] = 0;
				}
			} else {
				this.gotoAndStop(1);
				horizontal_hover[this.pos-1] = 0;
			}
			if (horizontal_click[this.pos-1] == 1) {
				(this.gotoAndStop(3));
			}
		}
	};
	vs = grid.attachMovie("vswapper", "vswapper_"+x, grid.getNextHighestDepth(), {_x:40*x-20, _y:-20});
	vs.pos = x;
	vs.onEnterFrame = function() {
		if (!swapping) {
			if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
				this.gotoAndStop(2);
				vertical_hover[this.pos-1] = 1;
				for (x=0; x<8; x++) {
					horizontal_click[x] = 0;
				}
			} else {
				this.gotoAndStop(1);
				vertical_hover[this.pos-1] = 0;
			}
			if (vertical_click[this.pos-1] == 1) {
				(this.gotoAndStop(3));
			}
		}
	};
}
rays.onEnterFrame = function() {
	if (!swapping) {
		horizontal_swap = false;
		vertical_swap = false;
		this.clear();
		clicks = 0;
		this.lineStyle(2, 0x00ff00);
		for (x=0; x<8; x++) {
			if ((horizontal_hover[x] == 1) or (horizontal_click[x] == 1)) {
				this.moveTo(90, x*40+110);
				this.lineTo(410, x*40+110);
				if (horizontal_click[x] == 1) {
					clicks++;
					horizontal_swap = true;
				}
			}
			if ((vertical_hover[x] == 1) or (vertical_click[x] == 1)) {
				this.moveTo(x*40+110, 90);
				this.lineTo(x*40+110, 410);
				if (vertical_click[x] == 1) {
					clicks++;
					vertical_swap = true;
				}
			}
		}
		if (clicks == 2) {
			from = -1;
			to = -1;
			swapping = true;
			if (horizontal_swap) {
				for (x=0; x<8; x++) {
					if (horizontal_click[x] == 1) {
						if (from == -1) {
							from = x;
						} else {
							to = x;
						}
					}
				}
				if (!pieces_on_row(from) and !pieces_on_row(to)) {
					reset_swap();
				}
			}
			if (vertical_swap) {
				for (x=0; x<8; x++) {
					if (vertical_click[x] == 1) {
						if (from == -1) {
							from = x;
						} else {
							to = x;
						}
					}
				}
				if (!pieces_on_column(from) and !pieces_on_column(to)) {
					reset_swap();
				}
			}
		}
	}
};
_root.onMouseDown = function() {
	for (x=0; x<8; x++) {
		if (horizontal_hover[x] == 1) {
			horizontal_click[x] = 1-horizontal_click[x];
		}
		if (vertical_hover[x] == 1) {
			vertical_click[x] = 1-vertical_click[x];
		}
	}
};
function pieces_on_row(row) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[row*8+x]>=2) {
			res = true;
		}
	}
	return (res);
}
function pieces_on_column(col) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[col+x*8]>=2) {
			res = true;
		}
	}
	return (res);
}
function reset_swap() {
	for (x=0; x<8; x++) {
		horizontal_click[x] = 0;
		vertical_click[x] = 0;
		swapping = false;
	}
}

Line 65: This line is executed only when the square stopped moving. So before updating its pos value, we need to update the field array.

Remember in step 3 I coded the level this way:

0: empty cell
1: ring
2: square
3 (2+1): ring + square

So at line 65 to tell the array I don’t have a square anymore I just subtract 2 to the pos-th element in the level array

Line 67: After updating the pos value according to square’s position at line 66, now I add 2 to the new calculated pos-th element in the level array to tell the array I have a square at the pos-th element

Line 68: Calling a function named reset_swap that we’ll see later in this script

Lines 153-155: The same reset_swap function is called if there aren’t squares on the source row to swap and the destination row to swap. I can determine it with the pieces_on_row function that I will explain later in the script

Lines 167-169: And the same thing happens if I have no squares on the source column and the destination column, thanks to the pieces_on_column function

Line 184: Beginning of the pieces_on_row function, where you will pass the number of the row to check and will return true if there is at least one square on that row, false otherwise

Line 185: Declaring a res variable (the result) setting it to false

Lines 186-190: Scanning the entire row and setting res to true if a square has been found (when the element in the level array is greater of equal to 2)

Line 191: Returning res

Lines 193-201: pieces_on_column function, that works in the same way as pieces_on_row, just scanning a column

Line 202: Beginning of the reset_swap function

Lines 203-207: Turning off all clicks and setting swapping to false (again, I don’t know why line 206 is inside the cycle)

And here it is: now you can safely swap rows and columns and have a completely playable level

Now, I must include in my game all the levels of the original one.

Adding levels

The game has 21 levels, and converting them in order to make them playable on my game was the most boring part

I also created two movieclips with the previous level and next level arrows, as shown here

Perfectionism

now, let’s see how I changed the actionscript

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
_root.createEmptyMovieClip("rays",2);
_root.attachMovie("next_level","next_level",3,{_x:400, _y:450});
_root.attachMovie("prev_level","prev_level",4,{_x:50, _y:450});
level = new Array();
level[1] = new Array(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, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
level[2] = new Array(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, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
level[3] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 0, 0, 0, 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0, 1, 2, 2, 1, 0, 1, 2, 2, 1, 0, 0, 2, 2, 1, 0, 2, 1, 2, 2, 1, 1, 1, 2, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 2, 1, 0, 0, 2, 1, 0, 2, 2, 1, 1, 0, 2, 1, 1, 2, 0, 1);
level[20] = new Array(0, 2, 1, 0, 0, 2, 2, 2, 2, 1, 1, 1, 0, 0, 1, 0, 2, 0, 2, 0, 2, 2, 1, 0, 1, 2, 2, 1, 1, 0, 0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 2, 1, 2, 2);
level[21] = new Array(0, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 0, 0, 1, 1, 1, 2, 1, 1, 1, 0, 1, 2, 2, 2, 2, 2, 1, 0, 0, 2, 1, 1, 1, 2, 1, 0, 0, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0);
current_level = 1;
play_level(current_level);
function play_level(current_level) {
	_root.attachMovie("grid","grid",1,{_x:90, _y:90});
	swapping = false;
	from = -1;
	to = -1;
	nextlevel = false;
	prevlevel = false;
	horizontal_swap = false;
	vertical_swap = false;
	horizontal_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_hover = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	horizontal_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	vertical_click = new Array(0, 0, 0, 0, 0, 0, 0, 0);
	for (x=0; x<64; x++) {
		if (level[current_level][x] == 1 or level[current_level][x] == 3) {
			grid.attachMovie("cell","cell_"+x,grid.getNextHighestDepth(),{_x:(x%8)*40, _y:Math.floor(x/8)*40});
		}
		if (level[current_level][x] == 2 or level[current_level][x] == 3) {
			at = grid.attachMovie("atom", "atom_"+x, grid.getNextHighestDepth(), {_x:(x%8)*40, _y:Math.floor(x/8)*40});
			at.pos = x;
			at.moving = 0;
			at.onEnterFrame = function() {
				if (this.moving == 0) {
					if (swapping) {
						if (horizontal_swap) {
							if ((this.pos>=from*8) and (this.pos<=from*8+7)) {
								this.moving = (to-from)*40;
							}
							if ((this.pos>=to*8) and (this.pos<=to*8+7)) {
								this.moving = (from-to)*40;
							}
						}
						if (vertical_swap) {
							for (x=0; x<8; x++) {
								if (this.pos == x*8+from) {
									this.moving = (to-from)*40;
								}
							}
							for (x=0; x<8; x++) {
								if (this.pos == x*8+to) {
									this.moving = (from-to)*40;
								}
							}
						}
					}
				} else {
					if (this.moving>0) {
						if (horizontal_swap) {
							this._y += 10;
						}
						if (vertical_swap) {
							this._x += 10;
						}
						this.moving -= 10;
					}
					if (this.moving<0) {
						if (horizontal_swap) {
							this._y -= 10;
						}
						if (vertical_swap) {
							this._x -= 10;
						}
						this.moving += 10;
					}
					if (this.moving == 0) {
						level[current_level][this.pos] -= 2;
						this.pos = Math.round(this._x/40)+Math.round(this._y/40)*8;
						level[current_level][this.pos] += 2;
						reset_swap();
					}
				}
			};
		}
	}
	for (x=1; x<=8; x++) {
		hs = grid.attachMovie("hswapper", "hswapper_"+x, grid.getNextHighestDepth(), {_x:-20, _y:40*x-20});
		hs.pos = x;
		hs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					horizontal_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						vertical_click[x] = 0;
					}
				} else {
					this.gotoAndStop(1);
					horizontal_hover[this.pos-1] = 0;
				}
				if (horizontal_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
		vs = grid.attachMovie("vswapper", "vswapper_"+x, grid.getNextHighestDepth(), {_x:40*x-20, _y:-20});
		vs.pos = x;
		vs.onEnterFrame = function() {
			if (!swapping) {
				if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
					this.gotoAndStop(2);
					vertical_hover[this.pos-1] = 1;
					for (x=0; x<8; x++) {
						horizontal_click[x] = 0;
					}
				} else {
					this.gotoAndStop(1);
					vertical_hover[this.pos-1] = 0;
				}
				if (vertical_click[this.pos-1] == 1) {
					(this.gotoAndStop(3));
				}
			}
		};
	}
 
	rays.onEnterFrame = function() {
		if (!swapping) {
			horizontal_swap = false;
			vertical_swap = false;
			this.clear();
			clicks = 0;
			this.lineStyle(2,0x00ff00);
			for (x=0; x<8; x++) {
				if ((horizontal_hover[x] == 1) or (horizontal_click[x] == 1)) {
					this.moveTo(90,x*40+110);
					this.lineTo(410,x*40+110);
					if (horizontal_click[x] == 1) {
						clicks++;
						horizontal_swap = true;
					}
				}
				if ((vertical_hover[x] == 1) or (vertical_click[x] == 1)) {
					this.moveTo(x*40+110,90);
					this.lineTo(x*40+110,410);
					if (vertical_click[x] == 1) {
						clicks++;
						vertical_swap = true;
					}
				}
			}
			if (clicks == 2) {
				from = -1;
				to = -1;
				swapping = true;
				if (horizontal_swap) {
					for (x=0; x<8; x++) {
						if (horizontal_click[x] == 1) {
							if (from == -1) {
								from = x;
							} else {
								to = x;
							}
						}
					}
					if (!pieces_on_row(from) and !pieces_on_row(to)) {
						reset_swap();
					}
				}
				if (vertical_swap) {
					for (x=0; x<8; x++) {
						if (vertical_click[x] == 1) {
							if (from == -1) {
								from = x;
							} else {
								to = x;
							}
						}
					}
					if (!pieces_on_column(from) and !pieces_on_column(to)) {
						reset_swap();
					}
				}
			}
		}
	};
}
_root.onMouseDown = function() {
	for (x=0; x<8; x++) {
		if (horizontal_hover[x] == 1) {
			horizontal_click[x] = 1-horizontal_click[x];
		}
		if (vertical_hover[x] == 1) {
			vertical_click[x] = 1-vertical_click[x];
		}
	}
	if (nextlevel) {
		grid.removeMovieClip();
		current_level++;
		play_level(current_level);
	}
	if (prevlevel) {
		grid.removeMovieClip();
		current_level--;
		play_level(current_level);
	}
};
function pieces_on_row(row) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][row*8+x]>=2) {
			res = true;
		}
	}
	return (res);
}
function pieces_on_column(col) {
	res = false;
	for (x=0; x<8; x++) {
		if (level[current_level][col+x*8]>=2) {
			res = true;
		}
	}
	return (res);
}
 
function reset_swap() {
	for (x=0; x<8; x++) {
		horizontal_click[x] = 0;
		vertical_click[x] = 0;
		swapping = false;
	}
}
next_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		nextlevel = true;
	} else {
		nextlevel = false;
		this.gotoAndStop(1);
	}
};
prev_level.onEnterFrame = function() {
	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
		this.gotoAndStop(2);
		prevlevel = true;
	} else {
		prevlevel = false;
		this.gotoAndStop(1);
	}
};

Lines 5-25: All 21 levels of the game have been transformed into arrays

Line 26: Declaring a variable called current_level that will host the current level number. It will obviously start at 1

Line 27: Calling the function play_level that will render the current_level-th level and make it playable

Line 28: Beginning of the play_level function. As you can see, almost all previously written code has been embedded into the play_level function

Line 33: Declaring a boolean variable called nextlevel that will tell me if I have to go to the next level or not

Line 34: Same thing with the previous level

Line 42: In this line, and wherever we had level[elelement] now we have level[current_level][element].

Line 213: When the mouse is pressed, I am checking if nextlevel is true (the player clicked on the next level button, as you will see later)

Line 214: Removing the grid movieclip, with all its children

Line 215: Increasing current_level variable

Line 216: call the play_level function with the new current_level value

Lines 218-222: Same thing as lines 213-217, but going to the previous level

Line 250: Function that the next_level movieclip will execute at every frame

Line 251: If the mouse is over it…

Line 252: Showing the second frame of the movieclip (to make a rollover effect)

Line 253: Setting nextlevel to true (I am ready to advance level and I will do it if the player clicks the mouse)

Line 254: If the mouse is not over it…

Line 255: Setting nextlevel to false

Line 256: Showing the first frame of the movieclip

Lines 259-267: Same thing as lines 250-258, but managing prev_level movieclip

And that’s it! Now you can play all 21 levels!

The only thing still to be made is the moves/scoring system

Meanwhile download the sources.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
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.

8 Responses to “Create a game like Perfectionism with Flash – Step 5”

  1. JDog on April 10th, 2008 7:35 pm

    Geez, this is looking great, real fast aswell !

  2. Lolinder on April 10th, 2008 10:59 pm

    This is a problem i have gotten around, for the most part, when the lines of code are less than 100, but when its like this, its a problem. Do you intend us to copy/paste the code, or type it out by hand? If copying and pasting is your intention, than there is a problem. In my browser at least, it copys the line numbers as well, resulting in, especially in this case, several hundred deletings of numbers. Is there any way you can fix this?

    I love your tutorials, and wish there were an easier way to put the code into my actionscript window.

    Lolinder

  3. Graham on April 11th, 2008 12:00 am

    I would agree with jdog. That’s pretty much the entire game, in only five posts. Nice Job!

  4. Emanuele Feronato on April 11th, 2008 12:54 am

    Lolinder: you can click on the “plain text” button at the top of the script window to have in plain text, without line numbers.

    Or you can download the sources

  5. Nate on April 11th, 2008 4:14 am

    Oh, thanks, I didn’t see that until you mentioned it.

  6. Nate on April 11th, 2008 4:55 am

    I just finished the tut, and have to agree with the other 2, great job doing such a complex game with only 5 post!

  7. Suely (Snakesue) on April 11th, 2008 7:22 am

    Holy stupidity!!!
    Hi Emanuele,
    I think you are italian geek, programmer and nanny.
    Thanks for all tutorials and patience with us.

  8. Xodus on April 11th, 2008 5:23 pm

    wow, this is amazing, all the game i make, when i look at the as, i cant understand a thing, but your, it just so simple! Great Job!
    I just wish that after this, you would continue with the boxhead tuts.
    thanx!

Leave a Reply




flash games company