【发布时间】:2020-10-17 02:33:03
【问题描述】:
我的游戏将使用触摸控件,但我在实现以下机制时遇到了很多麻烦。
当我对精灵进行短按时,我希望它执行一个简单的点击事件,在我的代码中,它应该突出显示精灵。
但是当我长按一个精灵时,它应该在某一点锚定到光标并被它拖动。当我释放它时,它应该停止跟随光标并保持在原地。
最后但并非最不重要的一点是,我实际上正在使用多个精灵,我希望始终影响最顶部的精灵(z_index 明智)。
所以现在,我的代码如下所示:
# Node2D, parent of my sprite
func _process(delta):
# Checks for clicks and hold, calls function based on events
if not owning_hand or not owning_hand.my_hand:
is_holding = false
return
if Input.is_action_just_released("click"):
owning_hand.drop_card()
is_holding = false
if not input_in_sprite:
is_holding = false
return
if Input.is_action_just_pressed("click"):
is_holding = true
if Input.is_action_just_released("click"):
if hold_time < HOLD_TARGET:
owning_hand.clicked_cards.append(self)
is_holding = false
if is_holding:
hold_time += delta
else:
hold_time = 0
if hold_time >= HOLD_TARGET:
owning_hand.held_cards.append(self)
func _input(event):
# If the mouse is in the sprite, set input_in_sprite to true
if not owning_hand or not owning_hand.my_hand:
return
if event is InputEventMouse and sprite.get_rect().has_point(to_local(event.position)) and not played:
input_in_sprite = true
else:
input_in_sprite = false
# Position2D, represents the player's input and methods
func _process(delta): # Gets the top most card z_index wise
# Checks for any clicked cards
print(held_cards)
if dragged_card:
dragged_card.position = get_global_mouse_position()
if clicked_cards:
var top_card = clicked_cards[0]
for Card in clicked_cards:
if Card.z_index > top_card.z_index:
top_card = Card
clicked_cards.clear()
highlight_card(top_card)
if held_cards:
var top_card = held_cards[0]
for Card in held_cards:
if Card.z_index > top_card.z_index:
top_card = Card
held_cards.clear()
drag_card(top_card)
func drag_card(card):
# Drags the card around
dragged_card = card
func drop_card():
# Drops the card
dragged_card = null
func highlight_card(card):
# Highlights the card
card.move_rotate(card.position + transform.y * -HIGHLIGHT_HEIGHT, card.rotation, REORGANISE_TIME)
目前,唯一的问题是当我的光标下有另一个精灵时放下一个精灵会触发精灵的点击事件没有被删除。
坦率地说,代码对于我正在做的事情来说还不错。我在这里询问是否有人知道更好的方法来编写这些机制。
【问题讨论】:
标签: touch sprite godot gdscript