Jump to content

help with uncaught typeError


ibb671
 Share

Recommended Posts

Hello if anyone can help me with my code. I'm trying to make a platformer game to learn phaser. Whenever a bullet hits an enemy in my game I get this error

phaser.js:62544 Uncaught TypeError: Cannot read property 'right' of null

i think this is the code that is causing the problem but I'm having trouble fixing it. 

 
//This code is inside the update method 
this.game.physics.arcade.overlap(this.bullets,this.enemies,this.killBulletOverlapEnemy,null,this);

this.game.physics.arcade.overlap(this.bullets,this.collisionLayer,this.killBulletOverlapCollision,null,this);

this.bullets.setAll("checkWorldBounds",true);
this.bullets.setAll("outOfBoundsKill",true);




killBulletOverlapEnemy:function(bullet,enemy){
  bullet.destroy();
  enemy.destroy();
},

killBulletOverlapCollision:function(bullet,collision){
  bullet.destroy();
  collision.destroy();
}

 

Capture.PNG

Link to comment
Share on other sites

I did use the newer version but i got an error in the first screen shot. So i switched it back to version i was using and used enemy.kill() instead. That seemed to work but i found another bug where it would shoot bullets in both directions here is my current code

 

shootBullet:function(){
  var getDeadBullet = this.bullets.getFirstDead();
  if(getDeadBullet){
    //bring it back to life
   
   getDeadBullet.reset(this.player.x +10,this.player.y+10);
  }


  if(this.bullets.length<5){
    var bullet = new ZPlat.Bullet(this.game,this.player.x + 10,this.player.y+10,this.direction,this.bulletXSpeed);
    this.bullets.add(bullet);
  }
},

killBulletOverlapEnemy:function(bullet,enemy){
  bullet.kill();
  enemy.kill();
},

killBulletOverlapCollision:function(bullet,collision){
  bullet.kill();
  
}

 

 

 

Capture.PNG

Capture.PNG

Link to comment
Share on other sites

When you call "destroy", Phaser immediately removes a bunch of essential stuff from sprites, including its physics body. It also removes it from its parent. I'm betting that your bullet is destroyed, and then something is trying to check its bounds afterwards. You *probably* want "kill" instead of destroy. "kill" implies that you're going to re-use whatever it is that you're "kill"ing (like bullets! in a pool of bullets). "destroy" means "I never want to see this again for the life of my game" (probably not bullets!).

I don't see enough code around shooting bullets to determine how their velocity is calculated.

Link to comment
Share on other sites

 Oh ok, so using kill is to be more efficient basically. Here is the prefab i used for bullets. Still learning Phaser and programming in general hehe so i hope my code is understandable

ZPlat = ZPlat || {};
ZPlat.Bullet = function(game,x,y,direction,speed){
	Phaser.Sprite.call(this,game,x,y,"bullet");
	this.game.physics.enable(this,Phaser.Physics.ARCADE);
	this.xSpeed = direction * speed;
	this.x=x;
	//this.game=game;
	this.y=y;
	this.direction=direction;
	this.speed=speed;
};

ZPlat.Bullet.prototype = Object.create(Phaser.Sprite.prototype);
ZPlat.Bullet.prototype.constructor = ZPlat.Bullet;


ZPlat.Bullet.prototype.update=function(){
	
	this.body.velocity.y=0;
	this.body.velocity.x = this.xSpeed;
	
};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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