mirror of
https://github.com/tiennm99/sokoban.git
synced 2026-10-16 06:14:38 +00:00
fix: not response after change level
This commit is contained in:
1 parent
d0140de19a
commit
0fe4e7cbcf
2 files changed
+65
-55
No files matched your search
@@ -97,6 +97,11 @@ class LevelScene extends Phaser.Scene {
|
|||||||
buttonBg.on('pointerdown', () => {
|
buttonBg.on('pointerdown', () => {
|
||||||
// Update currentLevel in gameState
|
// Update currentLevel in gameState
|
||||||
this.game.registry.get('updateGameState')({ currentLevel: i });
|
this.game.registry.get('updateGameState')({ currentLevel: i });
|
||||||
|
|
||||||
|
// Stop and restart MainScene before starting it
|
||||||
|
if (this.scene.get('MainScene').scene.isActive()) {
|
||||||
|
this.scene.stop('MainScene');
|
||||||
|
}
|
||||||
this.scene.start('MainScene');
|
this.scene.start('MainScene');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,8 @@ class MainScene extends Phaser.Scene {
|
|||||||
|
|
||||||
// Controls
|
// Controls
|
||||||
this.cursors = null;
|
this.cursors = null;
|
||||||
this.keyState = {
|
this.lastKeyPressed = 0; // Add timestamp for key press tracking
|
||||||
up: false,
|
this.keyDelay = 150; // Minimum delay between key presses in milliseconds
|
||||||
down: false,
|
|
||||||
left: false,
|
|
||||||
right: false
|
|
||||||
};
|
|
||||||
|
|
||||||
// Level data
|
// Level data
|
||||||
this.levelData = null;
|
this.levelData = null;
|
||||||
@@ -31,6 +27,18 @@ class MainScene extends Phaser.Scene {
|
|||||||
this.tileSize = 64;
|
this.tileSize = 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
// Reset all state variables when scene initializes
|
||||||
|
this.player = null;
|
||||||
|
this.walls = null;
|
||||||
|
this.boxes = null;
|
||||||
|
this.targets = null;
|
||||||
|
this.cursors = null;
|
||||||
|
this.isMoving = false;
|
||||||
|
this.lastKeyPressed = 0;
|
||||||
|
this.levelData = null;
|
||||||
|
}
|
||||||
|
|
||||||
preload() {
|
preload() {
|
||||||
// Load level data
|
// Load level data
|
||||||
this.load.json('level1', 'assets/levels/level1.json');
|
this.load.json('level1', 'assets/levels/level1.json');
|
||||||
@@ -39,35 +47,9 @@ class MainScene extends Phaser.Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
// Set up controls
|
// Set up controls - using Phaser's built-in cursor keys
|
||||||
this.cursors = this.input.keyboard.createCursorKeys();
|
this.cursors = this.input.keyboard.createCursorKeys();
|
||||||
|
|
||||||
// Reset key state
|
|
||||||
this.keyState = {
|
|
||||||
up: false,
|
|
||||||
down: false,
|
|
||||||
left: false,
|
|
||||||
right: false
|
|
||||||
};
|
|
||||||
|
|
||||||
// Set up key release listeners
|
|
||||||
this.input.keyboard.on('keyup', (event) => {
|
|
||||||
switch (event.keyCode) {
|
|
||||||
case 37: // Left
|
|
||||||
this.keyState.left = false;
|
|
||||||
break;
|
|
||||||
case 38: // Up
|
|
||||||
this.keyState.up = false;
|
|
||||||
break;
|
|
||||||
case 39: // Right
|
|
||||||
this.keyState.right = false;
|
|
||||||
break;
|
|
||||||
case 40: // Down
|
|
||||||
this.keyState.down = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get game state from registry
|
// Get game state from registry
|
||||||
const gameState = this.game.registry.get('gameState');
|
const gameState = this.game.registry.get('gameState');
|
||||||
@@ -142,19 +124,23 @@ class MainScene extends Phaser.Scene {
|
|||||||
// Skip if player is already moving or player is not defined
|
// Skip if player is already moving or player is not defined
|
||||||
if (this.isMoving || !this.player) return;
|
if (this.isMoving || !this.player) return;
|
||||||
|
|
||||||
// Handle player movement with key state tracking
|
const currentTime = Date.now();
|
||||||
if (this.cursors.left.isDown && !this.keyState.left) {
|
|
||||||
this.keyState.left = true;
|
// Only process input if enough time has passed since last key press
|
||||||
this.movePlayer(-1, 0);
|
if (currentTime - this.lastKeyPressed >= this.keyDelay) {
|
||||||
} else if (this.cursors.right.isDown && !this.keyState.right) {
|
if (this.cursors.left.isDown) {
|
||||||
this.keyState.right = true;
|
this.movePlayer(-1, 0);
|
||||||
this.movePlayer(1, 0);
|
this.lastKeyPressed = currentTime;
|
||||||
} else if (this.cursors.up.isDown && !this.keyState.up) {
|
} else if (this.cursors.right.isDown) {
|
||||||
this.keyState.up = true;
|
this.movePlayer(1, 0);
|
||||||
this.movePlayer(0, -1);
|
this.lastKeyPressed = currentTime;
|
||||||
} else if (this.cursors.down.isDown && !this.keyState.down) {
|
} else if (this.cursors.up.isDown) {
|
||||||
this.keyState.down = true;
|
this.movePlayer(0, -1);
|
||||||
this.movePlayer(0, 1);
|
this.lastKeyPressed = currentTime;
|
||||||
|
} else if (this.cursors.down.isDown) {
|
||||||
|
this.movePlayer(0, 1);
|
||||||
|
this.lastKeyPressed = currentTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check win condition
|
// Check win condition
|
||||||
@@ -568,16 +554,35 @@ class MainScene extends Phaser.Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shutdown() {
|
shutdown() {
|
||||||
// Clean up event listeners
|
// Clean up physics
|
||||||
this.input.keyboard.off('keyup');
|
if (this.physics && this.physics.world) {
|
||||||
|
this.physics.world.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
// Reset key state
|
// Destroy all game objects
|
||||||
this.keyState = {
|
if (this.player) this.player.destroy();
|
||||||
up: false,
|
if (this.walls) this.walls.destroy(true);
|
||||||
down: false,
|
if (this.boxes) this.boxes.destroy(true);
|
||||||
left: false,
|
if (this.targets) this.targets.destroy(true);
|
||||||
right: false
|
|
||||||
};
|
// Reset all state variables
|
||||||
|
this.player = null;
|
||||||
|
this.walls = null;
|
||||||
|
this.boxes = null;
|
||||||
|
this.targets = null;
|
||||||
|
this.cursors = null;
|
||||||
|
this.isMoving = false;
|
||||||
|
this.lastKeyPressed = 0;
|
||||||
|
this.levelData = null;
|
||||||
|
|
||||||
|
// Remove all input listeners
|
||||||
|
this.input.keyboard.removeAllKeys(true);
|
||||||
|
this.input.keyboard.removeAllListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
this.shutdown();
|
||||||
|
super.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default MainScene;
|
export default MainScene;
|
||||||
Reference in new issue
Block a user