Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Mp3 Player with XML Playlist
Image Slider
Flash Media Player
3d Rotating Image Cube
Catapult Game


Random Articles

Album Slide
Growing Tree using Recursion
MegaCombs
Base Defender
Music Player
Moving Clouds and Waving Grass


Links

Foundation-Flash
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

Base Defender

Base Defender
AddThis Social Bookmark Button
Description: Create this game. Defend the TNT beneath the grass by blowing up any incoming airplanes and the bombs they drop.
Author: John Bezanis
Added: October 25th 2008
Version: Flash 8


The objective of this game is to defend the tnt beneath the grass by blowing up any incoming airplanes and the bombs they drop. If a bomb hits the grass, it will leave an impact crater. The game is lost when a bomb travels all of the way to the tnt. The airplanes appear progressively faster, as do the bombs they drop. A point is scored each time an airplane or a bomb is shot.

There are a few interesting techniques used in this game, and I'll try to highlight them. The full source file is available at the end of this tutorial if you'd rather just digest the code in its entirety.

For the grass, we use a bitmap that is generated by importing a PNG graphic. The png format is useful because it uses transparency.
  1. //Create a bitmap that holds the data of the grass
  2. var grasspng:BitmapData = BitmapData.loadBitmap('grass');
  3. //Create another bitmap the size of the grass, and make all of the pixels transparent
  4. var grass:BitmapData = new BitmapData(grasspng.width, grasspng.height, true, 0x00000000);
  5. //Create a movieclip that will hold the grass bitmap
  6. this.createEmptyMovieClip('grasscontainer', this.getNextHighestDepth());
  7. //This function refreshes the grass bitmap filling in spaces that were blown up by bombs
  8. function resetGrass() {
  9.   //copy the grasspng bitmap over to the grass bitmap
  10.   grass.copyPixels(grasspng, new Rectangle(0, 0, grasspng.width, grasspng.height), new Point(0, 0), grasspng, new Point(0, 0), true);
  11.   //attach the grass bitmap to the grasscontainer
  12.   grasscontainer.attachBitmap(grass, grasscontainer.getNextHighestDepth());
  13.   //move the y position
  14.   grasscontainer._y = 275;
  15. }
  16. //apply the grass
  17. resetGrass();
Another interesting function is the circle erase function. A circle is made by erasing a series of one pixel vertical lines. The lines get longer as they get closer to the center horizontally.
  1. //This function erases a circle of the specified size from the specified bitmap
  2. function circleErase(bitmap, xpos, ypos, circlesize) {
  3.   //r2 is the radius of the circle squared. Keeps the math out of the for loop
  4.   r2 = circlesize*circlesize;
  5.   //The circle is drawn using a series of vertical lines.
  6.   for (circx=-circlesize; circx<=circlesize; circx++) {
  7.     //Determine the y distance at the current x position.
  8.     //The y is going to be larger as it gets closer to the center of the circle
  9.     circy = Math.round(Math.sqrt(r2-circx*circx)+0.5);
  10.     //Draw a transparent vertical line using the start x and y positions,
  11.     //with height according to its distance from the center
  12.     bitmap.fillRect(new Rectangle(xpos+circx, ypos-circy, 1, circy*2), 0x0);
  13.   }
  14. }
The third function of interest is one that runs whenever the mouse is clicked. Each time the mouse is clicked, a rock is hurled towards the mouse. The rock is given an x and y velocity and moved each frame. When the rock hits an enemy or leaves the stage area, it is removed.
  1. //Each time the player clicks the mouse, a rock is thrown towards the mouse
  2. onMouseDown = function () {
  3.   //attach the rock
  4.   this.attachMovie('interceptor', 'interceptor'+totalobjects, this.getNextHighestDepth());
  5.   //set the x and y positions at the position of the soldier
  6.   eval('interceptor'+totalobjects)._x = soldier._x;
  7.   eval('interceptor'+totalobjects)._y = soldier._y-20;
  8.   //The velocities are set according to the angle of the mouse relative to the soldier
  9.   //X velocity of the rock. More x velocity means less y velocity
  10.   eval('interceptor'+totalobjects).xvel = 13*(_xmouse-soldier._x)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
  11.   //Y velocity of the rock. More y velocity means less x velocity
  12.   eval('interceptor'+totalobjects).yvel = 13*(_ymouse-soldier._y)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
  13.   //At each frame, move the rock according to its velocity and check for enemy collisions
  14.   eval('interceptor'+totalobjects).onEnterFrame = function() {
  15.     //Move the x and y positions according to the velocity
  16.     this._x += this.xvel;
  17.     this._y += this.yvel;
  18.     //check if this rock collides with any enemies
  19.     enemyCollision(this._name);
  20.     //if the rock has left the boundaries of the Stage, remove it
  21.     if (this._x<0 || this._x>Stage.width || this._y<0 || this._y>Stage.height) {
  22.       removeMovieClip(this);
  23.     }
  24.   };
  25.   //Increment the total number of objects created
  26.   totalobjects++;
  27. };
The game uses an array to keep track of all of the enemies on the stage. Whenever an enemy is created or removed, it is also added or removed from the array. There is also an object counter that increments each time a new enemy or rock is created. This is useful for giving each enemy a unique name. The count is added to the end of the enemy type to create a unique instance name.
The source file is available below for download:

Download Source File
Download Demo SWF
Comments
Hi great tutorial
December 6th 2008 12:12AM   -   Unknown7
these a bug in the source file i doanloaded when i play it the air planes apeare of map ad drop bombs that just go straigh down and dnt hit the grass
December 19th 2008 08:12AM   -   kerne
Hi, nice tutorial :) One of the best made here, works fine.
January 25th 2009 06:01AM   -   Sage
wonderful game
February 5th 2009 04:02AM   -   venkatesh
cool played it alot
February 26th 2009 01:02PM   -   foo
Cool game
March 30th 2009 01:03AM   -   Gopi.s
dude i saw your tutorial and the only thing i want to know is how you made the floor get chunks out of it. if you could tell me how it works that would be nice.
April 15th 2009 05:04PM   -   icefrog50
bc i am an amaeuture at understanding actionscript,
where is the code where you state not to have the enemy duplicate once it is hit?
June 9th 2009 02:06PM   -   nat
Lol love this game
June 10th 2009 02:06AM   -   lol
I have been working around with the code here, and trying to develop a game with this. However, in order for it to have a title screen and such, I need to know how to remove the grass image after the player presses play again. Is there a way I can do this?
July 16th 2009 11:07PM   -   Tom
I'm liking the explosion animation! Good fun :D
August 20th 2009 11:08AM   -   Alex email marketing dude
loving it
November 6th 2009 10:11AM   -   vigrx
Really nice game I like it very much I will add it at my site
November 10th 2009 09:11AM   -   free online games
Really nice game I like it very much I will add it at my site
November 14th 2009 03:11AM   -   Hermes Handbags
Love the pixel graphics! Explosions IMHO should be pixelated as well though to keep the graphic style consistent.
November 22nd 2009 04:11PM   -   Sara
I've been really enjoying flash games for some time but I think it would be cool to be able to design my own. Thanks.
November 30th 2009 02:11PM   -   Phil
Flash developers will love it.
December 6th 2009 01:12AM   -   seo
Nice share, it's addictive.
December 8th 2009 01:12PM   -   2010 fit
I have bookmarked your site. Thanks for sharing
December 13th 2009 08:12AM   -   dental
this is so cool how you create this. You do amazing work on these games.
December 17th 2009 12:12PM   -   orlando family dentist
Great game. I played it alot and liked it alot as well. Pretty easy to play.
December 19th 2009 10:12AM   -   news script
I like this Flash Game. Thanks for sharing.
December 22nd 2009 06:12AM   -   Flex Development India
Nice game, thanks for sharing, i will add this to my blog too.
December 28th 2009 03:12PM   -   SEO Services
Nice game. Reminds me of the fort defense games. Cheers
December 29th 2009 11:12PM   -   free sonic games
Great job man, loving it.
December 30th 2009 07:12AM   -   encuestas pagadas
[b][url=http://www.mbtmall.com/Sports.html]mbt sneakers m walk[/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mbt sneakers chapa [/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mbt sneakers lami[/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mbt masai shoes[/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mbt barabara mens shoes [/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mbt chapa sneakers[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-M.-Walk-Sky-36.html]mens mbt m walk sky[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-M.-Walk-Silver-37.html]mens mbt m walk silver[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-M.-Walk-Pink-35.html]mens mbt m walk pink[/url][/b]
[b][url=http://www.mbtmall.com/Sports.html]mens mbt m walk black[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-M.-Walk-White-34.html]mens mbt m walk white[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-Water-49.html]mens mbt chapa water[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-Ebony-48.html]mens mbt chapa ebony[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-Black-18.html]mens mbt chapa black[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-GTX-Stone-47.html]mens mbt chapa gtx stone[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-GTX-Black-46.html]mens mbt chapa gtx black[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Chapa-Caviar-Black-19.html]mens mbt chapa caviar black[/url][/b]
[b][url=http://www.mbtmall.com/Sports/Men-s-MBT-Sport-Black-17.html]mens mbt sport black[/url][/b]
[b][url=http://www.mbtmall.com/Casual/Men-s-MBT-Safiri-Chill-20.html]mens mbt safiri chill[/url][/b]
[b][url=http://www.mbtmall.com/Casual/Men-s-MBT-Safiri-Navy-21.html]mens mbt safiri navy[/url][/b]


December 31st 2009 01:12AM   -   mbt shoes
i saw your tutorial and the only thing i want to know is how you made the floor get chunks out of it. if you could tell me how it works that would be nice.
January 1st 2010 09:01PM   -   Cialis
Great game... I am very impressed <a href="http://www.superslim.co.uk">Slimming</a> and <a href="http://www.superslim.co.uk/online-slimming.html">Online Slimming</a>
January 3rd 2010 02:01PM   -   Online Slimming
i like
January 4th 2010 02:01AM   -   Bridesmaid Dresses
I like this Flash Game. Thanks for sharing.
January 5th 2010 03:01PM   -   watch avatar online
i saw your tutorial and the only thing i want to know is how you made the floor get chunks out of it. if you could tell me how it works that would be nice.
January 5th 2010 03:01PM   -   watch did you hear about
Great game... I am very impressed
January 5th 2010 03:01PM   -   watch nine online
Just one question: how to add your blog into my rrs reader, thanks so much.
January 7th 2010 09:01PM   -   christian louboutin
i saw your tutorial and the only thing i want to know is how you made the floor get chunks out of it. if you could tell me how it works that would be nice.
January 11th 2010 06:01PM   -   Home Business
Interesting tutorial. Thanks for taking the time to type it up, I've always loved flash games.
January 12th 2010 02:01PM   -   Julia
Buy wellbutrin online now! No prescription needed!
January 14th 2010 05:01PM   -   Buy Wellbutrin Online
Wow, I sure would like to know the meaning behind the symbols. I suspect they carry a message. How creative.
January 17th 2010 05:01AM   -   Download HD wallpapers
Good info to be shared :)
January 18th 2010 09:01AM   -   Acai Berry
Base Defender is the great gream, I love it.
January 18th 2010 09:01PM   -   book
All source codes above are easy to understand.
January 18th 2010 09:01PM   -   film
Good war game for download =)
January 21st 2010 01:01PM   -   War Airplanes base
Good post.
January 22nd 2010 04:01PM   -   drug store online use
thanx for this post i really loved it
January 23rd 2010 03:01PM   -   order augmentin
good post.
January 23rd 2010 04:01PM   -   levitra vardenafil
great infos here i will come back more often
January 23rd 2010 09:01PM   -   augmentin pharmacy
great post i will read this more often
January 23rd 2010 10:01PM   -   get augmentin
Thanks for sharing,it's really good and need some update.
January 24th 2010 02:01AM   -   livescore
Good info to be shared :)
January 24th 2010 02:01AM   -   7m
Really nice post,thanks for sharing.
January 24th 2010 02:01AM   -   polball
Base Defender is the great gream, I love it
January 24th 2010 02:01AM   -   big4club
Base Defender is the great gream, I love
January 24th 2010 06:01AM   -   evening dresses
Special thank for good post.
February 5th 2010 08:02AM   -   à¹‚หลดเพลงà
Good info
February 5th 2010 07:02PM   -   bodyg
http://www.xn--72c9beqh3h3c.com/
February 5th 2010 07:02PM   -   mixy
Thank
February 5th 2010 10:02PM   -   à¹‚หลดเพลงm
nice <a href="http://tramadol.viviti.com/">Tramadol</a> nice
February 6th 2010 07:02AM   -   tramadol
<a href="http://www.baidu.com">°Ù¶È</a>
[url=http://www.sina.com]sina[/url]
[url="http://www.baidu.com"]baidu[/url]
[url=www.google.com]google[/url]
[link=http://www.yahoo.com]yahoo[/link]
[a/]http://www.yahoo.com[a]yahoo[/a]
February 7th 2010 06:02PM   -   chi flat iron
Great game.. thanks
February 9th 2010 04:02AM   -   Acai Berry 14
Special thank for good post.
February 10th 2010 03:02AM   -   à¹‚หลดเพลงà
Special thank for good post.
February 10th 2010 08:02AM   -   à¹‚หลดเพลงà
Add a Comment
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):