在Sprite上禁用用户交互

时间:2017-01-17 20:17:50

标签: swift xcode sprite-kit sprite skspritenode

所以我正在制作一个游戏,我有一个可以在屏幕上拖动的精灵,但是只要移动精灵,我希望精灵是无法辨认的,而不是响应用户交互。 目前这是我试过的

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

           }
            }
        }

}

1 个答案:

答案 0 :(得分:-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
                }
        }
    }
}