需要帮助才能使球只能从游戏的顶部掉落

时间:2015-11-19 17:38:53

标签: ios swift sprite-kit

playing game from anywhere

嗨,所以我希望用户只能从顶排的钉子上方点击,将球从钉子上掉下来。现在,您可以轻松触摸屏幕上的任何位置,包括目标,球将会到达那里。我已经尝试过使用if语句和CGPoint来制作它,这样你就可以从钉子上方删除它,但我太初学了解它。感谢。

这是我的代码,使用Swift 2和Xcode 7:我认为它会进入这里......

    for touch in touches {

        if self.nunus.count >= numberOfNunus {
            self.removeChildrenInArray(self.nunus)
            self.nunus = []
            self.score = 0
            updateLabels()
        } else {

            let nunu = SKSpriteNode(imageNamed:"nunu")

            nunu.xScale = 0.13
            nunu.yScale = 0.13
            nunu.position = touch.locationInNode(self)

            nunu.physicsBody = SKPhysicsBody(circleOfRadius: nunu.size.height / 2)
            nunu.physicsBody?.categoryBitMask = self.nunuCategory
            nunu.physicsBody?.collisionBitMask = self.pegCategory | self.borderCategory | self.postCategory | self.nunuCategory
            nunu.physicsBody?.contactTestBitMask = smallGoalCategory | self.mediumGoalCategory | self.bigGoalCategory



            self.addChild(nunu)

            self.nunus.append(nunu)
            updateLabels()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果只触摸高于场景的midle,此代码会创建和删除节点。如果你想改变高度改变这一行:

if location.y > worl.frame.size.height / 2

例如:

if location.y > worl.frame.size.height * 0.8

表示您的触摸位置必须高于场景高度的80%

touchesBegan

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {
        let location = touch.locationInNode(self)
        //let touchedNode = nodeAtPoint(location)

        if location.y > worl.frame.size.height / 2 {

            createNode()
        }
    }
}

func createAndDropNode() {

    //code here
}
  

world - 与SKScene完全相同的SKNode

答案 1 :(得分:0)

想出来。 iPhone屏幕上的点数为500或以上。因此,如果用户触及500点以下,则丢球的代码将无法运行。我添加了这行代码: if touch.locationInNode(self).y&gt; 500

func touchesBegan(touches:Set,withEvent event:UIEvent?){         / *触摸开始时调用* /

    for touch in touches {

        if touch.locationInNode(self).y > 500 {


        if self.nunus.count >= numberOfNunus {
            self.removeChildrenInArray(self.nunus)
            self.nunus = []
            self.score = 0
            updateLabels()
        } else {

            let nunu = SKSpriteNode(imageNamed:"nunu")

            nunu.xScale = 0.13
            nunu.yScale = 0.13
            nunu.position = touch.locationInNode(self)

            nunu.physicsBody = SKPhysicsBody(circleOfRadius: nunu.size.height / 2)
            nunu.physicsBody?.categoryBitMask = self.nunuCategory
            nunu.physicsBody?.collisionBitMask = self.pegCategory | self.borderCategory | self.postCategory | self.nunuCategory
            nunu.physicsBody?.contactTestBitMask = smallGoalCategory | self.mediumGoalCategory | self.bigGoalCategory




            self.addChild(nunu)

            self.nunus.append(nunu)
            updateLabels()
        }
        }
    }
}