Tuesday, September 28, 2010

Sharp Shooter Flash 10 CS4 Game Tutorials and Source

Hello friends and visitors, I am currently working on a game, I named it "Sharp Shooter" with no reason than it's a shooting gameJ . Well I am concerned here to make a game program. I will be not focusing on graphics designing. I will be using very simple graphics. I have completed some of its features currently working on enemy AI. I will try my best to complete and update the post because I am busy with my studies too. Your interest will give me pleasure and encourage making it fast so give your remarks in comments sectionsJ. I will be giving codes and snapshots at the end of the post because it would be easy for me to add/modify more codes and classes there. And one thing more you should know that I am not a professional and I am still learning. And I thought this kind of game to make because I don't know whyJ. I saw such kind of game over internet …. Okay let's start with no further talk.  :)



Game Name: Sharp Shooter
Version: 1.00 Testing  
Type: One Player Shooter
Controls: Mouse click (shoot)
Platform: Flash CS4
Language: Action Script 3.00 (AS3.0)
Description: Some tank or shooting machine shooting at some enemies trying to approach it. Top view game where player controls the Gun direction of tank with movements of mouse and click to shoot the approaching enemies. When an enemy hits our player Tank, it would destroy player Tank. Further details are to be added. Because it's not a final set of details :)
What You Should Know: You should know Flash CS4 and actionscript 3.0 syntax and OOP concept. How to make a class and create its objects, and how to make a document class of a flash file. I may be not explaining all the basic things. Or you can ask in comments only.    
Tutorials: 
The game uses following main classes:
  • Bullet.as [package xgame.props]
  • TankPlayer.as [same as Fla file]
  • TankEnemy.as [Same as Fla file]
  • SharpShooter.as [ Document Class ]
  1. First we will make a class Bullet in a package xgrame.props . Package is just a folder path of class file. You make folder in your project directory X:\...\SharpShooter\xgame\props . and make a new Bullet.as file from notepad or Flash CS4.The class coode is given at the end of this post .
  2. The Bullet.as class is a general class . It requires two parameters (to be Passed when creating class object). first the reference of a MovieClip class which contains graphics of bullet and second the DisplayObject where the bullet class will add graphics movie clip. In the class two private variables store these two as _mcClass and _mcContainer.
  3. Constructor of a class is a function which executes on the object creation. In Bullet class constructor  , an object of _mcClass is created and assigned to private var _mc.  and then it is added to DisplayList of _mcContainer class. x and y Position of _mc is set if constructor method is given two optional parameters two.
  4. Bullet class has following important properties and method which will be used in the game.
    speed, rotation, x,y and methods destroy(),animate(); The most import is animate function which animates _mc ( Bullet Visual) to an angle with specified speed. Its a set of trigonometry equations. You can the code with given simple forms as.
    xPos=cos(angle)*speed;yPos=sin(angle)*speed;
  5. Now download the template file given in downloads part below,and rename it to SharpShooter.fla in your project.
  6. Now before starting TankPlayer.as class and any other classes , you have to make movie clips that have some graphics of your choice. But assure following things._
    Make TankPlayer movie clip and in properties set Export for Actionscript and don't change class name.It should TankPlayer. It has some grafix for gun , the moviieclip instance of it should be "gun" with rotation angle=0 along positive x-axis (right side). You can get the idea from snap shots i uploaded.
  7. Now explaining TankPlayer class. Its very simple class. We need our tank gun to point to our mouse cursor . For this we will declare properties angle and we will get the angle from mouser cursor positions and trigonometric equations  .( Code Given at the end of the post )
  8. SharpShoot.as is our main document class.
    Snap Shots:        
Game First Look
TankPlayer MovieClip
Downloads
  Flash Template.Fla 

Action Script Code:  

Bullet.as

package xgame.props 
{ 
import flash.display.*;
public class Bullet  
{ 
 protected var _mcClass:Class;// refernece to bullet class of game bullet graphics 
 protected var _mc:MovieClip;//bullet  calss to movieclip 
 protected var _mcContainer:DisplayObjectContainer;//bullet container   
 protected var _speed:Number;// speed of bullet 
 protected var _angle:Number;// rotation angle of bullet 
 protected var _damage:Number;// strength of damage to enemy life (0-1 in percents); 
 protected var _gravity:Number;//gravity of a bullet; 
 protected var _nx:Number;//init xpos of mc 
 protected var _ny:Number;//init xpos of mc 
 protected var _dy:Number;//dy of yspeed  
 public var maxGravity:Number;  

 public function Bullet(c:Class,_con:DisplayObjectContainer,nx:Number=0,ny:Number=0) 
 { 
  _mcClass = c; 
  _mcContainer=_con; 
  _mc=new _mcClass(); 
  _mcContainer.addChild(_mc); 
  x=nx; 
  y=ny; 
  initValues(); 
 }  
 public function animate():void 
 { 
  var xsp:Number=speed*Math.cos(rotation*Math.PI/180);//calculating  
  var ysp:Number=speed*Math.sin(rotation*Math.PI/180); 
  x+=xsp;  
  _dy+=(_dy*gravity);
  if(_dy>=maxGravity) 
  { 
   _dy=maxGravity; 
  }
  y+=ysp+_dy;
 }       
 protected function initValues():void 
 { 
  speed=10; 
  rotation=0; 
  damage=0.5; 
  gravity=0;  
  maxGravity=20;
 }  
 public function destroy():void 
 { 
  _mc.parent.removeChild(_mc); _mc=null; 
  _mcClass=null; 
  _mcContainer=null; 
  delete this;
 }
  //**_______________geter/setters 
 public function get speed():Number 
 { 
  return _speed; 
 }
 public function set speed(val:Number):void 
 {
  _speed=val; 
 }   
 public function get rotation():Number 
 { 
  return _angle; 
 } 
 public function set rotation(val:Number):void 
 { 
  _angle=val; 
  _mc.rotation=_angle; 
 }   
 public function get damage():Number 
 { 
  return _damage; 
 } 
 public function set damage(val:Number):void 
 { 
  _damage=val; 
 }   
 public function get gravity():Number 
 { 
  return _gravity; 
 } 
 public function set gravity(val:Number):void 
 {  
  _gravity=val; 
  _dy=0; 
  if(_gravity!=0) 
  { 
   _dy=1; 
  }   
 }    
 public function get x():Number 
 { 
  return _nx; 
 } 
 public function set x(val:Number):void 
 { 
  _nx=val; 
  _mc.x=_nx; 
 }    
 public function get y():Number 
 { 
  return _ny; 
 } 
 public function set y(val:Number):void 
 { 
  _ny=val; 
  _mc.y=_ny; 
 }   
 public function get instance():MovieClip 
 { 
  return _mc; 
 }    
 //**===============end:Getter/Setter  
} 
}
TankPlayer.as


package  
{
  import flash.display.*;
  public class TankPlayer extends MovieClip 
 {
   var _angle:Number;
   var _gun:Sprite;
   public function TankPlayer() 
   {
    _gun=Sprite(this.gun);
    mouseEnabled=false;
   }
  public function set angle(_val:Number):void 
  {
    _angle=_val;
    _gun.rotation=_angle;
  }
  public function get angle():Number 
  {
   return _angle;
  }
 }
}

No comments:

Post a Comment