Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Player class not being added in


samcrowleymusic
 Share

Recommended Posts

Hi there,

Previously I had my player being created inside of the level state, but now I'm working on separating the player into its own object which I can create an instance of in the play state.

Here's my two files:

Player.js:

Player = function(game, x, y) {
	
	Phaser.Sprite.call(this, game, x, y, 'player');
	this.anchor.setTo(0.5, 0.5);
	this.game.physics.enable(this);
	
}

Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;

Player.prototype.update = function() {

    this.handleInput();

}

Player.prototype.handleInput = function() {
	if(keys.D.isDown) this.body.velocity.x += 1;
	if(keys.A.isDown) this.body.velocity.x -= 1;
	if(this.body.onFloor() && keys.SPACE.isDown) this.body.velocity.y -= 4;
};

playstate.js:

var playState = {

	create: function() {

		this.world.add(slickUI.container.displayGroup);
		
		game.stage.backgroundColor = "#1e1e1e";
		
		game.physics.arcade.gravity.y = 400;

		map = game.add.tilemap('map1');
		map.addTilesetImage('moontileset', 'tileset');
		layer = map.createLayer('Tile Layer 1');
		layer.resizeWorld();
		map.setCollisionBetween(1, 16);
		
		player = new Player(game, 128, 96);

		game.camera.follow(player, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1);

	},

	update: function() {

		game.physics.arcade.collide(player, layer);
		player.update();

	}

}

 

 

When I run the game, I get no errors, but the player isn't appearing on screen. Everything else works fine. The methods inside player are being called, everything. I don't know what I've done wrong.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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