Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Worms game, Actionscript 3, Flash, Game development and Users contributions.

Do you remember Worms game and its destructible terrain?

Jordi Sanglas Molist explains us how to create a basic destructible terrain with AS3

« Today, browsing the “most popular” posts, I found “Create a flash artillery game – step 1“, where you talked about making a game similar to Worms.

Then, I thought: “How will I make a destructible terrain?”

After looking for some information, I realized I should use a Bitmap. So I started with a really basic prototype:

– The character falls if he doesn’t touch the ground
– You can press at any point of the terrain to make a hole (because I don’t still have weapons)

The code is fully commented (and there are only 59 miserable lines), so you won’t have any problem to understand it.

I’ll be three days on holiday, so on Sunday I’ll start writing the next part, which I think it may talk about moving the character.

I think I’ll do it in that way: if you press the right arrow and in the right the obstacle is less than X
pixels height, you can move. I’ll try if that works. »

I am looking forward for character movement, meanwhile this is the script:

package {
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.geom.Point;
	import flash.geom.Matrix;
	import flash.events.Event;
	import flash.events.MouseEvent;
	public class worms extends Sprite {
		public var terrain_bmpd=new BitmapData(550,200,true,0xFF00FF00);//This is the Bitmap of the terrain
		public var terrain_bmp=new Bitmap(terrain_bmpd);//and this the BitmapData
		public var character=new Sprite();//The character will work as a worm
		public var hole=new Sprite();//That's the hole we need
		public var hole_matrix:Matrix;//The hole_matrix is used to set the position of the hole
		public var left_foot:Point;
		public var right_foot:Point;//These are the feet of the character. We will use it to check collisions
		public function worms() {
			draw_objects();//This function draws the character, the terrain and the hole.
			stage.addEventListener(Event.ENTER_FRAME,fall);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
		}
		public function fall(e:Event) {
			/*This function will move down the character if there isn't a collision
			between the terrain and the "feet" of the character*/
			for (var i:int=0; i<10; i++) {//We want to check every pixel if there's acollision, so we won't move the character 10 pixels all at once
				left_foot=new Point(character.x-5,character.y+10);
				right_foot=new Point(character.x+5,character.y+10);
				if (!(terrain_bmpd.hitTest(new
				Point(terrain_bmp.x,terrain_bmp.y),0x01,left_foot))&&!(terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,right_foot))) {
					character.y++;//If there aren't any collisions, make the character fall one pixel
				}
			}
		}
		public function mouse_up(e:MouseEvent) {
			hole_matrix=new Matrix();//We need to reset the matrix each new hole
			hole_matrix.translate(e.stageX-terrain_bmp.x,e.stageY-terrain_bmp.y);//and setthe coordinates of the hole
			terrain_bmpd.draw(hole,hole_matrix,null,BlendMode.ERASE);//Then, we can draw the hole in the BitmapData
		}
		public function draw_objects() {
			terrain_bmp.y=200;//The terrain shouldn't be at the top of the stage!
			stage.addChild(terrain_bmp);//We can make the terrain visible
			character.graphics.beginFill(0x0000FF);//Let's draw the character. It will be a blue rectangle.
			character.graphics.drawRect(-5,-10,10,20);
			character.x=250;
			stage.addChild(character);
			hole.graphics.beginFill(0x000000);//Now we draw the hole. It doesn't matter the colour.
			hole.graphics.drawCircle(0,0,30);
		}
	}
}

And this is the result:

Click on the terrain to create holes and watch your "worm" falling down.

Download the source code.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.