dimanche 22 janvier 2017

Im trying to import a game class and i get this message when i startup "Cannot access a property or method of a null object reference."?

I'm making an app for an assignment and i'm importing this game onto a page for some to click on a button at the game pops up. But i get this error and i've searched online and people say it could be anything..... I'm a beginner at AS3 so i'm not good at coding, but does anyone know why this is happening?

I would like the bottle to be in a random order but I'll have to come to that when it appears for me.

Here's the code:

package {

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;

public class Shoot extends MovieClip {

    private var bottleArray:Array = new Array();
    private var numOfBottle:uint = 9;
    private var bottleHit:uint = 0;
    private var hits:uint = 6;
    private var bg:Sprite;

    var bottle:MovieClip = new Bottle
    var shots:MovieClip = new Shots
    var endBox:MovieClip = new EndBox

    public function Shoot() {

    addEventListener(Event.ENTER_FRAME, enterHandler);


       //Adds the bottles onto the stage.
       for (var i:int = 0; i < numOfBottle; i++) {
        bottle.x = 0 + (i * 200);
        bottle.y = 344.35;
        bottle.addEventListener(MouseEvent.CLICK, clickHandler);
        addChild(bottle);
        bottleArray.push(bottle);
       }

       //Adds a invisibe background to detect when the targets have been missed.
       bg = new Sprite();
       bg.graphics.beginFill(0x000000,0);
       bg.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
       bg.graphics.endFill();
       addChildAt(bg,0);
       bg.addEventListener(MouseEvent.CLICK, checkShots);

       //Adds the number shot left to stage. 
       shots.x = 480;
       shots.y = 80;
       addChild(shots);

       addChild(endBox);
       endBox.x = 220;
       endBox.y = 550;
       endBox.visible = false;

      }


      //This function moves all the bottles in the array four pixels to the 
      //right at the current frame rate. If a bottle goes passed the stage 
      //width, it will get re-added a bottle width before the start of the stage.
    function enterHandler(e:Event):void{
       for (var j:uint = 0; j <  bottleArray.length; j++) {
        bottleArray[j].x +=  1;

        if (bottleArray[j].x > stage.stageWidth + bottleArray[j].width) {
         bottleArray[j].x = 0 - bottleArray[j].width;
        }
       }
      }


      //This removes the bottle from the stage, the event listener and 
      //removes it from the array if it has been clicked. 
      function clickHandler(e:MouseEvent):void {
       var index:uint = bottleArray.indexOf(e.currentTarget);
       var target:MovieClip = e.currentTarget as MovieClip;

       target.removeEventListener(MouseEvent.CLICK, clickHandler);
       removeChild(target);
       bottleArray.splice(index, 1);

       checkShots();
      }


      function checkShots(e:MouseEvent = null):void{
       //This increments the bottleHit counter by one and goes to 
       //the next frame in shots movie clip.
       bottleHit++;
       shots.gotoAndStop(shots.currentFrame + 1);


       //If there are no more shot available and array length is six then you win. 
       if (shots.currentFrame == shots.totalFrames && bottleArray.length == hits) {
        removeObject();
        endBox.visible = true;
        endBox.gotoAndStop(1);
       }
       //If there are no more shot available you lose.
       else if (shots.currentFrame == shots.totalFrames) {
        removeObject();
        endBox.visible = true;
        endBox.gotoAndStop(2);
       }
      }

      //This removes the event listeners
      function removeObject():void{
       removeEventListener(Event.ENTER_FRAME, enterHandler);
       bg.removeEventListener(MouseEvent.CLICK, checkShots);

       for (var i:int = 0; i < bottleArray.length; i++){
        bottleArray[i].removeEventListener(MouseEvent.CLICK, clickHandler);
       }
      }
     }
    }

Aucun commentaire:

Enregistrer un commentaire