InputEventScreenTouch
_input(event)という関数を使うと、eventがInputEventScreenTouchの場合、event.is_pressed()はスクリーンがタッチされるとtrueを、タッチがリリースされるとfalseを返します。これを使うと、スクリーンがタッチされているのか、リリースされたのかを判断できます。
これをSokobanのplayerの移動に活用しようと考えました。
現在は、移動ボタンを押すごとに1グリッド移動する形になっています。この機能を使うと、ボタンを押している間はplayerが移動し、ボタンを離すとplayerが止まる、という操作が可能になります。
player.gdを以下のように書き直しました。
これをSokobanのplayerの移動に活用しようと考えました。
現在は、移動ボタンを押すごとに1グリッド移動する形になっています。この機能を使うと、ボタンを押している間はplayerが移動し、ボタンを離すとplayerが止まる、という操作が可能になります。
player.gdを以下のように書き直しました。
# キーの入力判定.
var is_moving = false
var _is_mouseok = true
var _is_touch = false
# ---------------------------------------
# public functions.
# ---------------------------------------
func _input(event):
if event is InputEventScreenTouch:
if event.is_pressed():
_is_touch = true #スクリーンがタッチされている
else:
_is_touch = false #タッチがリリースされた
func proc(delta:float) -> void:
var _touch = Vector2()
# タッチのx座標
_touch.x = get_viewport().get_mouse_position().x
# タッチのY座標
_touch.y = get_viewport().get_mouse_position().y
if _is_mouseok and _is_touch:
if _touch.x > 320 and _touch.x < 380 and _touch.y > 704 and _touch.y < 758:
_dir = Direction.eType.UP
is_moving = true
elif _touch.x > 240 and _touch.x < 300 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.LEFT
is_moving = true
elif _touch.x > 320 and _touch.x < 380 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.DOWN
is_moving = true
elif _touch.x > 400 and _touch.x < 460 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.RIGHT
is_moving = true
if is_moving:
#移動処理の関数へ
# 移動処理中は_is_mouseok = falseにしてタッチ入力を受け付けない
var is_moving = false
var _is_mouseok = true
var _is_touch = false
# ---------------------------------------
# public functions.
# ---------------------------------------
func _input(event):
if event is InputEventScreenTouch:
if event.is_pressed():
_is_touch = true #スクリーンがタッチされている
else:
_is_touch = false #タッチがリリースされた
func proc(delta:float) -> void:
var _touch = Vector2()
# タッチのx座標
_touch.x = get_viewport().get_mouse_position().x
# タッチのY座標
_touch.y = get_viewport().get_mouse_position().y
if _is_mouseok and _is_touch:
if _touch.x > 320 and _touch.x < 380 and _touch.y > 704 and _touch.y < 758:
_dir = Direction.eType.UP
is_moving = true
elif _touch.x > 240 and _touch.x < 300 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.LEFT
is_moving = true
elif _touch.x > 320 and _touch.x < 380 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.DOWN
is_moving = true
elif _touch.x > 400 and _touch.x < 460 and _touch.y > 776 and _touch.y < 830:
_dir = Direction.eType.RIGHT
is_moving = true
if is_moving:
#移動処理の関数へ
# 移動処理中は_is_mouseok = falseにしてタッチ入力を受け付けない
タッチ入力とマウス入力が混在していますが、Godotがどちらもエミュレートしてくれるので、両方の良いとこ取りで書いています。
_touch.x > 320 and _touch.x < 380 and _touch.y > 704 and _touch.y <
758というのはそれぞれ矢印ボタンの位置です。
多分もっと簡単な表記方法で範囲を指定することができると思うのですが、初心者には分からないので仕方ありません。
主人公の移動はとてもスムーズになりました。