mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 04:31:29 +08:00
feat(control) 摇杆优化
This commit is contained in:
1 parent
344d61d0fb
commit
3863cfe1f6
3 files changed
+68
-13
No files matched your search
+25
-6
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user