Jump to content

Phaser Platformer: Not Jumping


zakuhtet
 Share

Recommended Posts

Hello.

I'm making a platformer in Phaser but when I tested it, the player's not jumping at all.

Here's the code just below:

var mainState = {
	preload: function() {
		game.load.image('player', 'assets/player.png');
		game.load.image('wall', 'assets/wall.png');
		game.load.image('coin', 'assets/coin.png');
		game.load.image('enemy', 'assets/enemy.png');
	},

	create: function() {
		game.stage.backgroundColor = '#3598db';
		game.physics.startSystem(Phaser.Physics.ARCADE);
		game.world.enableBody = true;

		this.cursor = game.input.keyboard.createCursorKeys();
		this.player = game.add.sprite(70, 100, 'player');
		this.player.body.gravity.y = 250;

		this.walls = game.add.group();
		this.coins = game.add.group();
		this.enemies = game.add.group();

		var level = [
			'xxxxxxxxxxxxxxxxxxxxxx',
			'!         !          x',
    		'!                 o  x',
    		'!         o          x',
    		'!                    x',
    		'!     o   !    x     x',
    		'xxxxxxxxxxxxxxxx!!!!!x',
		];

		for(var i = 0; i < level.length; i++){
			for(var j = 0; j < level[i].length; j++){
				if(level[i][j] == 'x'){
					var wall = game.add.sprite(30+20*j, 30+20*i, 'wall');
					this.walls.add(wall);
					wall.body.immovable = true;
				}
				else if(level[i][j] == 'o'){
					var coin = game.add.sprite(30+20*j, 30+20*i, 'coin');
					this.coins.add(coin);
				}
				else if(level[i][j] == '!'){
					var enemy = game.add.sprite(30+20*j, 30+20*i, 'enemy');
					this.enemies.add(enemy);
				}
			}
		}
		
		//this.labelScore = game.add.text(20, 20, this.player.body.touching.down + " " + this.cursor.up.isDown, {font: "12px Arial", fill:"#ffffff"});
	},

	update: function() {
		if (this.cursor.left.isDown) 
			this.player.body.velocity.x = -200;
		else if (this.cursor.right.isDown) 
			this.player.body.velocity.x = 200;
		else 
			this.player.body.velocity.x = 0;

		if (this.cursor.up.isDown && this.player.body.touching.down) 
			this.player.body.velocity.y = -250;
		
		game.physics.arcade.collide(this.player, this.walls);
		game.physics.arcade.overlap(this.player, this.coins, this.takeCoin, null, this);
		game.physics.arcade.overlap(this.player, this.enemies, this.restart, null, this);
	},
	
	render: function() {
		game.debug.text(this.cursor.up.isDown + " " + this.player.body.touching.down + " " + this.player.body.velocity.y, 20, 20);
	},

	takeCoin: function(player, coin) {
    	coin.kill();
	},

	restart: function() {
    	game.state.start('main');
	},
};

var game = new Phaser.Game(500, 200);
game.state.add('main', mainState);
game.state.start('main');

Notes:

-"touched,down" and "cursor keys" worked fine though.

-Also any optimization needed with the code? I know that the tile map needed to be improved. I was following a tutorial.

Link to comment
Share on other sites

You should collide player vs. walls before you test `player.body.touching`: http://codepen.io/anon/pen/ALbEGL?editors=0010 The `touching` flags are reset at the start of each update so you're just missing it (but still getting `touched` from the previous update).

The rest looks fine. You can omit `physics.startSystem(…)` (unnecessary for Arcade Physics). And you can use `game.state.restart()` if you like. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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