Jump to content

Organization and Collision trouble


Recommended Posts

Heya, Phaser newbie here.


I've been playing around making Screens (aka states) and Buttons using this method here. But I seem to have hit a wall trying to put together a basic level. I created a simple floor and a basic object, but they keep ignoring each other. Individually they seem to behave fine. Also I feel the need to point out that I don't know if I'm creating classes the right way (every tutorial I've come across seems to have their own way of doing this, so it ends up being a little confusing for me). Any help would be appreciated. 


Level1 code:

ForestGame.Level1_state = function (game) {  //declare the Level function		enemyATimer = 0;	enemyA = null;	levelFloor = null;	};ForestGame.Level1_state.prototype = {	    preload: function() {        // Everything in this function will be executed at the beginning. That’s where we usually load the game’s assets (images, sounds, etc.)            },    create: function() {         // This function will be called after the preload function. Here we set up the game, display sprites, add labels, etc.        var style = { font: "58px Arial", fill: "#ff0044", align: "center" };    	var text = this.add.text(this.world.centerX, this.world.height*0.2, "Hi, this is level1_state", style);    	text.anchor.setTo(0.5, 0.5);    	// set up the floor    	levelFloor = new ForestGame.LevelFloor(this);    	levelFloor.create();    	// test enemy	enemyA = new ForestGame.EnemyA(this);	enemyA.create();    	// set up phy    	this.physics.startSystem(Phaser.Physics.ARCADE);    	this.physics.enable( levelFloor, Phaser.Physics.ARCADE );    	this.physics.enable( enemyA, Phaser.Physics.ARCADE );    },         update: function() {    	//floor vs. enemy    	this.physics.arcade.collide(enemyA, levelFloor);    	//Test tags    	//console.log("Test tags a:"+enemyA.texttag+" b:"+levelFloor.texttag);    	    }   };

Floor code:

ForestGame.LevelFloor = function(gameparam) {	this.game = gameparam;	this.floorSprite = null;	this.texttag = "";};ForestGame.LevelFloor.prototype = {		preload: function() {			},	create: function() {    	//this.game.physics.startSystem(Phaser.Physics.ARCADE);    	this.floorSprite = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'lvl-floor');    	this.floorSprite.anchor.setTo(0.5, 0.5);    	this.floorSprite.y = this.game.world.height; - this.floorSprite.height/4;    	this.game.physics.enable( this.floorSprite, Phaser.Physics.ARCADE );    	//this.floorSprite.body.collideWorldBounds = false;    	    	//this.floorSprite.body.gravity.y = 1;		this.floorSprite.body.immovable = true;    	this.texttag = "floortag";  	},	update: function() {			}};

Enemy code:

ForestGame.EnemyA = function(gameparam) {	this.game = gameparam;	this.enemyASprite = null;	this.texttag = "";};ForestGame.EnemyA.prototype = {		preload: function() {			},	create: function() {    	//this.game.physics.startSystem(Phaser.Physics.ARCADE);    	this.enemyASprite = this.game.add.sprite(this.game.world.width-100, this.game.world.height - 300, 'enemya');    	this.enemyASprite.anchor.setTo(0.5, 0.5);    	this.game.physics.enable( this.enemyASprite, Phaser.Physics.ARCADE );    	this.enemyASprite.body.gravity.y = 100;    	this.enemyASprite.body.collideWorldBounds = true;    	this.texttag = "enemytag";  	},	update: function() {			}};
Link to comment
Share on other sites

Ok, as I suspected, it was a pretty newbie mistake.


I was checking the collision on the classes I had created, but not their actual bodies. So I added a getBody function to them.

getBody: function() {   return floorSprite;}


getBody: function() {   return enemyASprite;}

Then, back on the level code, I checked for collisions like this

this.physics.arcade.collide(enemyA.getBody(), levelFloor.getBody(), null, null, this);

And now it works :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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