fix(VJ) 修复只能操控一个轮盘的问题

This commit is contained in:
wvellichor committed 2026-01-18 02:37:29 +08:00
1 parent 8b801b8df0
commit 2b155e41bc
2 files changed
+56 -26

No files matched your search

+28 -13
View File
@@ -163,7 +163,7 @@ export default class SkillTestScene extends Phaser.Scene {
isActive: false, isActive: false,
base: null, base: null,
knob: null, knob: null,
pointer: null, activePointer: null,
centerX: 0, centerX: 0,
centerY: 0, centerY: 0,
radius: 50, radius: 50,
@@ -345,14 +345,18 @@ export default class SkillTestScene extends Phaser.Scene {
// 添加点击事件 // 添加点击事件
button.setInteractive({ useHandCursor: true }); button.setInteractive({ useHandCursor: true });
button.on('pointerdown', (pointer) => { button.on('pointerdown', (pointer) => {
// 选中技能后,将轮盘中心设置为技能按钮的中心位置 // 只有在技能轮盘未激活时才开始技能施法
this.skillWheel.centerX = x; // 这样可以避免与移动轮盘的指针冲突
this.skillWheel.centerY = y; if (!this.skillWheel.isActive) {
this.skillWheel.base.setPosition(x, y); // 选中技能后,将轮盘中心设置为技能按钮的中心位置
this.skillWheel.base.setVisible(true); this.skillWheel.centerX = x;
this.skillWheel.centerY = y;
this.skillWheel.activeSkill = skillIndex; this.skillWheel.base.setPosition(x, y);
this.startSkillCast(pointer); this.skillWheel.base.setVisible(true);
this.skillWheel.activeSkill = skillIndex;
this.startSkillCast(pointer);
}
}); });
this.skillWheel.skillButtons.push(button); this.skillWheel.skillButtons.push(button);
@@ -372,7 +376,7 @@ export default class SkillTestScene extends Phaser.Scene {
// 鼠标移动事件 // 鼠标移动事件
this.input.on('pointermove', (ptr) => { this.input.on('pointermove', (ptr) => {
// 只有在技能轮盘激活时才处理 // 只有在技能轮盘激活时才处理
if (!this.skillWheel.isActive || this.skillWheel.pointer !== ptr) { if (!this.skillWheel.isActive || this.skillWheel.activePointer !== ptr) {
return; return;
} }
@@ -395,7 +399,18 @@ export default class SkillTestScene extends Phaser.Scene {
// 鼠标释放事件 // 鼠标释放事件
this.input.on('pointerup', (ptr) => { this.input.on('pointerup', (ptr) => {
// 只有在技能轮盘激活时才处理 // 只有在技能轮盘激活时才处理
if (!this.skillWheel.isActive || this.skillWheel.pointer !== ptr) { if (!this.skillWheel.isActive || this.skillWheel.activePointer !== ptr) {
return;
}
// 完成技能释放
this.finishSkillCast();
});
// 处理指针离开屏幕的情况
this.input.on('pointerout', (ptr) => {
// 只有在技能轮盘激活时才处理
if (!this.skillWheel.isActive || this.skillWheel.activePointer !== ptr) {
return; return;
} }
@@ -410,7 +425,7 @@ export default class SkillTestScene extends Phaser.Scene {
startSkillCast(pointer) { startSkillCast(pointer) {
// 设置技能轮盘状态 // 设置技能轮盘状态
this.skillWheel.isActive = true; this.skillWheel.isActive = true;
this.skillWheel.pointer = pointer; this.skillWheel.activePointer = pointer;
// 显示旋钮和轮盘基础 // 显示旋钮和轮盘基础
this.skillWheel.knob.setVisible(true); this.skillWheel.knob.setVisible(true);
@@ -439,7 +454,7 @@ export default class SkillTestScene extends Phaser.Scene {
// 重置技能轮盘状态 // 重置技能轮盘状态
this.skillWheel.isActive = false; this.skillWheel.isActive = false;
this.skillWheel.pointer = null; this.skillWheel.activePointer = null;
this.skillWheel.activeSkill = null; this.skillWheel.activeSkill = null;
// 隐藏旋钮和轮盘基础 // 隐藏旋钮和轮盘基础
+28 -13
View File
@@ -5,7 +5,7 @@ export class VirtualJoystick {
this.isActive = false; this.isActive = false;
this.base = null; this.base = null;
this.knob = null; this.knob = null;
this.pointer = null; this.activePointer = null; // 保持单一激活指针,但允许不同指针触发
this.centerX = 0; this.centerX = 0;
this.centerY = 0; this.centerY = 0;
this.radius = 50; this.radius = 50;
@@ -35,13 +35,17 @@ export class VirtualJoystick {
bindEvents() { bindEvents() {
this.scene.input.on('pointerdown', (ptr) => { this.scene.input.on('pointerdown', (ptr) => {
if (this.isInBase(ptr)) { if (this.isInBase(ptr)) {
this.pointer = ptr; // 只有在没有激活指针时才设置新指针
this.isActive = true; // 这样可以避免不同触摸点之间的干扰
if (!this.isActive) {
this.activePointer = ptr;
this.isActive = true;
}
} }
}); });
this.scene.input.on('pointermove', (ptr) => { this.scene.input.on('pointermove', (ptr) => {
if (this.isActive && this.pointer === ptr) { if (this.isActive && this.activePointer === ptr) {
const dx = ptr.x - this.centerX; const dx = ptr.x - this.centerX;
const dy = ptr.y - this.centerY; const dy = ptr.y - this.centerY;
const distance = Math.sqrt(dx * dx + dy * dy); const distance = Math.sqrt(dx * dx + dy * dy);
@@ -49,12 +53,13 @@ export class VirtualJoystick {
if (distance > this.radius) { if (distance > this.radius) {
// 限制在圆内 // 限制在圆内
const angle = Math.atan2(dy, dx); const angle = Math.atan2(dy, dx);
ptr.x = this.centerX + Math.cos(angle) * this.radius; const knobX = this.centerX + Math.cos(angle) * this.radius;
ptr.y = this.centerY + Math.sin(angle) * this.radius; const knobY = this.centerY + Math.sin(angle) * this.radius;
this.knob.setPosition(knobX, knobY);
} else {
this.knob.setPosition(ptr.x, ptr.y);
} }
this.knob.setPosition(ptr.x, ptr.y);
// 发送相对方向(归一化) // 发送相对方向(归一化)
const dirX = dx / this.radius; const dirX = dx / this.radius;
const dirY = dy / this.radius; const dirY = dy / this.radius;
@@ -63,9 +68,19 @@ export class VirtualJoystick {
}); });
this.scene.input.on('pointerup', (ptr) => { this.scene.input.on('pointerup', (ptr) => {
if (this.pointer === ptr) { if (this.activePointer === ptr) {
this.isActive = false; this.isActive = false;
this.pointer = null; this.activePointer = null;
this.knob.setPosition(this.centerX, this.centerY);
this.scene.events.emit('move-relative', { x: 0, y: 0 }); // 停止
}
});
// 处理指针离开屏幕的情况
this.scene.input.on('pointerout', (ptr) => {
if (this.activePointer === ptr) {
this.isActive = false;
this.activePointer = null;
this.knob.setPosition(this.centerX, this.centerY); this.knob.setPosition(this.centerX, this.centerY);
this.scene.events.emit('move-relative', { x: 0, y: 0 }); // 停止 this.scene.events.emit('move-relative', { x: 0, y: 0 }); // 停止
} }
@@ -83,9 +98,9 @@ export class VirtualJoystick {
*/ */
update() { update() {
// 仅在启用时更新 // 仅在启用时更新
if (this.enabled && this.isActive && this.pointer) { if (this.enabled && this.isActive && this.activePointer) {
const dx = this.pointer.x - this.centerX; const dx = this.activePointer.x - this.centerX;
const dy = this.pointer.y - this.centerY; const dy = this.activePointer.y - this.centerY;
// 发送相对方向(归一化) // 发送相对方向(归一化)
const dirX = dx / this.radius; const dirX = dx / this.radius;