Create a Flash game like Mass Attack

If you love to stay updated with latest addicting Flash games, probably you already know Mass Attack.

Mass Attack

Mass Attack is based on the very simple idea of balancing weights on a scale. Just press the mouse button to create a spheric weight. The longer you hold down the mouse the larger the weight that is created.

Maybe you are thinking about physics, but I am showing you how easy can be the engine behind this game.

Let’s start with the object we are using:

Mass Attack

ball: it’s… the ball

bar: represents the bar where balls will fall

The first step consists in creating some balls that will grow as long as I keep the mouse pressed

Line 1: A boolean representing the “placed” status of the game. Determines if the last ball is still being placed (so I can’t create a new one) or not

Line 2: A simple counter, to keep in mind how many balls I have placed

Line 3: Beginning of the function to be called every time I press the mouse key

Line 4: If placed is false… (if I can place a new ball….)

Line 5: A flag called will_grow is created and set to true. When this variable is true, all balls in the stage will grow (this may come handy in future, if I want to have more than one ball growing at the same time)

Line 6: Attaching the ball movie with its center at the current mouse position and its width and height to 1 (the smallest ball ever!), ad giving it the name bubu (I promised myself to change this name but…)

Line 7: Boolean saying bubu will grow

Line 8: Beginning of the actions to be executed for bubu at every frame

Line 9: If balls in the movie are growing and this ball can grow…

Lines 10-11: Increasing bubu‘s width and height

Lines 13-14: If not, bubu will cease growing forever

Line 17: Setting placed to true, so I cannot place another ball at the moment (that’s because the last one is still growing)

Line 20: Actions to be executed every time the player releases the mouse

Line 21: Balls stop growing

Line 22: Increment the number of balls placed

Line 23: Setting placed to false, so I can add another ball next time I’ll press the mouse

Now try to click in the movie and place some balls. As you can see, they are no balls… they are bubbles! They float! Let’s add a bit of gravity…

Line 8: Assigning another flag to bubu. This will determine if the ball will fall

Line 15: The ball stopped growing. Now, if it has to fall…

Line 16: The lamest gravity in the world! I simply move it down by 10 pixels

Lines 17-20: Checking if the ball hits the “ground” (the bottom of the movie), in this case make it stop falling

Well done! Now balls fall, but I have to make them fall on a balance

Lines 3-4: Two new varabiles to know the weight on the left side of the balance and the on the right side

Lines 5-6: Attaching balance sides

Lines 13-17: Determining if the ball will fall on the left side or on the right side according to its x position

Lines 22-27: Updating the weight on the left or on the right according to the side where the ball will fall and its size

Lines 32-43: Replacing the check if the ball hit the end of the movie with the check if the ball hit the left (or right) side of the balance

Line 55: Function to be executed at every frame

Line 56: Output a debug string displaying weights on each side of the balance

Now we have balls falling on the balance and we keep in mind how many weight we have on each side of the balance. It’s time to make them move!

Line 56: Determining the weight difference between the left and the right balances

Line 57: Updating left bar vertical position. That 190 is its default position, the one it will have if left and right bars have the same weight on them

Line 58: Same thing with the right one

And now the main engine is done. You can create balls of different sizes and make them fall on the balance. This will react according to the weight it has on each side.

There are some issues like the balance is updating its position before balls hit it, and you can’t throw balls under the balance, but at the moment the main work of this beautiful one day game is done.

If you want to convert it in a real game feel free to send me your examples and I’ll publish it on the site.

About this engine, I have one idea or two that I will discuss in a future tutorial.

Meanwhile, take the full sources and give me feedback.

  • chaew

    nice one! I think I could probably use this engine quite effectively to make a cool game. Also, I was wondering whether you could create any tutorials for a shooting game emanuele, thnx.

  • Ed

    These tutorials are great, but you should concentrate on a few! You’ve started loads and I’d really like to see some things like the tower defense tutorial finished.

    Keep up the tutorials though, they’re the best! =)

  • Pingback: GiuseppeSorce » Blog Archive » Create a Flash game like Mass Attack()

  • Wow, addicting game alright, lol. I got all completely perfect balances up until level 5, then got kinda stuck. Such a good feeling when you get perfect balance with one weight, lol.

    Great tutorial. Always interesting seeing how easy a complex concept can actually be when you just think and break it down.

  • Josh

    I agree with Ed i would like to see tower defense finished. I don’t think anyone has another tutorial for that.

  • how could you make it so they explode after 3 seconds???

  • You can modify this
    _root.onMouseUp = function() {
    _root.will_grow = false;
    balls_placed++;
    placed = false;
    idDestroy=setInterval(destroyBall,3000)
    }

    just added a timer that calls to the function destroyBall…
    then, just add the function
    function destroyBall(){
    bubu.removeMovieClip()
    }

  • Remember to do:
    clearInterval(idDestroy)
    before the removeMovieClip

    Sorry

  • Realy good tutorial, thanks for publishing.
    Actionscript is amazing.

  • Hi there!
    Can send another part of this tutorial. There is a certain glitch in this tute. When you put the weight on any side, the balances keep falling till the mouse is pressed. Can you teach us to make it fall/rise when the mouse button is released. Also can you teach us to make it bounce on falling.

  • Oskar Koli

    Nice tutorial,
    was just wondering that if I made the balls into squares how coud I do so that the player could jump on them with a movie clip using the arrow keys?

  • Oskar Koli

    nvm mind that I figured it out :D

  • Russell

    hi,

    I am a newbee in flash gaming and actionscripting…I would really wish to make a game like mass attack some day.

    Please could you guide me with matching the mass on right and left and also the levels.

    Please.
    Many thanks..

  • Russell

    Ok I figured it out…
    Anyone can help me with randomly placing balls of random masses in the left bar please..

    Regards,

  • Russell

    can anyone guide me with randon weight appearing in left side?

    anyone…help highly appreciated..

  • Russell

    Thanks guys… I found out myself…

  • Pingback: Create an HTML5 game like Mass Attack with Phaser just using tweens (well, almost) - Emanuele Feronato()