Skip to main content
Participant
December 10, 2014
Question

TypeError: Error

  • December 10, 2014
  • 1 reply
  • 151 views

I have no idea what is wrong, I have checked for typos and everything else I can think of.

The game needs to be handed in tomorrow so help is appreciated!

These are the two errors I receive

at Shooter_fla::MainTimeline/moveEnemy()[Shooter_fla.MainTimeline::frame1:263]

at Shooter_fla::MainTimeline/Movement()[Shooter_fla.MainTimeline::frame1:83]

//Imports (these will often be created for you when required)

import flash.events.KeyboardEvent;

import flash.events.MouseEvent;

//Event Listeners (trigger specific functions)

stage.addEventListener(KeyboardEvent.KEY_DOWN, ControlDown);

stage.addEventListener(KeyboardEvent.KEY_UP, ControlUp);

stage.addEventListener(Event.ENTER_FRAME, Movement);

stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot2);

stage.addEventListener(MouseEvent.MOUSE_UP, shoot);

//Properties (Variables & Constants)

var forward:Boolean = false;

var left:Boolean = false;

var right:Boolean = false;

var back:Boolean = false;

var shots:Array = [];

var characterAngle:Number = char_mc.rotation * Math.PI / 180;

var spaceBar:Boolean = false;

var bulletSpeed:int = 7;

var movementspeed:int = 4;

var timer:int = 20;

var mousedown:Boolean = false;

var enemies:Array = [];

var score:int = 0;

var lives:int = 3;

Score_mc.Score_txt.text = String(score);

Score_mc.Lives_txt.text = String(lives);

//make the EndGame symbol invisible

GameOver_mc.visible = false;

Mouse.hide();

//This function deals with keyboard controls;

function Movement(event:Event):void

{

  if (forward)

  {

  char_mc.y -=  movementspeed;

  }

  if (back)

  {

  char_mc.y +=  movementspeed;

  }

  if (left)

  {

  char_mc.x -=  movementspeed;

  }

  if (right)

  {

  char_mc.x +=  movementspeed;

  }

  if (mousedown)

  {

  if (timer <= 0)

  {

  timer = 20;

  addBullet(char_mc.x, char_mc.y, char_mc.rotation);

  }

  }

  char_mc.rotation = -(Math.atan2(char_mc.x - mouseX, char_mc.y - mouseY) * 180 / Math.PI)  -90;

  crosshair.x = mouseX;

  crosshair.y = mouseY;

  timer--;

  bulletAction();

  addEnemy();

  moveEnemy();

  hitEnemy();

}

function ControlDown(event:KeyboardEvent):void

{

  switch (event.keyCode)

  {

  case 87 :

  forward = true;

  break;

  case 65 :

  left = true;

  break;

  case 68 :

  right = true;

  break;

  case 83 :

  back = true;

  break;

  case Keyboard.SPACE :

  spaceBar = true;

  break;

  }

}

function ControlUp(event:KeyboardEvent):void

{

  switch (event.keyCode)

  {

  case 87 :

  forward = false;

  break;

  case 65 :

  left = false;

  break;

  case 68 :

  right = false;

  break;

  case 83 :

  back = false;

  break;

  case Keyboard.SPACE :

  spaceBar = false;

  break;

  }

}

function shoot2(e:MouseEvent):void

{

  mousedown = true;

}

function shoot(e:MouseEvent):void

{

  mousedown = false;

}

function addBullet(startX, startY, rot):void

{

  var s:Shot = new Shot();

  s.x = startX;

  s.y = startY;

  s.rotation = rot;

  //add the new shot to the stage

  stage.addChild(s);

  //store the object in an array

  shots.push(s);

}

function bulletAction():void

{

  for (var i:int = 0; i <= shots.length - 1; i++)

  {

  shots.x +=  bulletSpeed * Math.cos(shots.rotation * Math.PI / 180);

  shots.y +=  bulletSpeed * Math.sin(shots.rotation * Math.PI / 180);

  if (shots.x > stage.stageWidth ||shots.y > stage.stageHeight || shots.x < 0 || shots.y < 0)

  {

  stage.removeChild(shots);

  shots.splice(i, 1);

  }

  }

}

function addEnemy():void

{

  //limit number of enemies on screen

  if (enemies.length < 3

    )

  {

  //declare an object instance from the enemy Class

  var e:Enemy = new Enemy();

  //set the enemy start point to the far right of the stage

  var startX:int = Math.random() * stage.stageWidth;

  //Randomise where the enemy starts on the right

  var startY:int = Math.random() * stage.stageHeight;

  //set the new bullet's x,y coordinates

  e.x = startX;

  e.y = startY;

  //add the new enemy to the stage

  stage.addChild(e);

  //store the object in an array

  enemies.push(e);

  }

}

function moveEnemy():void

{

  //loop through all instances of the enemy

  //loop from '0' to 'number_of_enemies'

  for (var i:int = 0; i <= enemies.length - 1; i++)

  {

  //if the enemy is below the ship then move up

  if (enemies.y > char_mc.y)

  {

  enemies.y -=  1;

  //else if the enemy is above the ship then move down

  }

  else if (enemies.y < char_mc.y)

  {

  enemies.y +=  1;

  }

  //if the enemy is below the ship then move up

  if (enemies.x > char_mc.x)

  {

  enemies.x -=  1;

  //else if the enemy is above the ship then move down

  }

  else if (enemies.x < char_mc.x)

  {

  enemies.x +=  1;

  }

  //if enemy hits the player's ship

  if (enemies.hitTestObject(char_mc))

  {

  //remove the enemy from the stage

  stage.removeChild(enemies);

  //remove the enemy from the array

  enemies.splice(i, 1);

  lives -=  1;

  Score_mc.Lives_txt.text = String(lives);

  if (lives <= 0)

  {

  //remove event listeners to cease all actions on stage

  stage.removeEventListener(KeyboardEvent.KEY_DOWN, ControlDown);

  stage.removeEventListener(KeyboardEvent.KEY_UP, ControlUp);

  stage.removeEventListener(Event.ENTER_FRAME, Movement);

  stage.removeEventListener(MouseEvent.MOUSE_DOWN, shoot2);

  stage.removeEventListener(MouseEvent.MOUSE_UP, shoot);

  //remove bullets (as these will appear...

  //...in front of our EndGame symbol)

  for (var j:int = 0; j <= shots.length - 1; j++)

  {

  stage.removeChild(shots);

  }

  //remove enemies (as these will appear...

  //...in front of our EndGame symbol)

  for (var k:int = 0; k <= enemies.length - 1; k++)

  {

  stage.removeChild(enemies);

  }

  //set final score

  GameOver_mc.FinalScore_txt.text = String(score);

  //show EndGame symbol

  GameOver_mc.visible = true;

  }

  }

  }

}

function hitEnemy():void

{

  //loop through all bullets...

  for (var i:int = 0; i <= shots.length - 1; i++)

  {

  //...and loop through all enemies for each bullet

  for (var j:int = 0; j <= enemies.length - 1; j++)

  {

  //check each bullet against each enemy for hits

  if (shots.hitTestObject(enemies))

  {

  //remove the bullet and enemy from the stage

  stage.removeChild(shots);

  stage.removeChild(enemies);

  //remove the bullet and enemy from their arrays

  shots.splice(i, 1);

  enemies.splice(j, 1);

  }

  }

  }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 10, 2014

your errors are on lines 83 and 263.

what are those lines and what are the error messages?