Jump to content

Two Keys at Once (e.g. Move & Fire)


LokisHood
 Share

Recommended Posts

Apologies if this is screamingly obvious but I've been Googling and searching this forum to no avail. This is only my second basic game so I've not much experience either.

 

I have a character who walks about (left & right animations) and I want him to cast spells as he walks (another 2 animations).

 

What I want to happen:

1. Hold left cursor key down.

2. Press C while left cursor key STILL held down.

3. Character immediately stops moving does "cast-left" animation.

4. Immediately continues walking left as you're still holding the button down.

 

Tried So Far (but not worked):

1 - Using the hotkey method from the examples where you add an event handler to a key.

2 - Using a if (left key down && C key down){ do cast animation }

 

I can get it to work if I stop pressing left to cast a spell but no one does that. Its always "move & fire" to use a common phrase. I can never get more than 1 key to register at the same time. Is this even possible or am I barking up the wrong tree?

 

Hope someone can help.

Link to comment
Share on other sites

The specific source where keys are defined are availble as part of game.js on my Github here: https://github.com/OdinsHat/drac/blob/master/js/game.js

 

The important bits where I try using the hotkey method is here:

this.key_c = this.game.input.keyboard.addKey(Phaser.Keyboard.C);this.key_c.onDown.add(this.castSpell, this);

Then the traditional if else method:

        if (this.cursors.left.isDown){            this.player.body.velocity.x = -100;            this.player.animations.play('left');        }else if(this.cursors.right.isDown){            this.player.body.velocity.x = 100;            this.player.animations.play('right');        }else if(this.cursors.up.isDown && this.cursors.left.isDown){            this.player.animations.play('cast-large-right');        }else if(this.key_c.isDown){            this.player.animations.play('cast-large-left');        }else{            this.player.animations.stop();            this.player.frame = 78;        }

This is my animation initialisation:

this.player.animations.add('left', [117, 118, 119, 120, 121, 122, 123, 124, 125], 10, true); // Row Jthis.player.animations.add('right', [143, 144, 145, 146, 147, 148, 149, 150, 151], 10, true); // Row Lthis.player.animations.add('cast-small-left', [65, 66, 67, 68, 69, 70, 71, 72], 10, false); // Row Fthis.player.animations.add('cast-small-right', [91, 92, 93, 94, 95, 96, 97, 98], 10, false); // Row Hthis.player.animations.add('cast-large-left', [221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233], 10, false); // Row Rthis.player.animations.add('cast-large-right', [247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259], 10, false); // Row T0

If I stop walking I can press "c" and the character will animate casting the spell. But I can't hold a key and press C. You'll also notice I've tried adding "UP" cursor key as a casting method but the same problem exists there. So I don't think its down to key combinations.

 

Apologies for the messy code - I'm experimenting a LOT at the moment.

 

Left + C - just walks

Left + Up - just walks

 

Left -stop- then C - Walks -stops- then casts.

 

I want to use two keys at once to move and fire.

Link to comment
Share on other sites

 if (this.cursors.left.isDown){

            this.player.body.velocity.x = -100;

            this.player.animations.play('left');

        }else if(this.cursors.right.isDown){

            this.player.body.velocity.x = 100;

            this.player.animations.play('right');

        }else if(this.cursors.up.isDown && this.cursors.left.isDown){

            this.player.animations.play('cast-large-right');

        }else if(this.key_c.isDown){

            this.player.animations.play('cast-large-left');

        }else{

            this.player.animations.stop();

            this.player.frame = 78;

        }

 

With that code if the left key is down the rest doesnt get checked including.....

}else if(this.cursors.up.isDown && this.cursors.left.isDown){

...and.....

}else if(this.key_c.isDown){

 ...."else" is your problem

 

EDIT : I think you want something like this.....



if (this.cursors.left.isDown) {
  this.player.body.velocity.x = -100;
  this.player.animations.play('left');
} else if (this.cursors.right.isDown) {
  this.player.body.velocity.x = 100;
  this.player.animations.play('right');
} else {
  this.player.animations.stop();
  this.player.frame = 78;
}


if (this.key_c.isDown) {
  if (this.cursors.right.isDown) this.player.animations.play('cast-large-right');
  else if (this.cursors.left.isDown) this.player.animations.play('cast-large-left');
// else shoot in the direction their facing
}

Link to comment
Share on other sites

I've been programming for 15yrs and I can't believe I made that basic logical error!! Of course the first if stops any others! I feel like SUCH an idiot.

 

Thank you for the help - also the extra info on checking the direction of the player before doing the animation. That was my next problem.

 

Very much appreciated. Thank you for your patience. :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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