Jump to content

left and right Jumping Problem in Phaser


wareja
 Share

Recommended Posts

Hello I am new to phaser and I'm having a problem with my player who seems to not be able to jump left and right but can jump up and down when I just press the jump button.

Can somebody help me?


        //-------------------player rendering-------------------------------------
        this.player = this.game.add.sprite(10, 282, 'exitar');
        this.game.physics.arcade.enable(this.player, Phaser.Physics.ARCADE);
        this.player.body.gravity.y = 300;
        this.player.body.collideWorldBounds = true;
        this.player.anchor.setTo(0, 0);
        this.player.body.setSize(50, 100, 20, 0);
        this.game.camera.follow(this.player);

        this.player.animations.add('right', [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25], 10, true);
        this.player.animations.add('left', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12], 10, true);
        this.player.animations.add('jump_left', [26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36], 7, true);
        this.player.animations.add('jump_right', [37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47], 8, true);
        this.player.animations.add('win', [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65], 17, false);
        this.player.animations.add('still', [48, 49], 2, true);
        //-------------------cursor settings---------------------------------------
        this.cursors = this.game.input.keyboard.createCursorKeys();
        jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.UP);
        left = this.game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
        right = this.game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
    },
    update: function () {

        this.game.physics.arcade.collide(this.player, this.groundLayer);
        this.game.physics.arcade.collide(this.player, this.stairsLayer);
        this.game.physics.arcade.collide(this.player, this.trash);
        //this.my_time();

        if (state_direction) {
            if (left.isDown && this.player.body.blocked.down) {
                this.player.body.velocity.x = -100;
                this.player.animations.play('left');
            } 
            else if (right.isDown && this.player.body.blocked.down) {
                this.player.body.velocity.x = 100;
                this.player.animations.play('right');
            } 
            else if (jumpButton.isDown && left.isDown && this.player.body.blocked.down) {
                this.jumpLeft();
            } 
            else if (jumpButton.isDown && right.isDown && this.player.body.blocked.down) {
                this.jumpRight();
            } 
            else if (jumpButton.isDown && this.player.body.blocked.down) {
                this.jump();
            }
            else if (this.game.physics.arcade.distanceToXY(this.player, 744, 510) < 92) {
                this.player.alpha = 0;
                this.train.animations.play('close')
                this.time.events.add(1000, this.go, this);

            } 
            else 
            {
                this.player.animations.play('still');
                this.player.body.velocity.x = 0;
            }
        }
    },

    jumpLeft: function () {
        state_direction = false;

        this.player.body.velocity.y = -200;
        this.player.body.velocity.x = -100
        this.time.events.add(2000, this.my_time, this);
        this.player.animations.play('jump_left');
        //state_direction = true;
    },

    jumpRight: function () {
        state_direction = false;

        this.player.body.velocity.y = -200;
        this.player.body.velocity.x = 100;
        this.time.events.add(1900, this.my_time, this);
        this.player.animations.play('jump_right');
        //state_direction = true;
    },

    jump: function () {
        state_direction = false;

        this.player.body.velocity.y = -150;
        this.time.events.add(1900, this.my_time, this);
       //this.player.animations.play('jump_right');
    },

 

Link to comment
Share on other sites

Hello!

Can you confirm if the problem is so that your code never reaches inside the IFs with "jump" and "left/right" pressed? It could be a case of anti-ghosting with your keyboard. Does the behavior still occurs in another machine? I've attached a link of a similar scenario that you mentioned (there doesn't seem to be anything wrong with the code).

And, regardless, would it be acceptable for the player to change the jump direction midair? If so, you could change your logic to check those buttons separately, and work around the issue.

Oh, one more thing: if you are creating the "cursors" object, it already watches for both LEFT and RIGHT keys, so there would be no need to create the "left" and "right" variables, and just use them like so:

if (cursors.left.isDown) {
// do stuff 
}
else if (cursors.right.isDown) { 
// do other stuff 
}

(Also, are you defining the "jumpButton", "left" and "right" variables outside the functions? I noticed you are not using "this." for them.)

 

 

Link to comment
Share on other sites

 

15 hours ago, DanielKlava said:

Hello!

Can you confirm if the problem is so that your code never reaches inside the IFs with "jump" and "left/right" pressed? It could be a case of anti-ghosting with your keyboard. Does the behavior still occurs in another machine? I've attached a link of a similar scenario that you mentioned (there doesn't seem to be anything wrong with the code).

And, regardless, would it be acceptable for the player to change the jump direction midair? If so, you could change your logic to check those buttons separately, and work around the issue.

Oh, one more thing: if you are creating the "cursors" object, it already watches for both LEFT and RIGHT keys, so there would be no need to create the "left" and "right" variables, and just use them like so:


if (cursors.left.isDown) {
// do stuff 
}
else if (cursors.right.isDown) { 
// do other stuff 
}

(Also, are you defining the "jumpButton", "left" and "right" variables outside the functions? I noticed you are not using "this." for them.)

 

 

I just checked and my program does not go inside those two functions

Also for "jumpButton", "left" and "right" variables I declared in my main.js file.

I don't know if I'm having a keyboard ghosting problem coz I used WASD as the keys for moving but I had the same issue

Can you help me?

Thanks

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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