Swift 如何使形状不去GameOver-斯皮特基特

Swift 如何使形状不去GameOver-斯皮特基特,swift,sprite-kit,Swift,Sprite Kit,我正在创建一个游戏,其中有一个正方形的形状,每次玩家点击正方形,它就会进入GameOver场景。我想做的就是当点击正方形形状时,它将被分配到屏幕的不同位置,以便在正方形上点击 这是我的密码: 我的建议是为不同的形状制作类似于enum的东西,这样您就可以跟踪您正在使用的形状 enum GameShape: Int { case circle = 0 case square = 1 } 然后在游戏场景的顶部创建GameShape属性: var currentShape: Gam

我正在创建一个游戏,其中有一个正方形的形状,每次玩家点击正方形,它就会进入GameOver场景。我想做的就是当点击正方形形状时,它将被分配到屏幕的不同位置,以便在正方形上点击

这是我的密码:


我的建议是为不同的形状制作类似于
enum
的东西,这样您就可以跟踪您正在使用的形状

enum GameShape: Int {

    case circle = 0
    case square = 1

}
然后在
游戏场景的顶部创建
GameShape
属性:

var currentShape: GameShape = .circle
然后,您可以创建某种
updateShape
方法,您可以在
touchesbeated
方法中调用它,而不仅仅是
addBall

func updateShape(shapeSize: CGSize) {

    switch currentShape {
        case .circle:
            addCircle(shapeSize)
        case .square:
            addSquare(shapeSize)
        default:
            break
    }

    // However you want to setup the condition for changing shape
    if (condition) {
        currentShape = .square
    }

}

func addBall(_ size: CGSize) {
    // Add the ball
}

func addSquare(_ size: CGSize) {
   // Add the square
}
现在,在您的
touchesbreated
方法中,您可以调用
updateShape
,而不是调用
addBall(size
):

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

    // Setup your code for detecting position for shape origin
    updateShape(shapeSize)

}

现在,上面的代码假设您有一个名为
shape
的属性,因为我可以告诉您从未显式实例化它,但您开始操作它。

addSquare,它给我一个错误:“在声明之前使用局部变量addSquare。”@D.W-也许你可以展示你用来实现它的代码?因为
addSquare
是一个函数,而不是一个变量-所以我想你可能是语法wrong@D.W-建议:编辑您的代码,使其格式正确。我们使用缩进是有原因的,它可以帮助我们了解特定代码块中发生了什么,以及Brake在哪里t开始和结束。我不能告诉你的代码中发生了什么,因为所有的东西都是向左对齐的。从我可以告诉你的情况来看,你在
addBall
方法中有
addSquare
方法,在你开始执行代码之前,你用
}
关闭了
addBall
方法。我刚刚添加了例如,您需要为methods@D.W-您不应该在其他函数中嵌套函数。看起来您将
randomBallPosition
函数和
addSquare
函数放在了
addBall
函数中。@D.W-我已尽力清理了您的代码。请参阅我的u更新的答案
override func touchesBegan(_ touches: Set<UITouch>!, with event: UIEvent?) {

    // Setup your code for detecting position for shape origin
    updateShape(shapeSize)

}
func addBall(_ size: CGSize) {
    // Add the ball
    let currentBall = SKShapeNode(circleOfRadius: CGFloat(size))

    let viewMidX = view!.bounds.midX
    let viewMidY = view!.bounds.midY

    currentBall.fillColor = pickColor()

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

    shape.fillColor = pickColor()

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

    self.addChild(shape)

    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"
    shape.name = "ball"

    self.addChild(currentBall)
}

func addSquare(_ size: CGSize) {
    // Add the square
}

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)

}