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.
They can be easily customized to meet the unique requirements of your project.
















(21 votes, average: 4.57 out of 5)










This post has 25 comments
Mavari
Excelent post!! I also made a game using box2d, about rescuing 33 Chilean miners … check it out on http://mavari.cl/MinersHero.html
Regards
Chris
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!
Kalle
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.
kek
Do you know QuickBox2D ?
I like using it, you have very few code lines compared to box2D.
http://actionsnippet.com/?page_id=1391
Sudeep Acharya
wow nice tutorial great job again !
Scott
Haha I suggested this :D:D:D:D:D:D:D:D:D:D:D:D:D
Alex
Now there will be new clones of Angry Birds))
XD
when i was aiming i hit the wall -_-’
Malken
By the Gods!
Gliiz
Nice !
Just curious how to setup the scrolling for following balls.
Kirtimaan
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?
Arindam
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.
Oleksandr
Fantastic, very nice
robertjoseph
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?
Ben Reynolds
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 :)
kumo
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.
MC
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…
Rocanten
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 :)
shyamal
ela emanuele,
i was looking for that code
LOL
OMG Adobe Flash shared this tutorial on the Facebook page!!!
ritterliu
Awesome job!!!
josh
hey emanuele, can you post a link to download the .fla and .swf thanks
Mike
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
Starling API – 20 – Lier Starling avec Box2D pour gérer les collisions « Adobe Flex Tutorial
[...] http://www.emanueleferonato.com/2011/10/10/develop-a-flash-game-like-angry-birds-using-box2d/ [...]
Ideas and solution for mini games 2 « Project Management
[...] 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 [...]