Files
CINC/src/scenes/GameScene.js
T
2026-02-04 21:48:37 +08:00

75 lines
2.4 KiB
JavaScript

// scenes/GameScene.js
export class GameScene extends Phaser.Scene {
constructor() {
super('GameScene');
this.moveSpeed = 200;
this.joystickId = null;
this.inputPlugin = null;
}
preload() {
}
create() {
const graphics = this.make.graphics();
graphics.fillStyle(0xffffff, 1);
graphics.fillRect(0, 0, 32, 32);
graphics.generateTexture('player', 32, 32);
graphics.destroy();
this.player = this.physics.add.sprite(400, 300, 'player');
this.player.setCollideWorldBounds(true);
// 获取插件实例(更可靠的方式)
this.inputPlugin = this.plugins.get('InputPlugin');
if (this.inputPlugin) {
// 添加触摸轮盘
this.joystickId = this.inputPlugin.createJoystick({
id: 'movement',
x: 100,
y: this.sys.game.config.height - 100,
radius: 60,
maxDistance: 40
});
} else {
console.error('InputPlugin 未找到!');
}
// 添加提示文字
this.add.text(10, 10, '使用 WASD/方向键 或 触摸轮盘移动', { fill: '#ffffff', fontSize: '16px' });
}
update() {
// 重置速度
this.player.setVelocity(0);
// 键盘输入
const cursors = this.input.keyboard.createCursorKeys();
const keys = this.input.keyboard.addKeys('W,A,S,D');
if (cursors.up.isDown || keys.W.isDown) {
this.player.setVelocityY(-this.moveSpeed);
}
if (cursors.down.isDown || keys.S.isDown) {
this.player.setVelocityY(this.moveSpeed);
}
if (cursors.left.isDown || keys.A.isDown) {
this.player.setVelocityX(-this.moveSpeed);
}
if (cursors.right.isDown || keys.D.isDown) {
this.player.setVelocityX(this.moveSpeed);
}
// 触摸轮盘输入(带归一化)
if (this.inputPlugin && this.joystickId) {
const joystickNormalized = this.inputPlugin.getJoystickNormalized(this.joystickId);
if (joystickNormalized.x !== 0 || joystickNormalized.y !== 0) {
this.player.setVelocityX(joystickNormalized.x * this.moveSpeed);
this.player.setVelocityY(joystickNormalized.y * this.moveSpeed);
}
}
}
}