【问题标题】:Trying to rotate bird up when touch began, and when touch ended rotate down (SceneKit)触摸开始时尝试向上旋转小鸟,触摸结束时向下旋转(SceneKit)
【发布时间】:2017-04-23 17:33:10
【问题描述】:

当我触摸屏幕时,小鸟以正确的角度向上旋转,但一旦触摸结束,它就会停留在那里。

我游戏中的墙也停止生成了!?

这是我的代码,如果有人能看到我做错了什么,请告诉我:D。 会很感激。谢谢

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


    if gameStarted == false{

        gameStarted = true

        bird.physicsBody?.affectedByGravity = true



        let spawn = SKAction.run({
            () in

            self.createWalls()

        })
        let delay = SKAction.wait(forDuration: 2.0)
        let spawnDelay = SKAction.sequence([delay, spawn])
        let spawnDelayForever = SKAction.repeatForever(spawnDelay)
        self.run(spawnDelayForever)


        let distance = CGFloat(self.frame.width + wallPair.frame.width)
        let movePipes = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval( 0.01 * distance))
        let removePipes = SKAction.removeFromParent()
        moveAndRemove = SKAction.sequence([movePipes,removePipes])

        bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))

        let rotateUp = SKAction.rotate(byAngle: 0.2, duration: 0)
        bird.run(rotateUp)


    }
    else{

        if died == true{

        }
        else{
            bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
            bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))



        }
    }

    for touch in touches{

        let location = touch.location(in: self)

        if died == true{

            if restartButton.contains(location){
                restartScene()
            }
        }
    }



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


    if gameStarted == false{
        gameStarted = true

        bird.physicsBody?.affectedByGravity = true

        let rotateDown = SKAction.rotate(byAngle: -0.2, duration: 0)
        bird.run(rotateDown)

    }
}

【问题讨论】:

    标签: ios swift rotation scenekit


    【解决方案1】:

    这里没有很多信息,但从我所看到的。当您开始触摸时,您将 gameStarted 设置为 true。所以你在 touchesended 中的 if 语句不会触发,因为 gameStarted 是真的。

    【讨论】:

    • 如果对您有帮助,我可以添加更多我的编码吗?你认为我应该使用 touchesEnded 声明吗?
    • 如果有帮助,我添加了其余的触摸代码! @Jaybit
    • 我必须在 if dead == true 语句下添加我的旋转操作。感谢您为我指明正确的方向!
    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多