Jump to content

Problem with keyboard events


Manthyne
 Share

Recommended Posts

Hello everyone,

I'm new to Phaser.

I'm trying to code a small game for my friend's birthday next month.

The problem is, my cursor keys don't seem to work correctly and I can't figure out why. I've downloaded the 'making your first phaser game' archive and launched it: the problem is already there.

When I press an arrow, my player moves in the right direction. But when I release the key, the player never stops walking. I made a console.log() and the game considers that the key is never released. 

I tried to set a X velocity of 0 on keyup, then I tried to set the velocity to 0 after a few seconds with a timer, but the problem is still there. My player never stops.

Can someone please help me ? Thank you in advance.

Here is my code :


let gameScene = new Phaser.Scene('level01');

let config = {
  type: Phaser.AUTO,
  width: 1280,
  height: 720,
  physics: {
      default: 'arcade',
      arcade: {
          gravity: { y: 300 },
          debug: false
      }
  },
  scene: {
      preload: preload,
      create: create,
      update: update
  }
};

var player;
var cursors;
var platforms;

var game = new Phaser.Game(config);

function preload ()
{
  this.load.image('roomFloor', 'assets/level01/roomFloor.png');
  this.load.spritesheet('pepon', 'anims/5frames.png', { frameWidth: 139, frameHeight: 118 } );
}

function create ()
{
  platforms = this.physics.add.staticGroup();
  platforms.create(0, 700, 'roomFloor').setOrigin(0, 0);

  player = this.physics.add.sprite(100, 450, 'pepon');
  player.setBounce(0.2);
  player.setCollideWorldBounds(true);

  this.physics.add.collider(player, platforms);

  // Animations
  this.anims.create({
    key: 'right',
    frames: this.anims.generateFrameNumbers('pepon', { start: 0, end: 1 }),
    frameRate: 5,
    repeat: -1
  });
  this.anims.create({
    key: 'left',
    frames: this.anims.generateFrameNumbers('pepon', { start: 3, end: 4 }),
    frameRate: 5,
    repeat: -1
  });
  this.anims.create({
    key: 'staticLeft',
    frames: [ { key: 'pepon', frame: 2 } ],
    frameRate: 20
  });
  this.anims.create({
    key: 'staticRight',
    frames: [ { key: 'pepon', frame: 3 } ],
    frameRate: 20
  });

  cursors = this.input.keyboard.createCursorKeys();
}

  function update ()
  {
    if (cursors.left.isDown)
    {
        player.setVelocityX(-160);

        player.anims.play('left', true);
    }
    else if (cursors.right.isDown)
    {
        player.setVelocityX(160);

        player.anims.play('right', true);
    }
    else
    {
        player.setVelocityX(0);

        player.anims.play('staticLeft');
    }

    if (cursors.up.isDown && player.body.touching.down)
    {
        player.setVelocityY(-330);
    }

}

Edit : I tested it on my brother's computer and it works correctly. I don't understand what is happening. I tested it with another keyboard and I encounter the same problem. 

I made a demo here : http://audrade.free.fr/phaser3/

Edit2 : it seems to be coming from my browser. I used IE and it works fine...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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