fix(CATS) 修复角色动画与原点问题

This commit is contained in:
chicrobot committed 2026-02-03 13:46:41 +08:00
1 parent f6f7076fcb
commit 683803945a
2 files changed
+45 -24

No files matched your search

+15 -1
View File
@@ -5,6 +5,12 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
scene.add.existing(this);
scene.physics.add.existing(this);
// 设置角色原点为底部中点
this.setOrigin(0.5, 1);
// 设置角色深度,确保显示在指示器上方
this.setDepth(10);
// 启用圆形碰撞体(更自然)
this.setCircle(14);
@@ -32,7 +38,8 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
*/
createSelectedIndicator() {
// 创建一个外圈指示器,默认隐藏
this.selectedIndicator = this.scene.add.circle(this.x, this.y, this.width * 0.7, 0x00ff00, 0.0);
// 椭圆指示器:中心位于图片底边中心,宽度为角色宽度的1.4倍,高度为角色高度的0.3倍
this.selectedIndicator = this.scene.add.ellipse(this.x, this.y, this.width * 1.5, this.height * 0.5, 0x00ff00, 0.0);
this.selectedIndicator.setStrokeStyle(2, 0x00ff00, 1);
this.selectedIndicator.setVisible(false);
@@ -40,6 +47,8 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
this.selectedIndicator.setOrigin(0.5, 0.5);
// 移除固定位置设置,让指示器随单位和相机一起移动
this.selectedIndicator.setScrollFactor(1, 1);
// 设置较低的深度,确保角色显示在上方
this.selectedIndicator.setDepth(5);
}
/**
@@ -52,6 +61,8 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
this.targetIndicator.setOrigin(0.5, 0.5);
this.targetIndicator.setScrollFactor(1, 1);
this.targetIndicator.setVisible(false);
// 设置较低的深度,确保角色显示在上方
this.targetIndicator.setDepth(5);
}
}
@@ -63,6 +74,8 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
// 创建图形对象用于绘制路径
this.pathGraphics = this.scene.add.graphics();
this.pathGraphics.setScrollFactor(1, 1);
// 设置较低的深度,确保角色显示在上方
this.pathGraphics.setDepth(5);
}
}
@@ -189,6 +202,7 @@ export default class Unit extends Phaser.Physics.Arcade.Sprite {
update() {
// 更新选中指示器位置
if (this.selectedIndicator && this.isSelected) {
// 保持与创建时相同的位置偏移,确保指示器显示在角色底部
this.selectedIndicator.setPosition(this.x, this.y);
}
@@ -109,14 +109,8 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
this.anims.create({
key: 'player0-move',
frames: [
{ key: 'player0-0' },
{ key: 'player0-1' },
{ key: 'player0-2' },
{ key: 'player0-3' },
{ key: 'player0-4' },
{ key: 'player0-5' },
{ key: 'player0-6' },
{ key: 'player0-7' }
{ key: 'player0-2' }
],
frameRate: 10,
repeat: -1
@@ -126,14 +120,8 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
this.anims.create({
key: 'player1-move',
frames: [
{ key: 'player1-0' },
{ key: 'player1-1' },
{ key: 'player1-2' },
{ key: 'player1-3' },
{ key: 'player1-4' },
{ key: 'player1-5' },
{ key: 'player1-6' },
{ key: 'player1-7' }
{ key: 'player1-2' }
],
frameRate: 10,
repeat: -1
@@ -218,14 +206,29 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
* 切换角色
*/
switchCharacter() {
// 根据当前使用的纹理切换角色
if (this.player.anims.currentAnim && this.player.anims.currentAnim.key === 'player0-move') {
// 检查单位是否在移动
const isMoving = Math.abs(this.player.body.velocity.x) > 10 || Math.abs(this.player.body.velocity.y) > 10;
// 切换角色
if (this.currentCharacterText.text === '当前角色: Player0') {
// 切换到Player1
this.player.play('player1-move');
this.currentCharacterText.setText('当前角色: Player1');
if (isMoving) {
this.player.play('player1-move');
} else {
this.player.anims.stop();
this.player.setTexture('player1-0');
}
console.log('切换到Player1');
} else {
// 切换到Player0
this.player.play('player0-move');
this.currentCharacterText.setText('当前角色: Player0');
if (isMoving) {
this.player.play('player0-move');
} else {
this.player.anims.stop();
this.player.setTexture('player0-0');
}
console.log('切换到Player0');
}
}
@@ -307,9 +310,8 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
unit.setFlipX(false); // 向右不翻转
}
// 播放动画
if (!unit.anims.isPlaying) {
// 根据当前角色播放对应的移动动画
// 播放移动动画
if (!unit.anims.isPlaying || !unit.anims.currentAnim.key.includes('move')) {
if (this.currentCharacterText.text === '当前角色: Player0') {
unit.play('player0-move');
} else {
@@ -317,8 +319,13 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
}
}
} else {
// 停止动画,保持当前帧
unit.anims.pause();
// 静止状态,显示图片0
unit.anims.stop();
if (this.currentCharacterText.text === '当前角色: Player0') {
unit.setTexture('player0-0');
} else {
unit.setTexture('player1-0');
}
}
}
}