Worms-like destructible terrain in Flash – Part 3

After another truck of Aspirin, Jordi Sanglas Molist is back with the 3rd step of his Worms tutorial.

I’ve finished part 3. I almost got a headache. Forgive me if there are some mistakes, I wrote this post and the comments quickly.

Now the character is affected by the explosions. To do that, I added an horizontal speed and a friction. However, I don’t want the character to slide when I’m using the arrow keys, so:

* The arrow keys won’t use horizontal speed
* If the horizontal speed is different than 0 (an explosion is moving the character), I can’t use the arrow keys

To calculate the impulse, we will need to use trigonometry:

I’ve also solved another bug: if the character was falling (without using the space bar), it could jump in the air. Here I removed the jumping variable and I check if the character is on the ground.

Pay attention!!! Sometimes I check if the character is ON the ground (character.y+10) and sometimes I check if the character is TOUCHING the ground (character.y+9).


* The horizontal speed won’t decrease faster or more slowly if the character is on a slope.
* The for loop isn’t exact when the horizontal speed is a decimal number (I think).

What should I do in the next part? I must do it before I start school, or I won’t have time.

While you think about the next part to request, here it is the script:

And this is the result:

As usual, left and right to move the character, spacebar to make it jump and mouse click to create explosions.

Download the source code and don’t forget to request the next part.

  • Colin Douch

    Looks Really Nice. Good Job!

  • Fighterlegend

    I like it!

  • MC

    cool, thanks

  • Jason

    A nice thing to see next is a background picture like in the real worms game.

  • Chesteer

    Nice part, but explosion impulse could be done little easier :).

    Graphics? Let’s see AS, sth like life bar( ~5min) and shooting :)

  • Arkshija

    ufff i cant understand as3, i worked only with as2 and i dont know how to start to practice with as3, as3 is much better than as2???

  • Jordi Sanglas Molist

    Well, I’ll be on holiday again. On tuesday 31st I’ll be back, so I won’t be able to code anything until then.

    However, I can’t write a new tutorial about a background picture.

    • Jason

      You know I meant that instead of a green colored terrain I meant a terrain background seen in the worms games?

      • Jordi Sanglas Molist

        Sorry, I thought you were talking about the “sky”. I’ll try it, if you want. Thanks, Jason.

      • Just make it a mask for some random picture

  • Arkshija

    Eh jordi no seras catala??

    • Jordi Sanglas Molist

      Efectivament. De moment estic fora, de vacances. Ja parlarem la propera setmana.

  • Jared

    I perfer to use as2, I want to make pixel perfect collision detection like in this game. How would I do this? Can you post a subject with that? Thanks.

    • Thanks! I have an excellent idea for the following game where I can use the given approach.

  • Jeric

    Very nice! Can you post as2 version for this one? that would be really great! Cheers :D

  • kareem

    very nice , waiting for the next part..

    but i have a question ,, how to put an image instead of these green terrain ??

  • saber

    but how can do this in actionscript 2?

  • Marz

    For those of you asking for an image it’s really simple.

    Put a bitmap type source in your library and export it to actionscript and give it a class name: mine was “tan_brown”.

    Then in the variable definitions I did this:

    public var fill_bmpd:BitmapData = new tan_brown();
    public var terrain_spr:Sprite = new Sprite();

    After you place this with the remainder of the variables, go into your constructor and do the next couple of lines:

    terrain_spr.graphics.beginBitmapFill(fill_bmpd, null, true, false);
    terrain_spr.graphics.drawRect(0, 0, 550, 200);

    This will put the background in as a texture of sorts and then swap it over to a bitmap so that it will allow the blendmodes. The nice thing about using this is that you can use the vector drawing methods described under the ‘mysprite’.graphics area and use those to create a random terrain to begin with.

    Let me know if this helped any of you. If you are still using actionscript 2.0, I’d recommend doing the switch and learning as3.0, it’s got a lot of improvements that are worth the jump and their are loads of tutorials out there on how to make the switchover.

    Cheers. godofwarmarz at gmail dot com
    Email me if you have some more questions. :)

  • EirĂșn

    This is awesome!

    But I’d love to see some weapons at work – Especially grenade types that bounce off by the correct angle. Is that even possible? Maybe leaving invisible vector “craters” that decide the mirror axis for the impact? Or can a reflection axis be calculated by a few surrounding pixels, i.e. 9×9 grid? Or is it enough to mirror the angle by X and Y axis upon top/bottom and left/right collision?

    I look forward to the continuation of this tutorial!

  • rex

    I’d see the gravity here so there are no gaps in the terrain, everything taht has gaps falls down

  • Raphael Santos

    now… how do you align the object to the terrain?

  • dennis

    Great tutorial!

    I’d like to know how to change the character graphics though.
    (Maybe Marz also knows how to do this?:D)

  • Don

    @dennis – I’d suggest making the current character .visible = false, then adding your own sprite with the same size and alignment point, and moving it over the top of your invisible programmer art – this way you can always switch programmer art back on to see your boundaries.

  • Don

    so I started playing with this as a base for a casual game, and I have a quick tip for you – instead of using “new Point(x,y)” and similarly for Rectangles, use a global temp variable for each (one set per function is how I’m using it) and set the temp value Point to new values rather than spawning new Points all the time – I believe that will increase your speed alot, especially where you use it in a for / while loop.

    (you’ll notice the speed difference if you add a few more characters :P)

    have a great day

  • Jorge

    What about a colapsing terrain like in the game Scorched Earth? How would it be? I’m trying hard with that.

  • gaffkins

    Hey. you could write a tutorial descriptive on the change of velocity on slope. Please :)

  • Artemman

    Would there be the next tutorial?

  • Erik

    Great tutorials!

    If you want a bounce effect:

    Add this variable:
    public var bounce:Number = 0.5;

    Replace the x_speed=0 with:
    x_speed = -x_speed*bounce;

    Replace the y_speed=0 with:
    y_speed = -y_speed*bounce;

    EXECPT THE x_speed=0 WITHIN if (Math.abs(x_speed)<0.1)

  • Erik

    Someone should help us with realistic bounces and rotation of the character!