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

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

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.

  • JDog

    Geez, this is looking great, real fast aswell !

  • 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

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

  • Emanuele Feronato

    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

  • Nate

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

  • Nate

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

  • Suely (Snakesue)

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

  • Xodus

    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!