Skip to content

Commit a89bb41

Browse files
committed
better menu controls
1 parent 0f4722b commit a89bb41

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

src/game/scenes/HighScoresScene.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ export class HighScoresScene extends Phaser.Scene {
3636
.setOrigin(0.5);
3737
});
3838

39-
this.input.keyboard?.on("keydown-W", this.scrollUp, this);
40-
this.input.keyboard?.on("keydown-S", this.scrollDown, this);
41-
42-
this.input.keyboard?.once("keydown-SPACE", () => {
43-
this.scene.start("MenuScene");
44-
});
39+
this.input.keyboard?.on(
40+
"keydown",
41+
(event: KeyboardEvent) => {
42+
if ([" ", "Enter"].includes(event.key)) {
43+
this.scene.start("MenuScene");
44+
} else if (["w", "ArrowUp"].includes(event.key)) {
45+
this.scrollUp();
46+
} else if (["s", "ArrowDown"].includes(event.key)) {
47+
this.scrollDown();
48+
}
49+
},
50+
this
51+
);
4552
}
4653

4754
renderScores() {

src/game/scenes/MenuScene.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,20 @@ export class MenuScene extends Phaser.Scene {
7171
.setOrigin(0.5, 0.75);
7272
});
7373

74-
this.input.keyboard?.on("keydown-W", () => {
75-
this.selectedIndex =
76-
(this.selectedIndex + this.options.length - 1) % this.options.length;
74+
this.input.keyboard?.on("keydown", (event: KeyboardEvent) => {
75+
console.log(event.key);
76+
if ([" ", "Enter"].includes(event.key)) {
77+
this.selectOption();
78+
}
79+
if (["w", "ArrowUp"].includes(event.key)) {
80+
this.selectedIndex =
81+
(this.selectedIndex + this.options.length - 1) % this.options.length;
82+
} else if (["s", "ArrowDown"].includes(event.key)) {
83+
this.selectedIndex =
84+
(this.selectedIndex + this.options.length + 1) % this.options.length;
85+
}
7786
this.updateCursor();
7887
});
79-
80-
this.input.keyboard?.on("keydown-S", () => {
81-
this.selectedIndex =
82-
(this.selectedIndex + this.options.length + 1) % this.options.length;
83-
this.updateCursor();
84-
});
85-
86-
this.input.keyboard?.on("keydown-SPACE", () => {
87-
this.selectOption();
88-
});
8988
}
9089

9190
updateCursor() {

src/game/scenes/NameEntryScene.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export class NameEntryScene extends Phaser.Scene {
5454
this.enteredName += key.toUpperCase();
5555

5656
this.updateNameDisplay();
57-
} else if (event.key === "Backspace" && this.enteredName.length > 0) {
57+
} else if (["Backspace"].includes(key) && this.enteredName.length > 0) {
5858
this.enteredName = this.enteredName.slice(0, -1);
5959
this.updateNameDisplay();
60-
} else if (event.key === " ") {
60+
} else if ([" ", "Enter"].includes(key)) {
6161
this.saveScore();
6262
this.scene.start("MenuScene");
6363
}
@@ -79,8 +79,10 @@ export class NameEntryScene extends Phaser.Scene {
7979
const stored = localStorage.getItem("switchy-hiscores");
8080
const scores = stored ? JSON.parse(stored) : [];
8181

82-
scores.push(newEntry);
83-
scores.sort((a: any, b: any) => b.score - a.score);
82+
if (newEntry.name.length > 0) {
83+
scores.push(newEntry);
84+
scores.sort((a: any, b: any) => b.score - a.score);
85+
}
8486

8587
localStorage.setItem("switchy-hiscores", JSON.stringify(scores));
8688
}

src/game/scenes/ScenarioScene.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export class ScenarioScene extends Phaser.Scene {
6060
.setWordWrapWidth(width);
6161
});
6262

63-
this.input.keyboard?.on("keydown-SPACE", () => {
64-
this.scene.start("GameScene", { device: this.device });
63+
this.input.keyboard?.on("keydown", (event: KeyboardEvent) => {
64+
if ([" ", "Enter"].includes(event.key)) {
65+
this.scene.start("GameScene", { device: this.device });
66+
}
6567
});
6668

6769
this.events.on("shutdown", this.cleanup, this);

0 commit comments

Comments
 (0)