Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

Develop a Flash game like Angry Birds using Box2D

Here we are with one of the most famous games running on your iPhone/iPad… Rovio‘s Angry Birds!

A lot of developers criticize this title, saying it’s just a clone of “shoot something to destroy something using physics”, but what can I say? It’s fun.

So don’t be envy and let’s start understanding how does the sling work.

I wrote a quick and quite messy code just as a pre-release, I will probably adjust something and explain the code tomorrow, but for all you “gimme the code now, I can read it”, here it is the sling:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),true);
		private var worldScale:int=30;
		private var bird:birdMc=new birdMc();
		private var birdSphere:b2Body;
		public function Main() {
			var bg:backgroundMc=new backgroundMc();
			addChild(bg);
			addChild(bird);
			bird.x=170;
			bird.y=270;
			bird.buttonMode=true;
			addWall(320,10,320,395);
			addWall(320,10,320,-5);
			addWall(10,320,-5,240);
			addWall(10,320,645,240);
			addEventListener(Event.ENTER_FRAME,updateWorld);
			bird.addEventListener(MouseEvent.MOUSE_DOWN,birdClicked);
		}
		private function addWall(w,h,px,py):void {
			var floorShape:b2PolygonShape = new b2PolygonShape();
			floorShape.SetAsBox(w/worldScale,h/worldScale);
			var floorFixture:b2FixtureDef = new b2FixtureDef();
			floorFixture.density=0;
			floorFixture.friction=10;
			floorFixture.restitution=0.5;
			floorFixture.shape=floorShape;
			var floorBodyDef:b2BodyDef = new b2BodyDef();
			floorBodyDef.position.Set(px/worldScale,py/worldScale);
			var floor:b2Body=world.CreateBody(floorBodyDef);
			floor.CreateFixture(floorFixture);
		}
		private function birdClicked(e:MouseEvent):void {
			addEventListener(MouseEvent.MOUSE_MOVE,birdMoved);
			addEventListener(MouseEvent.MOUSE_UP,birdReleased);
			bird.removeEventListener(MouseEvent.MOUSE_DOWN,birdClicked);
		}
		private function birdMoved(e:MouseEvent):void {
			bird.x=mouseX;
			bird.y=mouseY;
			var distanceX:Number=bird.x-170;
			var distanceY:Number=bird.y-270;
			if (distanceX*distanceX+distanceY*distanceY>10000) {
				var birdAngle:Number=Math.atan2(distanceY,distanceX);
				bird.x=170+100*Math.cos(birdAngle);
				bird.y=270+100*Math.sin(birdAngle);
			}
		}
		private function birdReleased(e:MouseEvent):void {
			bird.buttonMode=false;
			removeEventListener(MouseEvent.MOUSE_MOVE,birdMoved);
			removeEventListener(MouseEvent.MOUSE_UP,birdReleased);
			var sphereShape:b2CircleShape=new b2CircleShape(15/worldScale);
			var sphereFixture:b2FixtureDef = new b2FixtureDef();
			sphereFixture.density=1;
			sphereFixture.friction=3;
			sphereFixture.restitution=0.1;
			sphereFixture.shape=sphereShape;
			var sphereBodyDef:b2BodyDef = new b2BodyDef();
			sphereBodyDef.type=b2Body.b2_dynamicBody;
			sphereBodyDef.userData=bird;
			sphereBodyDef.position.Set(bird.x/worldScale,bird.y/worldScale);
			birdSphere=world.CreateBody(sphereBodyDef);
			birdSphere.CreateFixture(sphereFixture);
			var distanceX:Number=bird.x-170;
			var distanceY:Number=bird.y-270;
			var distance:Number=Math.sqrt(distanceX*distanceX+distanceY*distanceY);
			var birdAngle:Number=Math.atan2(distanceY,distanceX);
			birdSphere.SetLinearVelocity(new b2Vec2(-distance*Math.cos(birdAngle)/4,-distance*Math.sin(birdAngle)/4));
		}
		private function updateWorld(e:Event):void {
			world.Step(1/30,10,10);
			for (var currentBody:b2Body=world.GetBodyList(); currentBody; currentBody=currentBody.GetNext()) {
				if (currentBody.GetUserData()) {
					currentBody.GetUserData().x=currentBody.GetPosition().x*worldScale;
					currentBody.GetUserData().y=currentBody.GetPosition().y*worldScale;
					currentBody.GetUserData().rotation=currentBody.GetAngle()*(180/Math.PI);
				}
			}
			world.ClearForces();
			world.DrawDebugData();
		}
	}
}

And this is the result:

… you should know how to play… drag the bird to fire it with the sling.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (21 votes, average: 4.57 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 25 comments

  1. Mavari

    on October 10, 2011 at 7:27 pm

    Excelent post!! I also made a game using box2d, about rescuing 33 Chilean miners … check it out on http://mavari.cl/MinersHero.html

    Regards

  2. Chris

    on October 10, 2011 at 7:31 pm

    Very nice!
    Two “newer” games on the android market are “bunny shooter” and “early bird”, which use the same basic concepts of angry birds, but they are at the top downloaded!

    So learning how to make your own angry birds like game would be helpful for any developer.

    I love all the box2d tutorials!

  3. Kalle

    on October 10, 2011 at 7:45 pm

    This looks cool:) Can you also say how the objects breaking is done in this game(do they use animations for breaking?). It would be very useful. Thanks.

  4. kek

    on October 10, 2011 at 7:46 pm

    Do you know QuickBox2D ?
    I like using it, you have very few code lines compared to box2D.

    http://actionsnippet.com/?page_id=1391

  5. Sudeep Acharya

    on October 10, 2011 at 7:56 pm

    wow nice tutorial great job again !

  6. Scott

    on October 10, 2011 at 8:25 pm

    Haha I suggested this :D:D:D:D:D:D:D:D:D:D:D:D:D

  7. Alex

    on October 10, 2011 at 8:43 pm

    Now there will be new clones of Angry Birds))

  8. XD

    on October 10, 2011 at 9:17 pm

    when i was aiming i hit the wall -_-’

  9. Malken

    on October 10, 2011 at 9:54 pm

    By the Gods!

  10. Gliiz

    on October 11, 2011 at 12:28 am

    Nice !

    Just curious how to setup the scrolling for following balls.

  11. Kirtimaan

    on October 11, 2011 at 2:47 am

    Thanks for nice share as always. Do you have any plan to expand this tutorial a bit further, like dealing with camera motion and world panning?

  12. Arindam

    on October 11, 2011 at 8:41 am

    Very good..I love the tutorials…

    I want to develop a thing like in catch the candy (iphone). How they are making a stretchable Hand.. of a player..
    Can you please tell me.

  13. Oleksandr

    on October 11, 2011 at 7:04 pm

    Fantastic, very nice

  14. robertjoseph

    on October 12, 2011 at 12:23 pm

    Hi, I’m not a developer, but I am looking for someone to produce one game – and then probably a second. The first project would involve taking an elephant across various obstacles, and through a number of levels. The budget is not high (it’s intended to help promote a small brand) but we’re not looking for a highly sophisticated game. Ideally it would be developed for IOS and android. Can anyone help please?

  15. Ben Reynolds

    on October 12, 2011 at 7:15 pm

    Great choice for a game tutorial, I know a lot of people would love to learn the mechanics behind the beloved Angry Birds! Even though I am one of those developers you mentioned who “criticize this title, saying it’s just a clone of “shoot something to destroy something using physics”, I still admit it’s a fun game and a perfect application of Box2D at its best :)

  16. kumo

    on October 14, 2011 at 8:58 am

    This is how we learn and improve. Once we are able to remake the game, we should be able to improve the game and bring up the fun part to a higher level. I’m hoping to see a better game coming out.

  17. MC

    on October 14, 2011 at 9:44 am

    Emanuele, this is a feature requested by many
    and it should not be too difficult for you:
    You should use some script to hide/show the flash_preview
    (specially when you post more than one flash_movie in the same page)
    and you should be able to reload/refresh the flash without
    reloading the entire page…

  18. Rocanten

    on October 16, 2011 at 12:26 am

    to MC
    I think we cannot demand anything from Emanuele. Thanks for his work.

    Anyway we will see a lot of terrible clones of Angry Birds after this post i guess :)

  19. shyamal

    on November 7, 2011 at 10:55 am

    ela emanuele,
    i was looking for that code

  20. LOL

    on November 11, 2011 at 5:03 pm

    OMG Adobe Flash shared this tutorial on the Facebook page!!!

  21. ritterliu

    on November 18, 2011 at 2:07 pm

    Awesome job!!!

  22. josh

    on December 20, 2011 at 6:19 pm

    hey emanuele, can you post a link to download the .fla and .swf thanks

  23. Mike

    on January 4, 2012 at 5:22 pm

    What is the flash version you need for the .fla? because i tried opening it with macromedia flash 8 and adobe flash cs4..it says unexpected file format

  24. Starling API – 20 – Lier Starling avec Box2D pour gérer les collisions « Adobe Flex Tutorial

    on January 14, 2012 at 7:02 pm

    [...] http://www.emanueleferonato.com/2011/10/10/develop-a-flash-game-like-angry-birds-using-box2d/ [...]

  25. Ideas and solution for mini games 2 « Project Management

    on March 21, 2012 at 3:33 pm

    [...] wind blows game ( dandelion game), a tutorial: http://www.emanueleferonato.com/2011/10/10/develop-a-flash-game-like-angry-birds-using-box2d/ Develop a Flash game like Angry Birds using Box2D – Emanuele [...]