Jump to content

Phaser arcade physics player has infinite jumps.


TheNerdyDev
 Share

Recommended Posts




var mainState = {

	preload: function() {
		// This function will be executed at the beginning
		// That's where we load the game's assets
		game.load.image('player', 'assets/player.png');
		game.load.image('wallV', 'assets/wallVertical.png');
		game.load.image('wallH', 'assets/wallHorizontal.png');
		game.load.image('player2', 'assets/secondplayer.png');


	},
	create: function() { 

		game.stage.backgroundColor = '#3498db';
		game.physics.startSystem(Phaser.Physics.ARCADE);

		this.player2 = game.add.sprite(200,100, 'player2');
		this.player2.anchor.setTo(0.5, 0.5);
		game.physics.arcade.enable(this.player2); //first enable gravity
		this.player2.body.gravity.y = 500; //then use gravity properties.
		
		this.player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');
		this.player.anchor.setTo(0.5, 0.5);
		game.physics.arcade.enable(this.player);
		this.player.body.gravity.y = 500;
		this.cursor = game.input.keyboard.createCursorKeys();
		
		// Create a new group
		this.walls = game.add.group();
		// Add Arcade physics to the whole group
		this.walls.enableBody = true;
		// Create 2 walls in the group
		game.add.sprite(0, 0, 'wallV', 0, this.walls); // Left wall
		game.add.sprite(780, 0, 'wallV', 0, this.walls); // Right wall
		game.add.sprite(0,580, 'wallH' , 0, this.walls); //horizontal wall.
		this.walls.setAll('body.immovable', true);
	//	this.createWorld();
		
	},
	//timer
	update: function() {
		game.physics.arcade.collide(this.player, this.walls);
		this.movePlayer();
		game.physics.arcade.collide(this.player2, this.walls);


	},

	movePlayer: function() {
		// If the left arrow key is pressed
		if (this.cursor.left.isDown) {
			// Move the player to the left
			this.player.body.velocity.x = -200;
		}
		// If the right arrow key is pressed
		else if (this.cursor.right.isDown) {
			// Move the player to the right
			this.player.body.velocity.x = 200;
		}

		else if (this.cursor.up.isDown) && (this.player.body.touching.wallH == true) { 

			this.player.body.velocity.y = -500;
				//this.cursor.up.isDown := false
		}
		else if (this.cursor.down.isDown) {
			this.player.body.velocity.y = 200;
		}

		// If neither the right or left arrow key is pressed
		else {
			// Stop the player
			this.player.body.velocity.x = 0;
			//this.player.body.velocity.y = 0;
		}
		// If the up arrow key is pressed and the player is touching the ground
		if (this.cursor.up.isDown && this.player.body.touching.down) {
			// Move the player upward (jump)
			this.player.body.velocity.y = -320;
		}

	},


};

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameDiv');
	game.state.add('main', mainState);
	game.state.start('main');
	

Hello!

I am very new to Phaser, only started out a few days ago. As you might expect, I will do some dumb mistakes. My issue here is that my character that works, (the one controllable with arrow keys) has the ability to jump to infinity. I have tried a bit of code with a double if condition to solve this, but I can't get the syntax right and I don't know how to check if the player is touching the horizontal wall or not. (I don't want it to jump unless it's touching the horizontal wall, that way I can't keep on jumping in mid air.

 

Here's the horrible bit of code I am attempting to get to work:

//if i'm pressing up AND the player is touching the wall THEN set vertical velocity to -500px.

else if (this.cursor.up.isDown) && (this.player.body.touching.wallH == true) {

            this.player.body.velocity.y = -500;
  }

I have attached all my actual code. I'm new here, I don't know how to write this condition, thanks for your help :D

 

main.js

Link to comment
Share on other sites

Ok, I fixed it. Was a pretty dumb mistake, as expected.

Here's my new code:

}
        // If the up arrow key is pressed and the player is touching the ground
        if (this.cursor.up.isDown && this.player.body.touching.down) {
            // Move the player upward
            this.player.body.velocity.y = -450;
        }

Thanks for the help.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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