LoginSignup
2
0

More than 1 year has passed since last update.

【Playdate】Spriteをアニメーションさせる

Posted at

Playdateではマトリクス状にレイアウトしたスプライト画像を用いてコマ送りアニメーションができます。
しかし、アニメーションするSpriteを作るのは色々と実装を行わなければならないため中々面倒です。
そこでAnimatedSpriteという拡張クラスを使って簡単にコマ送りアニメーションするspriteを作ります。
今回もこちらを改造して作ります。

以下のリポジトリからAnimatedSprite.luaをダウンロードします。

更に今回使用するスプライト画像を用意します。
player-table-32-32.png
player-table-32-32.png

AnimatedSprite.luaplayer-table-32-32.pngをダウンロードしたら以下のようにファイルを追加します。

myProjectName/
    source/
        main.lua
        Images/
            player-table-32-32.png ← 追加
            playerImage.png
            background.png
        Plugins/
            AnimatedSprite.lua ← 追加

main.luaは以下のように修正します。

main.lua
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

import 'Plugins/AnimatedSprite.lua' -- AnimatedSprite読み込み

local gfx <const> = playdate.graphics

local playerSprite = nil

function myGameSetUp()

    -- local playerImage = gfx.image.new("Images/playerImage")
    -- assert( playerImage )
    local playerTable = gfx.imagetable.new("Images/player") -- imagetable追加

    -- playerSprite = gfx.sprite.new( playerImage )
    playerSprite = AnimatedSprite.new(playerTable) -- AnimatedSpriteインスタンス作成
    playerSprite:moveTo( 200, 120 )
    playerSprite:add()
    playerSprite:addState('idle',1,4,{ tickStep = 4 }) -- カスタムアニメーションステートの追加
	playerSprite:playAnimation() -- アニメーション再生

    local backgroundImage = gfx.image.new( "Images/background" )
    assert( backgroundImage )

    gfx.sprite.setBackgroundDrawingCallback(
        function( x, y, width, height )
            backgroundImage:draw( 0, 0 )
        end
    )

end

myGameSetUp()

function playdate.update()

    if playdate.buttonIsPressed( playdate.kButtonUp ) then
        playerSprite:moveBy( 0, -2 )
    end
    if playdate.buttonIsPressed( playdate.kButtonRight ) then
        playerSprite:moveBy( 2, 0 )
    end
    if playdate.buttonIsPressed( playdate.kButtonDown ) then
        playerSprite:moveBy( 0, 2 )
    end
    if playdate.buttonIsPressed( playdate.kButtonLeft ) then
        playerSprite:moveBy( -2, 0 )
    end

    gfx.sprite.update()
    playdate.timer.updateTimers()

end

AnimatedSprite.luaのドキュメントは以下にあります。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0