如何从圆形到方形切换? -SpriteKit

时间:2017-02-17 05:19:54

标签: ios swift

我正在创建一个游戏,玩家正在点击圈子,我希望玩家点击广场而不是圈子。

以下是我的仅用于点按圈子的代码

override func addBall(_ size: Int) {
    let currentBall = SKShapeNode(circleOfRadius: CGFloat(size))
    let shape = SKShapeNode()
    let viewMidX = view!.bounds.midX
    let viewMidY = view!.bounds.midY

    currentBall.fillColor = pickColor()

    //Rectangle
    shape.path = UIBezierPath(roundedRect: CGRect(x: 64, y: 64, width: 160, height: 160), cornerRadius: 50).cgPath

    shape.fillColor = pickColor()

    func randomBallPosition() -> CGPoint {
        let xPosition = CGFloat(arc4random_uniform(UInt32((view?.bounds.maxX)! + 1)))
        let yPosition = CGFloat(arc4random_uniform(UInt32((view?.bounds.maxY)! + 1)))

        return CGPoint(x: xPosition, y: yPosition)
    }

    currentBall.position = randomBallPosition()
    shape.position = randomBallPosition()

    self.addChild(shape)

    //Remove other balls and add the new one
    if scoreCount != 0{
        if scoreCount == 1{
            self.addChild(score)
            self.addChild(timeLeftLabel)
            self.childNode(withName: "welcome")?.removeFromParent()
        }
        self.childNode(withName: "ball")?.run(getSmaller)
        self.childNode(withName: "ball")?.removeFromParent()
    }
    currentBall.name = "ball"

    self.addChild(currentBall)
}

1 个答案:

答案 0 :(得分:1)

使用SKShapeNode(rectOfSize: )

而不是SKShapeNode(circleOfRadius:)

其余代码将正常运行。

编辑:

您还想使用UIBezierPath(rect: CGRect)代替init(roundedRect: CGRect, cornerRadius: CGFloat)