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

December 13th update: 3rd part released
January 16th update: 4th part released

This is the second part of the tutorial about the creation of a Flash ball game with visual from above.

I suggest you to read part 1 before reading this post. Here I will start adding features to our game as said in the I Had to do it post.

Let’s start with the creation of the first, and most important, tile:

Exit tile

Well, an exit tile means there is an exit, and if there is an exit there is another level, and if there is another level I need a way to code levels.

I stored all levels in a function, in this way:

The function accepts one parameter, that will be the level number. In this two-level game, the switch at line 3 loads level 1 (lines 4-18) or level 2 (lines 19-33).

You will notice at lines 5, 6, 20 and 21 two new declarations. ball_start_x and ball_start_y store the starting vertical and horizontal position of the ball in the level.

Lines 36-37 Places the ground according to ball_start_y and ball_start_x position. Remember that in this game the ball remain fixed in the middle of the screen while the background is scrolling.

The rest of the code is explained at part 1, except for line 43 that I will explain later.

Now we have more than 1 level. As much as 2!!

Before we proceed, let me introduce another feature you will get a lot familiar with:

The dead

Since there are several ways to die, I need a function to call everytime the player dies.

It’s very easy… when the player dies, the ball position is restored to its initial position and both xspeed and yspeed are set to zero. Then redraws the level.

Very, very easy.

Now it’s time to explain how to design tiles.

Look at this picture:

Tileball

As you can see, the brick movieclip has several frames (10 in the example). Every frame contains a brick type, and has a stop(); in it to… ehm… stop it at the current frame. Now it should be clear why I wrote that line 43 in the level drawing function: I goto and stop the brick instance according to the value stored in the level array.

Now it’s time to design some nasty levels and explain how does it work.

In this tutorial, I’ll design levels 5 bricks tall and 5 bricks wide, but there is (almost) no limit to levels size you can create.

It’s interesting anyway the complexity of some levels, even if so small.

Here it is the actionscript, all in the 1st frame as real PROs…

I am going to explain in detail only new features. Read step 1 if you do not understand some code.

Lines 1-2: Attaching stars and ball movieclips

Line 3: Masking the ball as explained in Creation of realistic spheres in Flash with textures and masking.

Lines 4-5: Setting ball’s x and y speeds to zero

Line 6: Set starting level at 1. Don’t cheat!!!

Line 7: Calling function to draw levels

Line 8: Beginning of the code to be executed to the ball at every frame

Lines 9-10: Defining friction and power as explained in Flash game creation tutorial – part 1

Lines 11-12: Determining the position of the ball in the level array according to brick movieclip position.

Some explication of those strange numbers:

80: brick width or height
200: movieclip width (500) divided by 2 (500/2=250) minus the half of the brick width or height (250-80/2=210) minus the radius of the ball (10)
180: same thing with movieclip height

Actually, the radius of the ball should not affect ball position but the ball is not in the precise centre of the stage, just radius pixel moved up-left. Why did I do this? Dunno, will be fixed in next tut :)

Line 13: Obtaining the tile type where the ball is rolling on

Line 14: Beginning of code to be executed according to tile type.

Lines 15-17: Normal tile – do nothing

Lines 18-21: Down spinning tile: increase y speed.

Lines 22-33: Up, Left and Right spinning tiles: same thing for the Down spinning tile, adjusting x and y speeds according to the direction to be spinned

Lines 34-41: Glass tile, start breaking once the ball is on it. The actionscript simply decrease the _alpha value of the ball, and once it reaches zero set the array at this position to zero (hole)

Lines 42-46: Spin tile, multiplies actual x and y speeds

Lines 47-51: Slippery tile, setting the friction at 1 and the power at zero means that you can’t control the ball that will continue running at the same speed on the same direction!

Lines 52-58: Laser beam tile: this tile is a movieclip that changes its color once every 90 frames and lasts 30 frames. When the tile is highlighted, it’s deadly. To determine if the tile is in its deadly status, I simply check its current frame. If it’s bigger than 90… ZZZZAP!

Lines 59-64: Your friend, the exit! Increases lev (the actual level), removes the old level from stage and draws the new one

Lines 65-68: Default tile… I mean no tile… hole… death… avoid it!

Lines 70-108: “Engine” of the game, the same explained in part 1.

Lines 109-115: Death routine, explained above

Lines 116-195: Level creation routine, with 6 levels (and an “end” level).

Try to finish the game!

And this is where part 2 ends. There are tons of new features waiting to be explained during next tutorial… all your suggestion will be included… suggest and get credited in the final game!

Meanwhile, thanks to Fairlyn (slippery tile) and Frederik J (lava tile, in this game converted in beam tile).

Download the source code and give me feedback.

Read 3rd part.

  • thank you so much this has helped me so much

  • David

    Very nice tutorial as always :D I read all your tutorials so i can’t wait for the next :D

  • Mousey

    Awsome tutorail as always =] as above i read off of them. Carnt wait for linerider!

  • Diego

    Thank you for making this tutorials, all of them are pretty good! you helped me a lot with these
    thanks!

  • abhilash

    cool!!!

  • hansa

    what about a locked door tile, a key tile, a timed door tile and a moving tile

  • a reverse controls tile its simple to do you just get the slippery tile and put the power to 0.99

  • sorry i meant -o.99

  • Nice Mr. Feronato! Great job :)

  • eblup

    i beat all the levels

  • lewis

    Thank you sooooo much!

  • lewis

    if you want to make an invisible tile make a new tile (case 11) and change tha alpha to zero.

    Your tutorials have helped me so much as i am only 13 i coldn’t have advanced without you.

    THANKS!

  • Rory

    Totally awesome!

  • Dieter Galea

    hey guys i want to insert a new frame before the game itself. when i insert a frame before the game the game gets stuck. which numbers shall i changed to make it set to frame 2?

  • lewis

    Check out my version of the finished game.
    http://www.freewebs.com/gamesbybb/spaceball.htm
    Thanks for the tutorials Emanuele your great.

  • Dieter I have a preloader inside my first frame. Here is the Action Script. Also don’t forget to add stop(); to the bottom of your code in the second frame (ie. The code provided in this tutorial)
    The gotoAndStop(2) is the main thing you need.

    function refreshloader()
    {
    loadedbytes = getBytesLoaded();
    totalbytes = getBytesTotal();
    loadedkbytes = Math.ceil(loadedbytes / 1000 * 100 / totalkbytes);
    totalkbytes = Math.ceil(totalbytes / 1000);
    if (loadedbytes == totalbytes)
    {
    gotoAndStop(2);
    mcPreloader._visible = false;
    clearInterval(id);
    }
    else
    {
    frame = int(loadedbytes / (totalbytes / 100));
    mcPreloader.mcLoader._xscale = frame;
    mcPreloader.ctbPercent.text = frame + ” %”;
    updateAfterEvent();
    } // end else if
    } // End of the function
    id = setInterval(refreshloader, 200);

    Andrew

  • Emanuele is a King. Here is the game I came up with using this awesome man’s help.
    http://www.destructionpaintball.net/game.html

    Thanks Again,
    Andrew

  • miguel(lito)

    my lava tile dosent work
    how do i make the lava tile
    what will i do

  • miguel(lito)

    its ok i fixed it

  • Is this one of your games Emanuele ?
    http://www.kongregate.com/games/ciarenc/roll-roll-roll

  • Emanuele Feronato

    No Monkios, this game was built by a guy following my tutorials.

  • Hawdon

    Luve ur tutorials!
    But I just need some way to be able to make a tile in the last level that sends the player to the next frame where is says something like
    “Congrats! You win!!—— Replay?”
    I just don’t know how O.o
    Plz could some1 help me.

  • ida

    hey just to let you know. i’ve come out with a complete game of this for my school project.. so called complete… :)
    thanks to you.. feel free to view or play it here..

    http://e-duh.org/blog or
    http://e-duh.org/game.swf

  • Pingback: A Gem of Flash Game Tutorials | Newbie Game Programmers()

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

  • Søren

    hi every one, i am trying to make a simpel game. But i need to make an exit, is there any one ho can write a simple code for me, for getting this exit working. so when the ball roll over the exit you come to an new level.

    Søren

  • Héctor

    Please help! I don’t know why my own 3d ball does not work.

    I read the post how to make it and the mask made by action script didn’t work. I deleted ball_itself and make the mask manually. Worked perfectly.

    But when I want to do that oriented in that game, the texture does not move faster than the ball. The “sphere” is not “real”, it is a painted circle. Why? I’ve made all the things that are explained and does not work.
    I’ve tried it for 5 days, in different ways, and nothing.

    Please, does anyone know why is it happening that? It is driving me crazy.

    Thanks.

    • Ygr3ku

      if u use cs5 and ActionScript3 might not work because AS2 use command (something._thing) and AS3 instead uses (something.thing), the _ being eliminated and not recognized anymore. did this helped you? (try remove the _ )