feat(control) 摇杆优化

This commit is contained in:
chicrobot committed 2025-12-31 00:48:54 +08:00
1 parent 344d61d0fb
commit 3863cfe1f6
3 files changed
+68 -13

No files matched your search

+25 -6
View File
@@ -11,15 +11,19 @@ export class VirtualJoystick {
this.radius = 50;
// 仅在触屏设备显示
if ('ontouchstart' in window) {
this.create();
this.bindEvents();
}
// if ('ontouchstart' in window) {
// this.create();
// this.bindEvents();
// }
// 在所有设备上都显示摇杆
this.create();
this.bindEvents();
}
create() {
const x = 100;
const y = this.scene.scale.height - 100;
const x = 200;
const y = this.scene.scale.height - 200;
this.centerX = x;
this.centerY = y;
@@ -75,4 +79,19 @@ export class VirtualJoystick {
const dy = pointer.y - this.centerY;
return Math.sqrt(dx * dx + dy * dy) <= this.radius + 20; // 容差
}
/**
* 更新方法,用于在游戏循环中持续发送移动指令
*/
update() {
if (this.isActive && this.pointer) {
const dx = this.pointer.x - this.centerX;
const dy = this.pointer.y - this.centerY;
// 发送相对方向(归一化)
const dirX = dx / this.radius;
const dirY = dy / this.radius;
this.scene.events.emit('move-relative', { x: dirX, y: dirY });
}
}
}