Jump to content

Collision Issue (New to phaser)


jesmaail
 Share

Recommended Posts

I'd like to preface this post by apologising, I'm new to Phaser so the issue could be really simple and I'm not seeing it.

 

Scene.Game.prototype = {		create: function() {				this.game.physics.startSystem(Phaser.Physics.ARCADE);				this.player = this.game.add.sprite(200 - 23, 540, "ship");		this.game.physics.enable(this.player, Phaser.Physics.ARCADE);		this.player.body.collideWorldBounds = true;				this.cursors = this.game.input.keyboard.createCursorKeys();						this.x = 0;	},		update: function(){		this.player.body.velocity.x = 0;						if(this.cursors.left.isDown){			this.player.body.velocity.x = -350;		}else if(this.cursors.right.isDown){			this.player.body.velocity.x = 350;		}				if(this.x % 20 ==0){					sprites = this.game.add.group();			this.enemies = sprites.create(this.game.rnd.integerInRange(0, 400), 0, 'asteroid');			this.game.physics.enable(this.enemies, Phaser.Physics.ARCADE);			this.enemies.body.immovable = true;			this.enemies.body.velocity.y = 350;				//this.enemies.body.allowGravity = false;		}				this.x = this.x + 1;						//this.game.physics.arcade.overlap(this.player, this.enemies, this.game.state.start('Death'), null, this);		//this.game.physics.arcade.collide(this.player, this.sprites, this.collision, null, this);			},		collision: function(obj1, obj2){		this.game.state.start('Death');	}		};

Using the overlap line, it goes to 'Death' state immediately, with no collision taking place.
Using the Collide line, nothing happens even when I cause a collision.

As I said, this may just be something catastrophically easy that I'm overlooking.

Thanks for reading :)

Link to comment
Share on other sites

Possibly a scope issue? Try this see if it works:

this.game.physics.arcade.collide(this.player, this.sprites, function(obj1, obj2) { this.game.state.start('Death'); }, null, this);

Or if not, try this:

var self = this;this.game.physics.arcade.collide(this.player, this.sprites, function(obj1, obj2) { self.game.state.start('Death'); }, null, this);
Link to comment
Share on other sites

in the collision check , you check for this.sprites. this.sprites does not exist, it is local on the update function You must create a variable this.sprites.

As for the overlap check you use this.game.state.start('Death') which immediatly gets called because it is a function call and not just a function name.

 

So, define the this.sprites variable on create( move the sprites = this.game.add.group(); to create as this.sprites = this.game.add.group(); ) and then use the first example Lewster wrote. It should work i guess.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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