mcpField
P
phaser-gameobj

phaser-gameobj

github.com/yakoub-ai开发29192 次安装9 周前更新
代码仓库

关于 phaser-gameobj

这是面向 Phaser 4 游戏开发的 Claude Code 插件,提供游戏对象 API 参考与常见实现模式。它帮助开发者按用途选择对象,并了解 Phaser 4 与旧版本的差异。

主要功能

  1. 01说明常见游戏对象类型
  2. 02展示 Arcade Physics 对象创建方式
  3. 03提供交互对象与动画模式
  4. 04介绍深度、相机与视差设置
  5. 05记录 Phaser 4 API 变更

适用场景

  1. 01为场景选择合适的游戏对象
  2. 02创建 HUD、按钮与粒子效果
  3. 03将 Phaser 3 代码模式迁移到 Phaser 4

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/yakoub-ai/phaser4-gamedev --skill phaser-gameobj

安装后在 Claude Code 中输入/phaser-gameobj确认已加载。

详细介绍

Phaser 4 游戏对象

为每种使用场景选择合适的游戏对象类型。所有对象都通过 this.add.*(仅视觉效果)或 this.physics.add.*(带有 Arcade Physics 物理体)创建。

对象类型参考

Sprite — 可动画、可交互,且可添加物理效果
// Static display sprite (no physics)
const sprite = this.add.sprite(x, y, 'textureKey');
const sprite = this.add.sprite(x, y, 'atlas', 'frameName.png');  // atlas frame

// Physics sprite (dynamic body, affected by gravity/velocity)
const sprite = this.physics.add.sprite(x, y, 'textureKey');

// Configure physics body
(sprite.body as Phaser.Physics.Arcade.Body)
  .setSize(24, 32)           // hitbox size (smaller than visual for player)
  .setOffset(4, 8)           // offset from sprite origin
  .setMaxVelocity(300, 600); // cap velocity

// Common sprite settings
sprite.setOrigin(0.5, 0.5);  // pivot point (default: 0.5, 0.5 = center)
sprite.setScale(2);           // scale up
sprite.setAlpha(0.8);         // transparency
sprite.setDepth(10);          // render order (higher = on top)
sprite.setFlipX(true);        // mirror horizontally
sprite.setTint(0xff0000);     // tint red
sprite.clearTint();
Image — 静态视觉元素,不支持动画
const img = this.add.image(x, y, 'key');
const img = this.add.image(x, y, 'atlas', 'frame.png');

// Useful for: backgrounds, UI panels, static decorations
// Lighter than Sprite (no animation overhead)
img.setScrollFactor(0);  // Fixed to camera (for UI)
img.setScrollFactor(0.5); // Parallax at half speed
Text — 动态文本显示
const text = this.add.text(x, y, 'Score: 0', {
  fontSize: '24px',
  fontFamily: 'Arial, sans-serif',
  color: '#ffffff',
  stroke: '#000000',
  strokeThickness: 4,
  align: 'center',
  backgroundColor: '#333333',
  padding: { x: 8, y: 4 },
  wordWrap: { width: 400, useAdvancedWrap: true },
});

text.setText('Score: 100');  // update text
text.setScrollFactor(0);     // fixed to camera (HUD)

BitmapText — 创建大量文本对象时速度更快(以纹理形式渲染):

// Must load bitmap font first in preload:
this.load.bitmapFont('arcade', 'assets/fonts/arcade.png', 'assets/fonts/arcade.xml');

const bmpText = this.add.bitmapText(x, y, 'arcade', 'Score: 0', 32);
bmpText.setText('Score: 100');
Graphics — 以编程方式绘制形状
const gfx = this.add.graphics();

// Filled shapes
gfx.fillStyle(0xff0000, 1.0);          // color, alpha
gfx.fillRect(x, y, width, height);
gfx.fillCircle(x, y, radius);
gfx.fillTriangle(x1,y1, x2,y2, x3,y3);

// Stroked shapes
gfx.lineStyle(2, 0xffffff, 1.0);       // lineWidth, color, alpha
gfx.strokeRect(x, y, width, height);
gfx.strokeCircle(x, y, radius);

// Paths
gfx.beginPath();
gfx.moveTo(x1, y1);
gfx.lineTo(x2, y2);
gfx.closePath();
gfx.strokePath();

gfx.clear();  // erase everything (call each frame if updating)
Container — 将对象分组,以便相对定位
// Create a health bar from multiple objects
const healthBar = this.add.container(x, y);
const bg = this.add.graphics().fillStyle(0x333333).fillRect(0, 0, 100, 12);
const bar = this.add.graphics().fillStyle(0x00ff00).fillRect(0, 0, 100, 12);
const label = this.add.text(50, -14, 'HP', { fontSize: '10px' }).setOrigin(0.5);
healthBar.add([bg, bar, label]);

// All children move/rotate/scale with container
healthBar.setPosition(200, 50);
healthBar.setDepth(10);
// Note: Physics does NOT work on containers — physics lives on individual sprites
Group — 管理对象集合
// Static group (pool of objects created manually)
const stars = this.add.group();
for (let i = 0; i < 20; i++) {
  stars.add(this.add.sprite(
    Phaser.Math.Between(0, 800),
    Phaser.Math.Between(0, 600),
    'star'
  ));
}

// Physics group with automatic pooling
const bullets = this.physics.add.group({
  classType: Bullet,     // custom class extending Arcade.Sprite
  maxSize: 30,
  runChildUpdate: true,  // calls update() on active members
});

// Get a member from the pool
const bullet = bullets.get(x, y) as Bullet;
if (bullet) {
  bullet.setActive(true).setVisible(true);
  bullet.setVelocityY(-400);
}

// Static group (for tilemap-like platforms)
const platforms = this.physics.add.staticGroup();
platforms.create(400, 580, 'platform');
platforms.create(100, 400, 'platform');
Particles — 视觉特效
// Phaser 4 particles API (same pattern as late v3)
const emitter = this.add.particles(0, 0, 'star', {
  speed: { min: 50, max: 150 },
  scale: { start: 0.5, end: 0 },
  blendMode: 'ADD',
  lifespan: 800,
  quantity: 5,
  frequency: 100,         // emit every 100ms
  maxParticles: 50,       // cap total particles (performance!)
  gravityY: 200,
  rotate: { min: 0, max: 360 },
});

// Follow a sprite
emitter.setPosition(player.x, player.y);

// One-shot explosion burst
this.add.particles(x, y, 'spark', {
  speed: { min: 100, max: 300 },
  lifespan: 600,
  quantity: 20,
  maxParticles: 20,
  scale: { start: 1, end: 0 },
  blendMode: 'ADD',
});
TileSprite — 可滚动/重复的纹理
const bg = this.add.tileSprite(0, 0, 800, 600, 'bg-tiles');
bg.setOrigin(0, 0);

// Scroll in update():
bg.tilePositionX += 1;  // scroll right
bg.tilePositionY -= 0.5;

// NOTE: TileSprite does NOT support texture cropping in Phaser 4 (removed from v3)

深度与摄像机

// Depth (z-order)
background.setDepth(0);
ground.setDepth(1);
player.setDepth(2);
hud.setDepth(10);         // HUD elements always on top

// setScrollFactor controls parallax / HUD behavior
uiElement.setScrollFactor(0);    // Fixed to camera (HUD)
farBg.setScrollFactor(0.2);      // Slow parallax
nearBg.setScrollFactor(0.7);     // Fast parallax
gameObject.setScrollFactor(1);   // Default: moves with world

常见模式

交互式对象(可点击)
const button = this.add.sprite(x, y, 'button-up');
button.setInteractive({ useHandCursor: true });  // pointer cursor on hover
button.on('pointerover', () => button.setTexture('button-hover'));
button.on('pointerout', () => button.setTexture('button-up'));
button.on('pointerdown', () => button.setTexture('button-down'));
button.on('pointerup', () => {
  button.setTexture('button-up');
  this.onButtonClick();
});
带状态机的动画 Sprite
// In update() — simple state-based animation
const body = this.player.body as Phaser.Physics.Arcade.Body;
const isGrounded = body.blocked.down;
const isMoving = Math.abs(body.velocity.x) > 10;

if (!isGrounded) {
  this.player.play('player-jump', true);
} else if (isMoving) {
  this.player.play('player-walk', true);
} else {
  this.player.play('player-idle', true);
}
浮动伤害数字
private showDamage(x: number, y: number, amount: number): void {
  const text = this.add.text(x, y, `-${amount}`, {
    fontSize: '20px',
    color: '#ff4444',
    stroke: '#000',
    strokeThickness: 3,
  }).setOrigin(0.5).setDepth(100);

  this.tweens.add({
    targets: text,
    y: y - 60,
    alpha: 0,
    duration: 900,
    ease: 'Quad.Out',
    onComplete: () => text.destroy(),
  });
}

Phaser 4 注意事项

  • 已移除 Geom.Point — 2D 坐标请使用 Phaser.Math.Vector2
  • 已移除 Math.PI2 — 请使用 Math.TAU(π×2)或 Math.PI_OVER_2(π/2)
  • TileSprite 不再支持纹理裁剪 — 如有需要,请使用 RenderTexture
  • DynamicTexture/RenderTexture — 绘制操作后必须调用 .render()

其他资源

参考文件
  • references/game-objects-api.md — 每种对象类型的完整 API 速查、进阶用法和性能提示

评论

支持 Markdown