LoginSignup
1
0

More than 1 year has passed since last update.

【Playdate】クランクでプレイヤーを動かしてみる

Posted at

前回のクランク通知をベースにして、クランクを動かすとプレイヤーも動くように調整してみましょう。

sample_crank.gif

import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"

import "CoreLibs/ui"

local gfx <const> = playdate.graphics

local playerSprite = nil

local distanceFromCenter <const> = 60 -- 中心からの距離

function myGameSetUp()

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

	playerSprite = gfx.sprite.new( playerImage )
	playerSprite:moveTo( 200, 120 )
	playerSprite:add()

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

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

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()
	
	if playdate.isCrankDocked() then
		playdate.ui.crankIndicator:update()
	else
        local angleRad = (playdate.getCrankPosition() -90) * ( math.pi / 180 )  -- radianを格納
        local x = distanceFromCenter * math.cos(angleRad) + 200 -- x座標
        local y = distanceFromCenter * math.sin(angleRad) + 120 -- y座標
        
        playerSprite:moveTo(x, y)
	end


end

playdate.getCrankPosition()で現在のクランクの角度を取得しています。
Playdateのクランクは真上が「0」となるので、ラジアンを求める際「-90」で起点を調整しています。
あとは通常通りの三角関数によって座標を計算し、playerSprite:moveTo(x, y)で座標に移動しています。

1
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
1
0