禁用 Sprite 上的用户交互

Posted

技术标签:

【中文标题】禁用 Sprite 上的用户交互【英文标题】:Disabling User Interaction On Sprites 【发布时间】:2017-01-17 20:17:50 【问题描述】:

所以我目前正在制作一个游戏,并且我有一个可以在屏幕上拖动的精灵,但是一旦我希望精灵不可争辩并且不响应用户交互,只要移动精灵。 目前这是我尝试过的

func addp1Cards() 

    let player1 = Card(cardType: CardType(rawValue: player1_cards[p1int])!)
    player1.size = CGSize(width: player1.size.width * 0.25, height: player1.size.height * 0.25)
    player1.position = CGPoint(x: -10.4927577972412, y: 615.942138671875)
    player1.zPosition = CGFloat(zpos)
    player1.name = "\(player1_cards[0])"
    print(player1_cards[0])
    addChild(player1)

    p1int += 1
    


   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
    for touch in touches 
        let location = touch.location(in: self)
        print(location)

        let rotR = SKAction.rotate(byAngle: 0.15, duration: 0.2)
        let rotL = SKAction.rotate(byAngle: -0.15, duration: 0.2)
        let cycle = SKAction.sequence([rotR, rotL, rotL, rotR])
        let wiggle = SKAction.repeatForever(cycle)

        if turnBool == true 

        if let card = atPoint(location) as? Card 
            card.zPosition = CardLevel.moving.rawValue
            addp1Cards()
            if touch.tapCount > 1 

                return

            

            if card.enlarged  return 

            card.run(wiggle, withKey: "wiggle")

            card.removeAction(forKey: "drop")
            card.run(SKAction.scale(to: 1.3, duration: 0.25), withKey: "pickup")

                        
        


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
    for touch in touches 

        let location = touch.location(in: self)
        if let card = atPoint(location) as? Card 

            if turnBool == true 

            if card.enlarged  return 
            card.position = location
        
        


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 
    for touch in touches 
        let location = touch.location(in: self)
        if let card = atPoint(location) as? Card 

            if turnBool == true 


            turnBool = false
            addp1Cards()
            if card.enlarged  return 

            card.zPosition = CardLevel.board.rawValue

            card.removeAction(forKey: "wiggle")
            card.run(SKAction.rotate(toAngle: 0, duration: 0.2), withKey:"rotate")

            card.removeAction(forKey: "pickup")
            card.run(SKAction.scale(to: 1.0, duration: 0.25), withKey: "drop")

            card.removeFromParent()
            addChild(card)

                card.flip()
             self.enumerateChildNodes(withName: "\(player2_cards[p2int - 2])")  card2y, stop in
                   card2y.isUserInteractionEnabled = false

           
            
        

【问题讨论】:

所以你希望能够点击它,这将是拖动的开始,然后拖动它,而第二次点击同一个精灵应该被忽略?但是您应该仍然可以拖动精灵,直到您将手指从精灵上移开? 是的,这正是我想要的 Moving multiple nodes at once from touch with SpriteKit的可能重复 这个问题之前已经问过很多次了 【参考方案1】:

我找到了解决办法。

我添加了一组卡片名称,其中包含已移动卡片的名称,如果我尝试移动的卡片名称与数组中的名称匹配,我将不允许移动代码运行。

这就是它的样子,

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
    for touch in touches 

        let location = touch.location(in: self)
        if let card = atPoint(location) as? Card 

            if turnBool == true 
                if Movedcards.contains(card.name!) 

                 else 
                    if card.enlarged  return 
                    card.position = location
                
        
    

【讨论】:

以上是关于禁用 Sprite 上的用户交互的主要内容,如果未能解决你的问题,请参考以下文章

ViewController 禁用交互 - iOS

禁用交互式搜索 - 用户端

如何禁用 UITableView 的部分索引的用户交互?

如何检测 UITextField 上的点击?

禁用所有 UITableViewCells 用户交互

两个不同的按钮对同一个视图控制器执行 segue,但希望通过一个按钮禁用用户交互以进行 segue