Jump to content

Arrow keys pressing up + left + spacebar issue


drunkenoodle
 Share

Recommended Posts

Hello everyone,
I've recently been having a mess around with one of the Phaser game examples (Invaders), with a mind to changing it in to a side-scrolling space shooter. I'm having a bit of an issue early on however in that when I press the left and up arrow keys, and then press space bar to fire, it doesn't seem to pick up the space bar press event.

 

I wondered if anyone else has had a similar issue, or, perhaps has a solution to it. Please see the source code below (input events are currently in the update method):

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-sidescroller', { preload: preload, create: create, update: update, render: render });function preload() {    game.load.image('bullet', 'assets/bullet.png');    game.load.image('enemyBullet', 'assets/enemy-bullet.png');    game.load.spritesheet('invader', 'assets/invader32x32x4.png', 32, 32);    game.load.image('ship', 'assets/player.png');    game.load.spritesheet('kaboom', 'assets/explode.png', 128, 128);    game.load.image('starfield', 'assets/starfield.png');    game.load.image('background', 'assets/background2.png');}var player;var aliens;var bulletsBeta;var bulletTime = 0;var playerSpeed = 3;var cursors;var fireButton;var explosions;var starfield;var score = 0;var scoreString = '';var scoreText;var lives;var enemyBullet;var firingTimer = 0;var stateText;var livingEnemies = [];function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    //  The scrolling starfield background    starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');    //  Our bullet group(s)    bulletsBeta = game.add.group();    bulletsBeta.enableBody = true;    bulletsBeta.physicsBodyType = Phaser.Physics.ARCADE;    bulletsBeta.createMultiple(30, 'bullet');    bulletsBeta.setAll('anchor.x', 0.5);    bulletsBeta.setAll('anchor.y', 1);    bulletsBeta.setAll('outOfBoundsKill', true);    bulletsBeta.setAll('checkWorldBounds', true);    //  The hero!    player = game.add.sprite(100, 300, 'ship');    player.anchor.setTo(0.5, 0.5);    game.physics.enable(player, Phaser.Physics.ARCADE);    //  And some controls to play the game with    cursors = game.input.keyboard.createCursorKeys();    fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    game.input.keyboard.addKeyCapture([        Phaser.Keyboard.LEFT,        Phaser.Keyboard.RIGHT,        Phaser.Keyboard.UP,        Phaser.Keyboard.DOWN,        Phaser.Keyboard.SPACEBAR    ]);}function render() {}function update() {    //  Scroll the background - 1.1? Change later.    starfield.tilePosition.x -= 1.1;    // Changed from velocity to simple coord increment    if (cursors.up.isDown)    {        player.body.y -= 1.1 * playerSpeed;    } else if (cursors.down.isDown)    {        player.body.y += 1.1 * playerSpeed;    }    if (cursors.left.isDown)    {        player.body.x -= 1.1 * playerSpeed;    } else if (cursors.right.isDown)    {        player.body.x += 1.1 * playerSpeed;    }    //  Firing? - Something wrong with this and up-left    if (fireButton.isDown)    {        fireBullet();    }}// Handle player fire bullet - Not very optmizedvar bullet;function fireBullet () {    //  To avoid them being allowed to fire too fast we set a time limit    if (game.time.now > bulletTime)    {        //  Grab the first bullet we can from the pool        bullet = bulletsBeta.getFirstExists(false);        if (bullet)        {            //  And fire it            bullet.reset(player.x, player.y + 12);            bullet.body.velocity.x = 800;            bulletTime = game.time.now + 100;        }    }}

Any help would be massively appreciated.

Thanks!

Link to comment
Share on other sites

  • 1 month later...

Hi Rich,

I'm really sorry I forgot to say thanks for replying to this post, actually carried on my merry way without coming back to check for a reply! Cheers for the advice, having great fun working with Phaser, especially now things are making more sense.

 

Thanks again!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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