lundi 5 décembre 2016

Why does my shark not get damaged when colliding with other sharks?

I also get errors on my output such as:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at AttackonSharkwithMovement_fla::MainTimeline/fl_AnimateHorizontally()

TypeError: Error #1009: Cannot access a property or method of a null object reference. at AttackonSharkwithMovement_fla::MainTimeline/fl_AnimateHorizontally_2()

TypeError: Error #1009: Cannot access a property or method of a null object reference. at AttackonSharkwithMovement_fla::MainTimeline/fl_EnterFrameHandler_2()[

Scene 1 - Main Menu

    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.MouseEvent

    stop();
    Play_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene);

    function fl_ClickToGoToScene(event:MouseEvent):void
    {
        MovieClip(this.root).gotoAndPlay(1, "Game");
    }

    Instructions_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_10);

    function fl_ClickToGoToAndStopAtFrame_10(event:MouseEvent):void
    {
        gotoAndStop(6);
    }

    function quit (event:MouseEvent):void
    {
        fscommand ("quit");
    }
    Quit_Button.addEventListener(MouseEvent.MOUSE_DOWN,quit);

Scene 2 - Game

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 = String(score1);
    score1 +=  1;
    trace("gameScore.text is : " + gameScore.text);
    trace("score1 is : " + score1);
}

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)
{
    fishes.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();
    }
}

var myTimer:Timer = new Timer(1000,50);
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();

function onTimer(e: TimerEvent):void
{
    myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}

function onComplete(e: TimerEvent):void
{
    MovieClip(this.root).gotoAndPlay(1, "You Survived");
}

Scene 3 - You Survived

stop();

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

PlayAgain_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_12);

function fl_ClickToGoToScene_12(event:MouseEvent):void
{
    MovieClip(this.root).gotoAndPlay(1, "Game");
}

Scene 4 - Game Over

stop();
MainMenu_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_9);

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

PlayAgain_Button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_11);

function fl_ClickToGoToScene_11(event:MouseEvent):void
{
    MovieClip(this.root).gotoAndPlay(1, "Game");
}

Aucun commentaire:

Enregistrer un commentaire