Hellow again guys, continuing our last tutorial HERE, where we worked on

our ship movements, now this ship will gonna to make PEW PEW!. If you want to read the previous tutorial CLICK HERE!

If you don’t understand  something, just ask me, also feel free to correct me.

I – The Laser Shoot

Well first, let’s add to our Scene the “LASER SHOOT” image., you can “Save As” the image bellow just like the spaceship image from the tutorial actionscript 3 part 1:

lasershoot1

yes, this thing red-yellow, drag it to your scene “Spaceship.fla”.

Now you have a Bitmap Laser Shoot in your scene, let’s transform it in a symbol:

-Select it

-Press F8 or go to Modify–>Convert to Symbol.

-Name: Lasershoot

-Mark the box: “Export for Actionscript”

-Class: Lasershoot

-Base Class: flash.display.MovieClip

-Click in “Ok”

Screen Shot 2014-08-06 at 5.20.03 PM

-If a message telling about “Definition for this class could not be found in the classpath, so one will be automatically generated in the SWF file upon export.” appears, just click “Ok”.

Now if you have a laser in your scene, just delete it, we won’t use, what we are gonna use is just the Lasershoot in your Library.

Now let’s head to “Spaceship.as

Before I continue I must say it again:

The next step definitely IS NOT the most right way to code this game, but in my opinion it is a good and very simple way to understand and apply.

So let’s add some private var for our public class Spaceship:

private var _pushshoot:Boolean=false; 
//MovieClips
private var _mcHolder:MovieClip = new MovieClip(); //a Holder of movieclips for our scene
//Array Lists
private var laserList:Array=new Array();
//Others
private var minimumi:int=0;
private var _weaponcooldown:int=0;
private var _weaponcooldownmax:int=10;
private var _shootSpeed:Number=20;

Well explaining a little what each of this var will do:

_pushshoot: It will tell if we are pressing our shoot button, in the case will be SPACE bar.

_McHolder: will hold our movieclip that will be called in actionscript. That will make easier to clean or make some adjustment in the future.

laserList: It is an array that will keep all laser that will be shooted.

minimumi: This is a simple var that will keep our minimum “i” from our “for”, I think you will understand better what I’m trying to do here later.

_weaponcooldown: is the actual cooldown value.

_weaponcooldownmax: is our time of cooldown, if you plan to make this game bigger and complex you should start use the Ship class to keep some values.

_shootSpeed:  This one is the speed of our bullet (or lasershoot).

Now, head to init(), inside of it, let’s et some values:

minimumi=0;
_weaponcooldown=0; //Everytime the game starts, the weapon cooldown will be set to 0
addChild(_mcHolder);//Adding a holder for the scene

 

Now head to inside of our “KeyDown” and add:

 

if(event.keyCode == Keyboard.SPACE) { //new
	_pushshoot = true;
}

 

And  go inside of our “KeyUp”:

 

if(event.keyCode == Keyboard.SPACE) { //new
	_pushshoot = false;
}

Now, if you press SPACE bar, our var _pushshoot will be true, so let’s work for this shoot happens.

Inside of our function “Draw” let’s add:

 

//Uncooldown
			if(_weaponcooldown>0){
				_weaponcooldown--;
			}
			//Movements
			if(laserList.length>0){
				for(var i:int=minimumi;iweaponcooldownmax;
			}

 

So what happens here:

Where is commented //KEYSHOOT:

will check if we press the SPACE bar, check the boolean _pushshoot, and it check if our weapon is out of cooldown, after that the laser is created in the screen on the mid position of the ship and rescalled to looks better, so this laser is added inside of _mcHolder and the addChild put it in our scene, the setChildIndex just tell to the laser goes to the front of the scene and for last the actual weaponcooldown is set with the value of the max weapon cooldown.

In movements, it is moving everything inside of the laserList array and if it goes out of our resolution it will be removed.

Now, if you goes to your Spaceship.fla and test it, your ship will be shooting every time that you hit SPACE bar. 😉

 

II – Playing Pew-Pew Sound

 

For play pew-pew sound, first you’re gonna need a lasershoot sound, and you can download it HERE.

It’s the same that i used in my Cocos2d tuturoail/

After the download of the lasershot.mp3. Open your flash document Spaceship.fla, and drag the laserhot.mp3 to your library.

You should see the lasershot.mp3 on this:

Screen Shot 2014-08-06 at 10.21.57 PM

 

 

Now, head back to our Spaceship.as:

First of all, let’s make some imports:

 

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.media.SoundChannel;
import flash.media.SoundTransform;

 

Inside our public class Spaceship, let’s add 2 more private vars:

 

private var Pewpew:URLRequest = new URLRequest("lasershoot.mp3"); //the URLRequest must match the exact name of the sound file in your library.
private var pewpewSound:Sound = new Sound(Pewpew);

 

Go to where our lasershoot is created, in function Draw, inside of the if “if(_pushshoot && _weaponcooldown==0){

and add:

 

pewpewSound.play();

 

Now, you can test and see your ship making some odly noise when you shoot that I’m calling pew pew. :3

Well, thx for read guys.

You can see the entire code of our Spaceship.as bellow! Cya

 

package  {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.ui.Keyboard;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLVariables;
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform; 

	public class Spaceship extends MovieClip {
		//horizontal values
		private var _horispeedmax:Number=15;
		private var _horispeedaccel:Number=2;
		private var _horispeeddesaccel:Number=0.5;
		private var _horispeednow:Number=0;
		//vertical values
		private var _vertspeedmax:Number=15;
		private var _vertspeedaccel:Number=2;
		private var _vertspeeddesaccel:Number=0.5;
		private var _vertspeednow:Number=0;
		//keys
		private var _pushshoot:Boolean=false;
		private var _pushleft:Boolean=false;
		private var _pushright:Boolean=false;
		private var _pushtop:Boolean=false;
		private var _pushbottom:Boolean=false;
		//Mouse Click
		private var _mouseClicked:Boolean = false;
		//MovieClips
		private var _mcHolder:MovieClip = new MovieClip(); //a Holder of movieclips for our scene
		//Array Lists
		private var laserList:Array=new Array();
		//Sound Effects:
		private var Pewpew:URLRequest = new URLRequest("lasershot.mp3");
		private var pewpewSound:Sound = new Sound(Pewpew);
		//Others
		private var minimumi:int=0;
		private var _weaponcooldown:int=0;
		private var _weaponcooldownmax:int=10;
		private var _shootSpeed:Number=20;
                private var lastyclicked:Number=0;
		
		public function Spaceship() {
			trace("inited");
			init();
		}
		private function init():void{
			minimumi=0;
			_weaponcooldown=0; //Everytime the game starts, the weapon cooldown will be set to 0
			addChild(_mcHolder);//Adding a holder for the scene
			//myShip posicioning
			myShip.x=140;
			myShip.y=120;
			myShip.rotation=90;
			myShip.scaleY=0.6;
			myShip.scaleX=0.4;
			
			//Keyboard events:
			stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
			stage.addEventListener(Event.ENTER_FRAME, Draw);
			//Mouse events:
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
		}
		
		function KeyDown(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.SPACE) { //new
				_pushshoot = true;
			}
			if(event.keyCode == Keyboard.RIGHT) {
				_pushright = true;
				_pushleft =false;
			}
			if(event.keyCode == Keyboard.LEFT) {
				_pushleft = true;
				_pushright = false;
			}
			if(event.keyCode == Keyboard.UP) {
				
				_pushtop = true;
				_pushbottom = false;
			}
			if(event.keyCode == Keyboard.DOWN) {
				_pushbottom = true;
				_pushtop = false;
			}
		}
		
		function KeyUp(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.SPACE) { 
				_pushshoot = false;
			}
			if(event.keyCode == Keyboard.RIGHT) {
				_pushright = false;
			}
			if(event.keyCode == Keyboard.LEFT) {
				_pushleft = false;
			}
			if(event.keyCode == Keyboard.UP) {
				_pushtop = false;
			}
			if(event.keyCode == Keyboard.DOWN) {
				_pushbottom = false;
			}
		}
		
		private function onMouseDownHandler(e:MouseEvent):void {
			 _mouseClicked = true;
		}
		private function onMouseUpHandler(e:MouseEvent):void {
			 _mouseClicked = false;
		}
		
		function Draw(event:Event):void
		{
			//Uncooldown
			if(_weaponcooldown>0){
				_weaponcooldown--;
			}
			//Movements
			if(laserList.length>0){
				for(var i:int=minimumi;i<laserlist.length;i++){ if(laserlist[i].x<1000){="" laserlist[i].x+="_shootSpeed;" }="" else{="" minimumi="i+1;" _mcholder.removechild(laserlist[i]);="" keys="" if(_pushshoot="" &&="" _weaponcooldown="=0){" var="" laser:lasershoot="new" lasershoot();="" laser.x="myShip.x+(laser.height/2);" laser.y="myShip.y+(myShip.width/1.1);" laser.rotation="90;" laser.scaley="0.4;" laserlist.push(laser);="" _mcholder.addchild(laser);="" _mcholder.setchildindex(laser,0);="" pewpewsound.play();="" if(_mouseclicked){="" if(stage.mousey="">=(myShip.y+(myShip.width/2)-_vertspeedmax+20) && stage.mouseY<=(myShip.y+(myShip.width/2)+_vertspeedmax+20)){
 					_pushbottom=false;
 					_pushtop=false;
 					_vertspeednow=0;
 				}
 				else{
 					if(stage.mouseY>=(myShip.y+(myShip.width/2))){
						_pushbottom=true;
						_pushtop=false;
					}
					else{
						_pushbottom=false;
						_pushtop=true;
					}
				}
			}
                        else{
				if(lastyclicked>=(myShip.y+(myShip.width/2)-_vertspeedmax+20) && lastyclicked<=(myShip.y+(myShip.width/2)+_vertspeedmax+20)){
					_vertspeednow=0;
					_pushbottom=false;
					_pushtop=false;
				}
			}
			if(_pushright && _horispeednow<=_horispeedmax ) {
 				_horispeednow+=_horispeedaccel;
 			}
 			else{
 				if(_horispeednow>0){
					_horispeednow-=_horispeeddesaccel;
				}
				if(_horispeednow<0){
 					_horispeednow+=_horispeeddesaccel;
 				}
 			}
 			if(_pushleft && _horispeednow>=-_horispeedmax) {
				_horispeednow-=_horispeedaccel;
			}
			else{
				if(_horispeednow>0){
					_horispeednow-=_horispeeddesaccel;
				}
				if(_horispeednow<0){
 					_horispeednow+=_horispeeddesaccel;
 				}
 			}
 			if(_pushtop && _vertspeednow>=-_vertspeedmax) {
				_vertspeednow-=_vertspeedaccel;
			}
			else{
				if(_vertspeednow>0){
					_vertspeednow-=_vertspeeddesaccel;
				}
				if(_vertspeednow<0){
					_vertspeednow+=_vertspeeddesaccel;
				}
				if(_vertspeednow-1){
						_vertspeednow=0;
				}
			}
			if(_pushbottom && _vertspeednow<=_vertspeedmax) {
 				_vertspeednow+=_vertspeedaccel;
 			}
 			else{
 				if(_vertspeednow>0){
					_vertspeednow-=_vertspeeddesaccel;
					
				}
				if(_vertspeednow<0){
					_vertspeednow+=_vertspeeddesaccel;
				}
				if(_vertspeednow-1){
						_vertspeednow=0;
				}
			}
			myShip.y+=_vertspeednow;
			myShip.x+=_horispeednow;
		}
	}
	
}

If you want the zip with the actual project, you can download it just in the next step of the tutorial, that is on HERE!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s