Adding a new twist to the prototype of a Flash game like Poux
Do you remember the Poux tutorial series?
If not, then read parts 1, 2 and 3 and take a look at the finished project known as Christmas Couples.
I always say using prototypes to create games is a good idea, and the almost 7,000,000 plays Christmas Couples had since its release confirm my theory.
Anyway, the "twist" is you must remove adjacent tiles by drawing on them.
You must connect two or more adjacent tiles with the same color with the "pen", and if you have the precision of a surgeon, you can also connect tiles diagonally!
Oh, well, take the commented actionscript
-
// declaration of the array that will contain the game
-
field = Array();
-
// declaration of the arrat that will contain the tiles to be removed
-
tiles_to_remove = Array();
-
// number of frames to pass before inserting the first row
-
interval = 1;
-
// tiles placed so far
-
tiles_placed = 0;
-
// loop that initializes the field
-
for (x=0; x<10; x++) {
-
field[x] = Array();
-
for (y=0; y<10; y++) {
-
field[x][y] = 0;
-
}
-
}
-
// creation of the movieclip where to place tiles
-
createEmptyMovieClip("tiles", 1);
-
// creation of the movieclip where to draw
-
createEmptyMovieClip("drawing", 2);
-
// flag that states if the I can draw or not
-
can_draw = false;
-
// function that places a line of tiles in the bottom of the field
-
function place_line() {
-
// 1 row = 10 tiles
-
for (x=0; x<10; x++) {
-
tiles_placed++;
-
// if the spot is not empty, must shift the colum
-
if (field[x][0] != 0) {
-
push_blocks(x);
-
}
-
// placing the tile
-
tile = tiles.attachMovie("tile", "tile_"+tiles_placed, tiles_placed, {_x:10+32*x, _y:300});
-
// assigning a random color (frame) to the tile
-
num = Math.floor(Math.random()*8)+1;
-
tile.gotoAndStop(num);
-
field[x][0] = tiles_placed;
-
}
-
}
-
// function to be executed at every frame
-
_root.onEnterFrame = function() {
-
// decreasing the interval
-
interval--;
-
// placing a row and set a new interval if the interval reached zero
-
if (interval<0) {
-
interval = 200;
-
place_line();
-
}
-
// if I can draw...
-
if (can_draw) {
-
// determining on which tile I should draw
-
x_tile_drawing = Math.floor((_root._xmouse-10)/32);
-
y_tile_drawing = -Math.floor((_root._ymouse-300)/32);
-
if (field[x_tile_drawing][y_tile_drawing] != 0) {
-
// draw a line until my mouse
-
drawing.lineTo(_root._xmouse, _root._ymouse);
-
// determining the color of the tile I am drawing on
-
current_color = _root.tiles["tile_"+field[x_tile_drawing][y_tile_drawing]]._currentframe;
-
// if the current color is different than the one I started drawing on...
-
if (current_color != starting_color) {
-
// stop drawing
-
stop_drawing();
-
} else {
-
// if the current color the same as the one I started drawing on...
-
// insert_it determines if I have to insert the current tile in the array of tiles to be removed
-
insert_it = true;
-
// scanning the array of tiles to be removed
-
for (x=0; x<tiles_to_remove.length; x++) {
-
// if the tile I am drawing on is already in the array...
-
if (tiles_to_remove[x] == y_tile_drawing*10+x_tile_drawing) {
-
// don't insert the tile in the array of tiles to be removed
-
insert_it = false;
-
// exit the cycle
-
break;
-
}
-
}
-
// if I have to insert the tile in the array of tiles to be removed...
-
if (insert_it) {
-
// push the tile in the array
-
tiles_to_remove.push(y_tile_drawing*10+x_tile_drawing);
-
}
-
}
-
}
-
}
-
};
-
// function that shifts the column of blocks
-
function push_blocks(col_number) {
-
// scanning backward the columns
-
for (i=9; i>=0; i--) {
-
// if the place is not empty...
-
if (field[col_number][i] != 0) {
-
// if I don't have a column with 10 blocks...
-
if (i != 9) {
-
// shift the blocks
-
field[col_number][i+1] = field[col_number][i];
-
_root.tiles["tile_"+field[col_number][i]]._y -= 32;
-
} else {
-
// if I have more than 10 blocks in a column, remove the 10th block
-
// In a normal game, it would be "game over"
-
_root.tiles["tile_"+field[col_number][i]].removeMovieClip();
-
}
-
}
-
}
-
}
-
// when I click...
-
onMouseDown = function () {
-
// obtain tile clicked according to mouse position
-
x_tile_clicked = Math.floor((_root._xmouse-10)/32);
-
y_tile_clicked = -Math.floor((_root._ymouse-300)/32);
-
// if the tile clicked exists...
-
if ((x_tile_clicked>=0) and (x_tile_clicked<=9) and (y_tile_clicked>=0) and (y_tile_clicked<=9)) {
-
if (field[x_tile_clicked][y_tile_clicked] != 0) {
-
// set the starting color as the one of the tile I am clicking on
-
starting_color = _root.tiles["tile_"+field[x_tile_clicked][y_tile_clicked]]._currentframe;
-
}
-
}
-
// set the drawing style
-
drawing.lineStyle(5, 0x000000);
-
// moving the pen to my mouse position
-
drawing.moveTo(_root._xmouse, _root._ymouse);
-
// now I can drag
-
can_draw = true;
-
};
-
// when I release...
-
onMouseUp = function () {
-
// if the array of tiles to be removed has more than one element...
-
if (tiles_to_remove.length>1) {
-
// scanning all array
-
for (x=0; x<tiles_to_remove.length; x++) {
-
posx = tiles_to_remove[x]%10;
-
posy = Math.floor(tiles_to_remove[x]/10);
-
// removing the tile
-
remove_tiles(posx, posy);
-
}
-
update_field();
-
}
-
stop_drawing();
-
};
-
// removing tiles of the same color of the one clicked
-
function remove_tiles(tx, ty) {
-
_root.tiles["tile_"+field[tx][ty]].removeMovieClip();
-
field[tx][ty] = 0;
-
}
-
// updating the field, making tiles fall if bottom tiles disappear
-
function update_field() {
-
for (i=0; i<10; i++) {
-
for (j=1; j<10; j++) {
-
if ((field[i][j] != 0) and (field[i][j-1] == 0)) {
-
falling = j-1;
-
while ((field[i][falling] == 0) and (falling>=0)) {
-
field[i][falling] = field[i][falling+1];
-
_root.tiles["tile_"+field[i][falling+1]]._y += 32;
-
field[i][falling+1] = 0;
-
falling--;
-
}
-
}
-
}
-
}
-
}
-
// stop drawing
-
function stop_drawing() {
-
// now I can't draw
-
can_draw = false;
-
// clearing the drawing
-
drawing.clear();
-
// deleting and reinitializing the array. It's just a quick way to clear it
-
delete (tiles_to_remove);
-
tiles_to_remove = Array();
-
}
And play with it
Finally, download the source code.
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
11 Responses to “Adding a new twist to the prototype of a Flash game like Poux”
Leave a Reply
Trackbacks
-
Summer Couples: a game made in an airplane : Emanuele Feronato - italian geek and PROgrammer on
July 8th, 2008 12:55 pm
[...] It’s not my case, so I opened my laptop (always ask to the flight crew before doing it!) and started working on the code published on Adding a new twist to the prototype of a Flash game like Poux. [...]

Wow, nice job on that, a good little twist and a fun minigame!
I’ll be playing around with this one.
This already existed:
[Bomboozle]
http://www.minijuegos.com/juegos/jugar.php?id=6559
Anyway, great tutorial
Damn, they stole my concept BEFORE I published it…
it’s quite old bomboozle….
;)
Can you make an online game tutorial? a simple one, for like 2 players atleast?
just to show us how it works?
and also, how do you keep on duplicating the background and items when an object is moving fast through it (like canon games) :
http://www.addictinggames.com/kittencannon.html
Nice tutorial, but drawing isn’t a good control mechanic for this game, imo.
You lose the pen trail after each column shift, you can’t use big&fast mouse movements…It’s just not fun.
Much tweaking required.
(all of these doesn’t matter if you won’t use it in a full game, of course)
@Everybody
Check out 9 Ball Connect… The twist is that you’re forced to follow a numeric order.
Here’s the URL:
http://www.triqui.com/game/9-ball-connect/
@Thousand blades
Pausing the column shift interval when the mouse is down might fix it.
I’m sure emanuele would find another way to improve the gameplay! ;)
LOL… Wow… spent more time playing with it with my Wacom than actually reading the tutorial. I should really check out more games designed for use with a stylus and such. :)
Very good work.