处理短时和长时单击,拖放和精灵重叠吗?

问题描述

我的游戏将使用触摸控件,在实现以下机制时遇到很多麻烦。

当我短按一个精灵时,我希望它执行一个简单的click事件,在我的代码中,它应该突出显示该精灵。

但是当我长时间单击一个精灵时,它应该在某一时刻锚定到光标上并被它拖动。当我释放它时,它应该停止跟随光标并停留在原处。

最后但并非最不重要的一点是,我实际上正在处理多个Sprite,并且我希望始终影响最上面的Sprite(明智地使用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)

目前,唯一的问题是当我的光标下还有另一个精灵时,删除该精灵会触发该精灵的点击事件。

坦率地说,代码对于我正在做的事情来说还算不错。我要在这里询问是否有人知道一种更好的方式来编写这些机制。

解决方法

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"):
        if hold_time < HOLD_TARGET and input_in_sprite and is_holding:
            owning_hand.clicked_cards.append(self)
        if owning_hand.dragged_card:
            owning_hand.drop_card()
        is_holding = false
    if Input.is_action_just_pressed("click") and input_in_sprite:
        is_holding = true
    if is_holding:
        hold_time += delta
    else:
        hold_time = 0
    if hold_time >= HOLD_TARGET:
        owning_hand.held_cards.append(self)

这样编码似乎可以使其正常工作。 基本上,我使用is_holding布尔来收集有关是否早先按下了精灵的知识。如果不是,则is_action_just_released应该完全忽略对精灵进行的任何操作。

随时建议实现此目的的更好方法