Keyboard combination event

Hey!

I’m new to Phaser and i’m making a game where the enemies, one at the time, are being spawned random at one of five y-positions(the y, positions are kept in an array [100, 200, 300, 400, 500]).

I’ve decided that the enemies can only be killed with the right combination of keys pressed at the same time on the keyboard. And i want the “right combination” to be different for each y-position.

Examle: If the Enemy are spawned at y:100, you can only kill it with ‘A’ and ‘S’,
If the Enemy are spawned at y:200, you can only kill it with ‘A’ and ‘D’, and so on.

Can anyone give me advice on how to keep track of the enemies positions and killing them with the right keycombo?

Do you want to kill the enemy if the keys are pressed at the same time, or one key after another in the correct order?


Edit:
Did not see this before :blush:

Maybe you could put the key combination into the array with the Y-position as an object and in the update function you would check if the key combo matches with the enemy visible (use setVisible()).

let array = [
 {
  y: 100,
  keyCombo: ["LEFT", "A", "F"]
 },
 {
  ...
 }
];

I had fun playing around. This is the result

export default class MainScene extends Phaser.Scene {
  group
  keys = []

  constructor() {
    super({ key: 'MainScene' })
  }

  create() {
    const alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'] // ... and so on
    const y = [100, 200, 300, 400, 500]
    const x = [100, 200, 300, 400, 500]

    const createEnemy = () => {
      let enemy = this.add.sprite(Phaser.Math.RND.pick(x), Phaser.Math.RND.pick(y), 'enemy')

      let fistLetter = Phaser.Math.RND.pick(alphabet)
      let secondAlphabet = [...alphabet]

      let index = secondAlphabet.indexOf(fistLetter)
      if (index > -1) secondAlphabet.splice(index, 1)
      let secondLetter = Phaser.Math.RND.pick(secondAlphabet)

      enemy.customParams = {
        combination: [fistLetter, secondLetter]
      }

      console.log(`Enemy: ${enemy.customParams.combination}`)
      this.group.add(enemy)
    }

    // create the enemies group
    this.group = this.add.group()

    // create the first enemy
    createEnemy()

    this.time.addEvent({
      delay: 5000,
      callback: () => createEnemy(),
      loop: true
    })

    // create all key inputs
    alphabet.forEach(letter => {
      const key = this.input.keyboard.addKey(letter)
      this.keys.push({ input: key, letter: letter })
    })
  }

  update() {
    // check which keys are down
    const keysDown = this.keys
      .filter(key => key.input.isDown)
      .map(key => key.letter)
      .sort()
      .toString()

    if (keysDown.length > 0) {
      //console.log(`KeysDown: ${keysDown}`)

      this.group.children.each(enemy => {
        const combination = enemy.customParams.combination.sort().toString()

        // destroy the enemy if the keys matches the enemy combination
        if (keysDown === combination) enemy.destroy()
      })
    }
  }
}

1 Like

at the same time

Wow! Thank you so much! I will try that :slight_smile: