如何强制touch.location只在屏幕上的特殊位置工作?

时间:2017-03-02 11:40:42

标签: swift swift3

这是我在这里的第一个问题 - Hello Word。

我的代码有一个小问题。我想强制执行这个程序,只有当你触摸并等于y:650点时才会出现球,并且只有当你在y:650点下触摸时才出现方框。 x位置在这里没有任何作用。

那是我的代码:

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

    if let touch = touches.first {
        let location = touch.location(in: self)
        let objects = nodes(at: location)

        if objects.contains(editLabel) {
            editingMode = !editingMode
        } else {
            if editingMode {
                let size = CGSize(width: GKRandomDistribution(lowestValue: 16, highestValue: 128).nextInt(), height: 16)
                let box = SKSpriteNode(color: RandomColor(), size: size)
                box.zRotation = RandomCGFloat(min: 0, max: 3)
                box.position = location
                box.physicsBody = SKPhysicsBody(rectangleOf: box.size)
                box.physicsBody!.isDynamic = false
                addChild(box)
            } else {
            let ball = SKSpriteNode(imageNamed: "ballRed")
            ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 2.0)
            ball.physicsBody!.contactTestBitMask = ball.physicsBody!.collisionBitMask
            ball.physicsBody!.restitution = 0.4
            ball.position = location
            ball.name = "ball"
            addChild(ball)
            }
        }
    }
}

期待寻求帮助:)

1 个答案:

答案 0 :(得分:0)

对我来说这很有效:

ball.position = location
if (location.y < 500) {
    ball.position.y = 500
}