Create a game like Perfectionism with Flash – Step 6
This is the last step.
And the end of this one, we’ll have our complete Perfectionism game
The score
You get one point every time you move a square into a ring, and you lose one point every time you move a square out of a ring
To show the score, I just created a new movieclip with a dynamic text area into it just like in the picture

As you can see, the movieclip is called and linked as score while the text area is instanced as scoretxt
Now, 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | _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});
_root.attachMovie("score","score",5);
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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, 0, 2, 0, 1, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 1, 1, 1, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 2, 0, 1, 2, 0, 0, 1, 0, 1, 1);
level[18] = new Array(1, 2, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 1, 0, 1, 2, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 2, 0, 2, 1, 2, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 2, 1);
level[19] = new Array(1, 1, 2, 0, 0, 2, 1, 1, 1, 2, 0, 2, 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;
points = 0;
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) {
if (level[current_level][this.pos] == 3) {
points--;
}
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;
if (level[current_level][this.pos] == 3) {
points++;
}
score.scoretxt.text = "Score: "+points;
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);
}
}; |
Line 4: Attaching the score movieclip into the movie
Line 28: Declaring a new variable called points that will contain the score
Line 96: When the square stopped moving, I check if before moving the square was inside a ring. It’s easy: I just have to check if the level array element at pos attribute (that has not been updated yet) was 3
Line 97: In this case, subtract 1 to points variable
Line 102: Now I check if the square moved into a ring. Now that I have the pos value updated at line 100, I have to check if the level array element at pos attribute (that now is updated) is 3
Line 103: In this case, add 1 to points variable
Line 105: Updating the text in the scoretext text area into the score movieclip.
And now the game has score too!
Now, we must allow the player to make only 100 moves (swapping rows/columns and changing level count as a move) and fix a little bug that allows the player to go to previous level even if there isn’t any previous level (when he’s playing level 1) and go to next level even if there isn’s any next level (when he’s playing level 21)
Only 100 moves
After creating the moves movieclip, in the same way as for the score, just calling and linking the movieclip as moves and instancing the text area as movestxt, this is 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | _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});
_root.attachMovie("score","score",5);
_root.attachMovie("moves","moves",6);
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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, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 2, 0, 1, 2, 0, 0, 1, 0, 1, 1);
level[18] = new Array(1, 2, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 1, 0, 1, 2, 0, 0, 0, 2, 0, 0, 1, 2, 0, 1, 0, 2, 0, 2, 1, 2, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 0, 0, 0, 0, 2, 1);
level[19] = new Array(1, 1, 2, 0, 0, 2, 1, 1, 1, 2, 0, 2, 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;
points = 0;
turns = 100;
play_level(current_level);
function play_level(current_level) {
if (current_level == 1) {
prev_level._visible = false;
}
else {
prev_level._visible = true;
}
if (current_level == 21) {
next_level._visible = false;
}
else {
next_level._visible = true;
}
_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) {
if (level[current_level][this.pos] == 3) {
points--;
}
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;
if (level[current_level][this.pos] == 3) {
points++;
}
score.scoretxt.text = "Score: "+points;
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) {
if (turns>0) {
turns--;
moves.movestxt.text = "Moves: "+turns;
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();
}
}
}
else {
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) and (current_level != 21) and (turns>0)) {
turns--;
moves.movestxt.text = "Moves: "+turns;
grid.removeMovieClip();
current_level++;
play_level(current_level);
}
if ((prevlevel) and (current_level != 1) and (turns>0)) {
turns--;
moves.movestxt.text = "Moves: "+turns;
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);
}
}; |
Line 5: Attaching the moves movieclip
Line 30: Declaring a variable called turns, that will store the number of moves left, and setting it to 100
Line 33: If we are playing level 1…
Line 34: … then don’t show the previous level button
Line 37: If not, then show the previous level button
Lines 39-44: Same thing as lines 33-38, just for the next level button when we are playing on level 21
Please notice that this method will only make the buttons invisible, but they will remain clickable.
Line 195: All the code that previously was executed when the player clicked on two buttons, now it will be executed only if turns is greater than zero (the player did not spend all moves)
Line 196: If the player switched rows/columns, then drecrease turns variable
Line 197: Update the number of moves left shown onscreen
Lines 232-234: If turns is zero, then call the function reset_swap… nothing happens and the player is not longer able to swap rows/columns
Line 248: Now the condition to advance level is: nextlevel is true (as before) and the current level is not the last one (21st) and turns is greater than zero (the player has moves left)
Line 249: if the player changes the level, then decrease turns variable
Line 250: Update the number of moves left shown onscreen
Lines 255-257: Same concept as lines 248-250, just for the previous level
And finally the game is complete
Now you can optimize the code, make your custom levels, add an intro, a game over screen, a highscore tables, and whatever you want, but we made the complete game in just 300 lines of code.
Download the source code and tell me if you made some interesting game out of it.
They can be easily customized to meet the unique requirements of your project.

























This post has 7 comments
Keiran
This game is perfect, way to go!!
Nate
Perfect!
Eblup
Hey, i am having trouble with your platform physics. the guys corners have glitches so i would love for you to help. in return i will help you with a virtual camera for flash games. put in this code.this makes evreything move and youre guy stays in the same plase so now please help fix the glitches and my help with isometric games.
//camera code
_x -= _root.guy.speed;
_y -= _root.guy.gravity;
Kesh
watch out newgrounds, here they come
Ashker
Cool, good job :P.
I may make my own version of the game, and when/if i do i will post to see what you guys think.
Keep up the good work Emanuele
Viktor H
Hi Emanuele! Love your stuff.
Anyway, the RSS feed haven’t updated for me! It still says Blog in peace is the latest blog-post.
What’s up with that?
Sunil Patel
Wow! This is such a fun and addicting game. Thanks for the tutorial.
Not looking forward to all the clones though :(