Game0003 - スプライトアニメーション

white_anime.pngを4フレームに分割してアニメーション表示します

⚙️ 操作パネル

📖 コード説明

// AnimatedSpriteクラス
class AnimatedSprite {
    constructor(sheetImage, x, y, size) {
        this.image = sheetImage;
        this.x = x;
        this.y = y;
        this.size = size;
        this.frameCount = 4;        // 4フレーム
        this.currentFrame = 0;      // 現在のフレーム
        this.frameSpeed = 10;       // フレーム速度(大きいほど遅い)
        this.counter = 0;           // カウンター
    }
    
    // フレーム更新
    update() {
        this.counter++;
        if (this.counter >= this.frameSpeed) {
            this.currentFrame = (this.currentFrame + 1) % this.frameCount;
            this.counter = 0;
        }
    }
    
    // 描画
    display() {
        let frameWidth = this.image.width / this.frameCount;
        let frameHeight = this.image.height;
        let srcX = frameWidth * this.currentFrame;
        
        // copy(元画像, 元X, 元Y, 元幅, 元高さ, 新X, 新Y, 新幅, 新高さ)
        p.copy(
            this.image,
            srcX, 0, frameWidth, frameHeight,
            this.x, this.y, this.size, this.size
        );
    }
}

// 使い方
let sprite = new AnimatedSprite(spriteSheet, 100, 100, 100);

p.draw = function() {
    sprite.update();  // フレーム更新
    sprite.display(); // 描画
};
    

🔑 重要なポイント

💡 応用例

🎮 コントロール

上のコントロールパネルでリアルタイムに設定を変更できます