jeudi 1 décembre 2016

TypeError: Error #1009... also when i collide with my shark, i dont lose health, nothing happens

please help, no idea how to solve this. I'm trying to make it so when my shark collides with an enemy shark, you lose health, when health hits zero, you go to a different scene known as "game over".

I keep getting the same error over and over, not sure what to do anymore. This is for a school project btw.

stage.displayState = StageDisplayState.FULL_SCREEN;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
import flash.system.fscommand;
import flash.events.TimerEvent;
import flash.utils.Timer;
stop();


var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var sharkSpeed:Number = 10;
var score1:Number = 0;
var maxHP:int = 100;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;


function updateHealthBar():void
{
     percentHP = currentHP / maxHP;
     healthBar.barColor.scaleX = percentHP;
}
function attackButtonClicked(e:MouseEvent):void
{
     currentHP -= 10;
     if(currentHP <= 0) 
     {
          currentHP = 0; 
          trace("You died! :P"); 
          MovieClip(this.root).gotoAndPlay(1, "Game Over");
     }
     updateHealthBar(); 
}
 attackButton.addEventListener(MouseEvent.CLICK, attackButtonClicked); 


stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function keyDownHandler(KeyEvent:KeyboardEvent):void
{
    if (KeyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = true;
    }
    else if (KeyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = true;
    }
    else if (KeyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = true;
    }
    else if (KeyEvent.keyCode == Keyboard.UP)
    {
        upPressed = true;
    }
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
    if (keyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = false;
    }
    else if (keyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = false;
    }
    else if (keyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = false;
    }
    else if (keyEvent.keyCode == Keyboard.UP)
    {
        upPressed = false;
    }
}
function gameLoop(loopEvent:Event):void
{
    if (rightPressed)
    {
        shark.x +=  sharkSpeed;
    }
    else if (leftPressed)
    {
        shark.x -=  sharkSpeed;
    }
    else if (downPressed)
    {
        shark.y +=  sharkSpeed;
    }
    else if (upPressed)
    {
        shark.y -=  sharkSpeed;
    }
}

MainMenu_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_2);

function fl_ClickToGoToScene_2(event:MouseEvent):void
    {
    MovieClip(this.root).gotoAndPlay(1, "Main Menu");
    }

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_2);
function fl_EnterFrameHandler_2(event:Event):void
   {
    gameScore.text = new String(score1);
    score1 +=  1;
   }

addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
   {

    enemy1.x+=  2;
    enemy2.x+=  2;
    enemy3.x+=  2;
    enemy4.x+=  2;
    enemy5.x+=  2;
    enemy6.x+=  2;
    megaladon.x+= 2;
   }

addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally_2);
function fl_AnimateHorizontally_2(event:Event)
   {
    Greenfish1.x +=  1.5;
    Greenfish2.x +=  1.5;
    Greenfish3.x +=  1.5;
    Greenfish4.x +=  1.5;
    Greenfish5.x +=  1.5;
    Brownfish1.x +=  1.5;
    Brownfish2.x +=  1.5;
    Brownfish3.x +=  1.5;
    Brownfish4.x +=  1.5;
    Brownfish5.x +=  1.5;
    Brownfish6.x +=  1.5;
   }

function hitsTheObject(e:Event)
{
   if (shark.hitTestObject(enemy1))
 {
  trace("player collided with enemy");

  currentHP -= 50;
  if(currentHP <= 0)

  {
  currentHP = 0;
  trace("You died!");
  MovieClip(this.root).gotoAndPlay(1, "Game Over");
  }
  updateHealthBar();
  }
}

Aucun commentaire:

Enregistrer un commentaire