Jump to content

TypeError: box is null


beginninggamedev
 Share

Recommended Posts

Below is my full code for the game but i'm getting an error after the game character flys through one of the holes between boxes full code is below. The error that I get is TypeError:Box is null and the line that it refers to is
    

 box.reset(x,y);
var gameState = {    preload:function()    {        db.stage.backgroundColor = '#ADD8E6';        //Load default box1        db.load.image('dBox1','assets/box1.png');        //Load droopy bird sheet        db.load.spritesheet('dBird','assets/droopy.png',49,44,2);    },    //Create game    create:function()    {        //Choose physics system        db.physics.startSystem(Phaser.Physics.ARCADE);        //Add bird player sprite        this.dBird = db.add.sprite(300,200,'dBird');        //Enable physics for bird        db.physics.arcade.enable(this.dBird);        //Make bird drop        this.dBird.body.gravity.y = 700;        //Add flap animation        this.dBird.animations.add('flap');       //Make droopy bird flap wings        this.dBird.animations.play('flap',9,true);        //Add mouse/touch controls to make bird jump        this.input.onDown.add(this.birdJump,this);        //Set up boxes group        this.boxes = db.add.group();        this.boxes.enableBody = true;        this.boxes.createMultiple(20,'dBox1');        //Call row of boxes every 1.3 secs        this.timer = db.time.events.loop(1500,this.rowOfBoxes,this);        //Set score label        this.scoreLabelHead = db.add.text(220,40,"Score:",{font:"23px Arial",fill:"#000"});        this.scoreLabel = db.add.text(300,40,"0",{font:"23px Arial",fill:"#000"});        this.levelLabelHead = db.add.text(370,40,"Level:",{font:"23px Arial",fill:"#000"});        this.levelLabel = db.add.text(445,40,"1",{font:"23px Arial",fill:"#000"});    },    update:function()    {      if(this.dBird.inWorld === false)      {          this.restartGame();      }            db.physics.arcade.overlap(this.dBird,this.boxes,this.restartGame,null,this);    },    scoreInc:function(newScore)    {      score +=newScore;      },    birdJump:function()    {        this.dBird.body.velocity.y = -280;         },    addOneBox : function(x,y)    {      var box = this.boxes.getFirstDead();      //Box new position      box.reset(x,y);      //Makes boxes move left      box.body.velocity.x = -200;       //Kill boxes when out of bounds      box.checkWorldBounds = true;      box.outOfBoundsKill = true;          },    rowOfBoxes:function()    {            var h = Math.floor(Math.random()*5)+1;            for(var i = 0; i < 9;i++)      {          if(i !== h && i !== h+1)          {              this.addOneBox(w,i*60+10);          }                }      //Update score      this.scoreInc(1);      this.scoreLabel.text = score;          },      //Restart game and call main state    restartGame:function()    {         score = 0;       level = 1;       db.state.start('main');     },    //Calculate droopyness based several factors    calDroopyness:function()    {            }};
Link to comment
Share on other sites

getFirstDead is returning null, so there are no dead boxes in the group.

Put a null check in there before trying to use the box and either bail from the function or create a new box...

Thank you I fixed it by doing this

if(this.box === null || this.box === undefined) {return;}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...