Create a Flash ball game with visual from above tutorial part 4

In the 4th part I am going to explain a feature that a race game must have: the laps

Read steps 1, 2 and 3 before continuing

Having laps in a track will result in a longer race.

The bad thing is that if you don’t design laps with some more actionscript than normally required, your players will cheat.

Look at the picture:

We have our track with 12 tiles with a clockwise direction. Assuming that you will start from 1, you should race through 1-2-3-4-5-6-7-8-9-A-B-C then 1 again in order to make a lap.

So when the ball rolls over C, it’s a new lap.

Wrong.

A cheater could move this way: 1-C-1-C-1 and he already completed 2 laps.

At this point, you would put an intermediate lap, let’s say at tile 7, and check that the intermediate lap has been passed before rolling over C. Now the player will have to drive through 1-2-3-4-5-6-7 but… let’s say the second part of the track is hardest than the first… he could make 1-2-3-4-5-6-7-6-5-4-3-2-1-C completing a lap without passing over 8-9-10-A-B, the hard part of the track.

To prevent this, you can put some “one way” tiles… if the tile 1 can be passed only from left to right, there is no way for the player to make 1-C because the game won’t allow it… but in some cases I would need to make 1-C, maybe he needs more speed to pass a certain tile (let’s say 3) and he founds himself at 2 without speed. He would need to make 2-1-C-B-C-1-2-3…

If you try to solve the lap problem this way, the player won’t be able to do 1-C to gain speed

Well, what about placing another intermediate lap, let’s say at A, so the player must pass over 7, then A and then C?

This may work, unless the path from A to C is harder than A to 1… so you may place an intermediate lap on B… and you end with an intermediate lap at every tile.

My solution allows to manage laps with only three tiles, one next to another. Let’s say C-1-2

The player will pass a lap when he rolls over C-1-2 but only if he never passed over tile 1 more than once. This means that a 1-C-1-2 does not count as a lap because he passed over the 1 twice.

Let’s see some actionscript: basically it’s the same as Create a Flash ball game with visual from above tutorial part 3 so I will comment only the new lines

Line 4: Attaching a movieclip to display laps

Line 11: Variable counting the number of laps

Line 23: Checking if the type of tile is greater than 10000. In my game, lap tiles are 10001, 10002 and 10003

Line 24: Obtaining the lap sequence: 1, 2 or 3, according on which tile the ball is rolling on

Line 25: If the lap sequence is equal to a variable called lap_to_go (it stores the next lap sequence to go in order to complete the lap as you will see later) and its value is 1 (the 10001 tile, the one that represents the start)

Line 26: Increase the number of laps

Line 27: Display the number of laps

Line 29: Calculating the value of the next tile in order to make a correct lap

Line 30: Defining the type of tile (a normal one)

Line 164: Level design of lap tiles

Lines 237-239: Showing the default tile if its number is greater than 10000

As you can see, now you have laps!

Now you have a lot more gamplay options with this engine… download the source code and create the ultimate game!

  • Pingback: Create a Flash ball game with visual from above tutorial part 3 : Emanuele Feronato - italian geek and PROgrammer()

  • very smart!
    can u make a car racing game based on the same anti-cheat lap setup?
    i wanted to make a space fighter racing game where u can shoot the cpu’s.

  • GReat EManuele!
    Well done!

  • xavi,

    if you want to make it, why do you ask him for it? try it yourself first:S. and if you succeed to make it without help you’ll be the only one with that code. which makes your game a lot more original (not like all the ball-game clones on the web)

  • very good!

  • robert

    hey emanuel is thier any way to make a tile that is smaller like in widtt so that peolpe could take short cuts?????????

  • gartman222

    first, i have tryed and tryed and tryed all freakin week to try to get a hit collison right. the thing wrong is that i cant get the too get my two marble rollers, when they run into each other, they they go back at first half the speed of the 1st guy, and some and half of the speed of the second guy,times -1.
    can u please help

  • Art

    Ok im trying to add more levels and more tiles so it will be 10 levels high and 10 tiles long. But when ever i do this i can’t see the tiles past the 5th one. Why is this?

  • Jorge Cifuentes

    Art:

    The last part of the code (lines 231 and 232), there are 2 cycles that “read” the arrays that determine the position of the tiles:

    for (y=0; y<=4; y++) {
    for (x=0; x0) {
    depth = y*12+x;

    if you want to have larger levels, you must modify the second part of both fors:

    y<= 4 and x<=4. If you want levels with 10 tiles width and 10 tiles long, you should change these values to y<= 9 and x<= 9. But remember to set ALL your arrays with 10 numbers, otherwise youll get errors. If you want a specific level to be shorter, just fill the “empty” spaces with zeros.

    So, your code should look like this:

    for (y=0; y<=9; y++) {
    for (x=0; x0) {
    depth = y*12+x;

    Wow this tutorial is great!!! Thanks a lot Emanuele for being so kind to share some of your knowledge with us!

  • I’m just itching to make a game for Bonus Level! I can’t wait for the level editor!

  • tom

    i played for hours and got 101 laps

  • Alcator

    This anti-cheat mechanism is flawed.

    You said that perhaps the player needs to return a bit to gain speed. But what if he needs to return for gaining speed to the tile that you only allow “once”?

    The correct anti-cheat procedure for any racing game is:
    1. Whenever player passes through Tile N, store the time of passing (Time[N])
    2. Whenever he passes some later tile O, again store the time (Time[O]).
    3. Lap is only counted as correct if Time[O]>Time[N].

    Even if player backtracks and returns to tile N, he then has to go through O again –> this check works.

  • vdervelden1

    i can’t put a gotoAndStop in this code help me pleas

  • how can i add those games to my homepage?

  • DutchBoy

    Very helpful, thanks!

    ~~~~~~~~ I HOPE YOU DON’T IGNORE ME ~~~~~~~~~

    Next time (please?) make:
    – 2 balls in 1 level
    – better effect for falling down
    – restart button
    – skip level button (with max. 2x)
    – previous level button
    – Passwords (for skip level)
    – keys to make doors open
    – a maximum of lifes (or you lose)
    – enemys
    – cheats

    !~~PLEASE MAKE MORE PARTS, BECAUSE THIS IS THE BEST YOU MAKED FOR ME~~!

    ~~~~~~~~ I HOPE YOU DON’T IGNORE ME ~~~~~~~~~

  • This is awesome! I will not be asking you to create this and that. I will use the code to learn from and create my own games. I thank you very much for providing this code, and free. This is just amazing, thank you.