【问题标题】:How to make shape not to go to GameOver? -SpriteKit如何使形状不去GameOver? -SpriteKit
【发布时间】:2017-02-15 17:55:46
【问题描述】:

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

这是我的代码:

    let touch:UITouch = touches.first!
    let positionInScene = touch.location(in: self)
    let touchedNode = self.atPoint(positionInScene)




   if let name = touchedNode.name
    {
        //The first ball that shows up
        if name == "startball"
        {
            print("Touched", terminator: "")
             addBall(ballSize)




            self.addChild(score)
        }
        else if name == "shape"{

            scoreCount += 1

           addBall(ballSize)




            audioPlayer.play()

        }  





    }



    else {

        let scene =  GameOver(size: self.size)
        scene.setMyScore(0)
        let skView = self.view! as SKView
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .resizeFill
        scene.size = skView.bounds.size
        scene.setMessage("You Lost!")

        scene.setEndGameMode(va)
        gameTimer.invalidate()
        shownTimer.invalidate()
        print(" timers invalidated ", terminator: "")
        ran = true
        skView.presentScene(scene, transition: SKTransition.crossFade(withDuration: 0.25))
    }








    if firstTouch {
        shownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameScene.decTimer), userInfo: nil, repeats: true)
        gameTimer = Timer.scheduledTimer(timeInterval: TIME_INCREMENT, target:self, selector: Selector("endGame"), userInfo: nil, repeats: false)
        firstTouch = false



    }

    if touchCount > 5 {

        for touch: AnyObject in touches {
            let skView = self.view! as SKView
            skView.ignoresSiblingOrder = true
            var scene: Congratz!
            scene =  Congratz(size: skView.bounds.size)
            scene.scaleMode = .aspectFill
            skView.presentScene(scene, transition: SKTransition.doorsOpenHorizontal(withDuration: 1.0))



        }




    }
    touchCount += 1



}




    override func update(_ currentTime: TimeInterval) {
    super.update(currentTime)




}

func endGame(){
    shownTimer.invalidate()
    gameTimer.invalidate()
    let scene =  GameOver(size: self.size)
    scene.setMyScore(scoreCount)
    if let skView = self.view {
        skView.ignoresSiblingOrder = false
        scene.scaleMode = .resizeFill
        scene.size = skView.bounds.size
        scene.setMessage("Times up")

        skView.presentScene(scene, transition: SKTransition.crossFade(withDuration: 0.25))
    }

}







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

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

    currentBall.fillColor = pickColor()



    currentBall.position = randomBallPosition()




    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)
}

     func addSquare(_ size: Int) {
    // Add the square
    let shape = SKShapeNode(rectOf: CGSize(width:CGFloat(size), height:CGFloat(size)))

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

    shape.fillColor = pickColor()

    shape.position = randomBallPosition()

    shape.name = "shape"

    self.addChild(shape)

} 


    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)

}

【问题讨论】:

    标签: swift sprite-kit


    【解决方案1】:

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

    enum GameShape: Int {
    
        case circle = 0
        case square = 1
    
    }
    

    然后在GameScene 的顶部创建一个GameShape 属性:

    var currentShape: GameShape = .circle
    

    然后您可以创建某种updateShape 方法,您可以在您的touchesBegan 方法中调用它,而不仅仅是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
    }
    

    现在在您的touchesBegan 方法中,您可以调用updateShape,而不是调用addBall(size

    override func touchesBegan(_ touches: Set<UITouch>!, with event: UIEvent?) {
    
        // Setup your code for detecting position for shape origin
        updateShape(shapeSize)
    
    }
    

    EDIT - 你的代码一团糟。你真的应该花时间确保在提交时它的格式正确,否则真的很难帮助你。缩进有助于查看闭包的开始和结束位置。据我所知,您的 addBall 方法中似乎嵌套了两个或多个函数。不是很好。我已经尽力为你清理了。您仍然需要编写代码来使形状变成正方形,但我已经引导您朝着正确的方向开始实现这一目标:

    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)
    
    }
    

    现在上面的代码假设您有一些名为 shape 的属性,因为据我所知,您从未明确实例化过它,但您开始对其进行操作。

    【讨论】:

    • addSquare,它给我一个错误:“在声明之前使用局部变量 addSquare。”
    • @D.W - 也许你可以展示你用来实现它的代码?因为addSquare 是一个函数,而不是一个变量——所以我想你的语法可能是错误的
    • @D.W - 建议:编辑您的代码,使其格式正确。我们使用缩进是有原因的,它可以帮助我们了解特定代码块中发生了什么,以及括号从哪里开始和结束。我不知道您的代码中发生了什么,因为所有内容都与左侧对齐。据我所知,您似乎在addBall 方法中拥有addSquare 方法,并且在开始执行代码之前就使用} 关闭了addBall 方法。我只是添加了这些作为示例。您需要实际编写方法的代码
    • @D.W - 您不应该将函数嵌套在其他函数中。看起来您将 randomBallPosition 函数和 addSquare 函数放入了 addBall 函数中。
    • @D.W - 我已尽力清理您的代码。请查看我的更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多