A Simple Shooter

17 posts

Flag Post

In my game I have made an ‘Engine’ class, which is the game’s engine, which will generate 2 ‘Ships’. However, the ships are not being generated. This is the ‘Engine’ class:

package com.argon98.shooter
{
	import flash.display.MovieClip;
	import flash.display.Stage;
 
	public class Engine extends MovieClip
	{
		public function Engine()
		{
			var Ship1:Ship = new Ship();
			stage.addChild(Ship1);
			Ship1.x = 100;
			Ship1.y = 150;
			
			var Ship2:Ship = new Ship();
			stage.addChild(Ship2);
			Ship2.x = 50;
			Ship2.y = 250;
		}
	}
}
 
Flag Post

The ships are now being displayed :) but now they cannot move :(

How do i make ships move with arrow keys in AS3?

This is my Ship.as file:

package com.argon98.shooter
{
 
	import flash.display.MovieClip;
	import flash.display.Stage;
	import com.argon98.shooter.Movement;
	import flash.ui.Keyboard;
	import flash.events.Event;
 
	public class Ship extends MovieClip
	{
 
		private var stageRef:Stage;
		private var key:Movement;
		private var speed:Number = 5;
 
		public function Ship(stageRef:Stage)
		{
			this.stageRef = stageRef;
			key = new Movement(stageRef);
 
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
 
		public function loop(e:Event) : void
		{
			if (key.isDown(Keyboard.LEFT))
				x -= speed;
			else if (key.isDown(Keyboard.RIGHT))
				x += speed;
 
			if (key.isDown(Keyboard.UP))
				y -= speed;
			else if (key.isDown(Keyboard.DOWN))
				y += speed;
		}
 
	}
 
}
 
Flag Post
package com.argon98.shooter
{

	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;

	public class Ship extends MovieClip
	{
		
		private var stageRef:Stage;
		private var speed:Number = 5;
		private var keys:Array = [false, false, false, false];
		
		public function Ship(stageRef:Stage):void
		{
			this.stageRef = stageRef;
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKey, false, 0, true);
		}
		
		private function onKey(e:KeyboardEvent):void 
		{
			var down:Boolean = e.type == "keyDown";
			switch(e.keyCode) {
				case Keyboard.UP	: keys[0] = down; break;
				case Keyboard.DOWN	: keys[1] = down; break;
				case Keyboard.LEFT	: keys[2] = down; break;
				case Keyboard.RIGHT	: keys[3] = down; break;
			}
		}
		
		public function loop(e:Event):void
		{
			if (keys[0]) y -= speed;
			else if (keys[1]) y += speed;
			if (keys[2]) x -= speed;
			else if (keys[3]) x += speed;
		}
		
	}

}
 
Flag Post

For future reference, throw your code between <pre> </pre> tags. Read the stickied formatting thread for more info.

 
Flag Post

DPBrad, thanks for the code, but I fixed the problem myself :P

 
Flag Post

I have another problem. :(

When I press Ctrl+Enter, the following error message is given:
C:\Users\User\Desktop\Flash\Files\com\asgamer\basics1\Ship.as, Line 107 1136: Incorrect number of arguments. Expected 3.

This is the ship.as class:

package com.asgamer.basics1
{
 
	import flash.display.MovieClip;
	import flash.display.Stage;
	import com.senocular.utils.KeyObject;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
 
	public class Ship extends MovieClip
	{
 
		private var stageRef:Stage;
		private var key:KeyObject;
		private var speed:Number = 0.5;
		private var vx:Number = 0;
		private var vy:Number = 0;
		private var friction:Number = 0.93;
		private var maxspeed:Number = 8;
 
		//fire related variables
		private var fireTimer:Timer; //causes delay between fires
		private var canFire:Boolean = true; //can you fire a laser
 
		public function Ship(stageRef:Stage)
		{
			this.stageRef = stageRef;
			key = new KeyObject(stageRef);
 
			//setup your fireTimer and attach a listener to it.
			fireTimer = new Timer(300, 1);
			fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
 
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
 
		public function loop(e:Event) : void
		{
			//keypresses
			if (key.isDown(Keyboard.LEFT))
				vx -= speed;
			else if (key.isDown(Keyboard.RIGHT))
				vx += speed;
			else
				vx *= friction;
 
			if (key.isDown(Keyboard.UP))
				vy -= speed;
			else if (key.isDown(Keyboard.DOWN))
				vy += speed;
			else
				vy *= friction;
 
			if (key.isDown(Keyboard.SPACE))
				fireBullet();
 
			//update position
			x += vx;
			y += vy;
 
			//speed adjustment
			if (vx > maxspeed)
				vx = maxspeed;
			else if (vx < -maxspeed)
				vx = -maxspeed;
 
			if (vy > maxspeed)
				vy = maxspeed;
			else if (vy < -maxspeed)
				vy = -maxspeed;
 
			//ship appearance
			rotation = vx;
			scaleX = (maxspeed - Math.abs(vx))/(maxspeed*4) + 0.75;
 
			//stay inside screen
			if (x > stageRef.stageWidth)
			{
				x = stageRef.stageWidth;
				vx = -vx;
			}
			else if (x < 0)
			{
				x = 0;
				vx = -vx;
			}
 
			if (y > stageRef.stageHeight)
			{
				y = stageRef.stageHeight;
				vy = -vy;
			}
			else if (y < 0)
			{
				y = 0;
				vy = -vy;
			}
 
		}
 
		private function fireBullet() : void
		{
			if (canFire)
			{
				stageRef.addChild(new LaserBlue());
				canFire = false;
				fireTimer.start();
			}
 
		}
 
		//HANDLERS
 
		private function fireTimerHandler(e:TimerEvent) : void
		{
			//timer ran, we can fire again.
			canFire = true;
		}
 
	}
 
}

EDIT: Line 107 of this class is “stageRef.addChild(new LaserBlue());”

 
Flag Post

If you read the error you would know the problem: “Incorrect number of arguments”

Incorrect: wrong, not right
number: an amount
arguments: something given to a function e.g. function (blah:string, blah2:string) has 2 arguments.

Therefore you did not give all three arguments in new LaserBlue(), it should be something like new LaserBlue(args1, args2,args3);

 
Flag Post
Originally posted by RTL_Shadow:

If you read the error you would know the problem: “Incorrect number of arguments”

Incorrect: wrong, not right
number: an amount
arguments: something given to a function e.g. function (blah:string, blah2:string) has 2 arguments.

Therefore you did not give all three arguments in new LaserBlue(), it should be something like new LaserBlue(args1, args2,args3);

Thanks for the reply :)

 
Flag Post

I got another error. :(

C:\Users\User\Desktop\Flash\Files\com\asgamer\basics1\EnemyBullet.as, Line 37 1136: Incorrect number of arguments. Expected 0.

This is the EnemyBullet.as class:

package com.asgamer.basics1
{
 
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
 
	public class EnemyBullet extends MovieClip
	{
 
		private var stageRef:Stage;
		private var target:Ship;
 
		private var vx:Number;
 
		public function EnemyBullet(stageRef:Stage, target:Ship, x:Number, y:Number, vx:Number) : void
		{
			this.stageRef = stageRef;
			this.target = target;
			this.x = x;
			this.y = y;
			this.vx = vx;
 
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
 
		private function loop(e:Event) : void
		{
			x += vx;
 
			if (x > stageRef.stageWidth || x < 0)
				removeSelf();
 
			if (hitTestObject(target.hit))
			{
				trace("hitME");
				stageRef.addChild(new SmallImplosion(stageRef, x, y));
				removeSelf();
			}
		}
 
		private function removeSelf() : void
		{
			removeEventListener(Event.ENTER_FRAME, loop);
			if (stageRef.contains(this))
				stageRef.removeChild(this);
		}
 
	}
 
}

(Line 37 is “stageRef.addChild(new SmallImplosion(stageRef, x, y));”)

If I remove the arguments from that line, the SmallImplosion is generated at 0,0.

 
Flag Post

Either change the parameters taken in the SmallImplosion constructor or pass the values to it after construction. Something like

var imp:SmallImplosion = new SmallImplosion();
stageRef.addChild(imp);
imp.x = x;
imp.y = y;
imp.stageRef = stageRef
 
Flag Post
Originally posted by Shake_N_Baker:

Either change the parameters taken in the SmallImplosion constructor or pass the values to it after construction. Something like

var imp:SmallImplosion = new SmallImplosion();
stageRef.addChild(imp);
imp.x = x;
imp.y = y;
imp.stageRef = stageRef

Thanks for the quick reply! :)

 
Flag Post

Btw, why are you passing all these “stageRef”’s about? If would be easier to just have a static variable in your Main class where there is a reference to the stage, so you dont have to keep passing it around.

eg;

public static var stageRef:Stage;

public function Main():void
{
    if(stage) init();
    else addEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
}

public function init(e:Event=null):void
{
    stageRef = stage;
    //Then whatever
}

And then, from then on, instead of passing the stageRef to every object, you can just reference it easily using Main.stageRef.

Not that I would particularly recommend adding everything directly to the stage, mind.

 
Flag Post

^ it’s just one of several ways to create global variables. As always they are useful as long as they are only used for what they absolutely have to.

 
Flag Post

I am getting more errors…

C:\Users\User\Desktop\Flash\Files\com\asgamer\basics1\EnemyBullet.as, Line 31 1084: Syntax error: expecting rightparen before semicolon.
C:\Users\User\Desktop\Flash\Files\com\asgamer\basics1\EnemyBullet.as, Line 31 1008: Attribute is invalid.
C:\Users\User\Desktop\Flash\Files\com\asgamer\basics1\EnemyBullet.as, Line 31 1084: Syntax error: expecting rightbrace before rightparen.

EnemyBullet.as:

package com.asgamer.basics1
{
 
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
 
	public class EnemyBullet extends MovieClip
	{
 
		private var stageRef:Stage;
		private var target:Ship;
 
		private var vx:Number;
 
		public function EnemyBullet(stageRef:Stage, target:Ship, x:Number, y:Number, vx:Number) : void
		{
			this.stageRef = stageRef;
			this.target = target;
			this.x = x;
			this.y = y;
			this.vx = vx;
 
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
 
		private function loop(e:Event) : void
		{
			x += vx;
 
			if (x &gt; stageRef.stageWidth || x &lt; 0) //line 31
				removeSelf();
 
			if (hitTestObject(target.hit))
			{
				target.takeHit();
				stageRef.addChild(new SmallImplosion(stageRef, x, y));
				removeSelf();
			}
		}
 
		private function removeSelf() : void
		{
			removeEventListener(Event.ENTER_FRAME, loop);
			if (stageRef.contains(this))
				stageRef.removeChild(this);
		}
 
	}
 
}
 
Flag Post
 if (x &gt; stageRef.stageWidth || x &lt; 0) 

… the

>
and
<
parts both are somehow in their ascii forms (I think?), they make no sense in that context and they have semicolons ( ; ). The semicolon tells the compiler where the end of a command is, putting them haphazardly into an if statement will make the program do weird things and throw errors.

 
Flag Post

TBH it seems like you are copy-pasting the code, some of it doesn’t even make sense. I suggest you read the tutorials if you aren’t.

 
Flag Post

It is. It comes from ASGamer. I know, cause I learnt AS3 through it. You won’t learn if you just download the source.