首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加速游戏中的所有精灵

加速游戏中的所有精灵
EN

Stack Overflow用户
提问于 2016-06-27 00:53:15
回答 1查看 56关注 0票数 0

解释

我试图在触摸屏幕时加快游戏速度(并在再次触摸时降低速度)。我让它工作了,但不是完全的。

当触摸屏幕时,所有操作都会被移除并再次运行(这样它就可以读取新的gameSpeed)。但是已经产生的精灵不会应用于新的操作,所以它仍然会在最后一个gameSpeed之前运行。有没有办法让所有的精灵都加速?

代码

这是我做的测试代码(你可以从here下载):

代码语言:javascript
复制
import SpriteKit

class GameScene: SKScene {

    // MARK: - Declare
    lazy var frameHeight : CGFloat = CGFloat(self.frame.height)

    var ball = SKSpriteNode()

    var speeded = Bool()
    var gameSpeed = CGFloat(1)

    // MARK: - Setup
    func setupBall(){

        ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPointMake(self.frame.width / 2, self.frame.height)
    }

    // MARK: - Action
    func actionsBall(){

        // Spawn ball
        self.setupBall()
        self.addChild(self.ball)

        // Move down and remove when go off screen
        let duration = (NSTimeInterval((0.003 / gameSpeed) * frameHeight))
        let moveBall = SKAction.moveByX(0, y: -frameHeight, duration: duration)
        let removeBall = SKAction.removeFromParent()

        self.ball.runAction(SKAction.sequence([moveBall, removeBall]))
    }

    // MARK: - First Event
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

      // Start spawning, moving and removing
      runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration((0.00075 / Double(gameSpeed)) * Double(frameHeight)), SKAction.runBlock(self.actionsBall)])))
    }

    // MARK: - Touch
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        if speeded == false{

            speeded = true

            gameSpeed = 2

            removeAllActions()
            runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration((0.00075 / Double(gameSpeed)) * Double(frameHeight)), SKAction.runBlock(self.actionsBall)])))
        }
        else if speeded == true{

            speeded = false

            gameSpeed = 1

            removeAllActions()
            runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration((0.00075 / Double(gameSpeed)) * Double(frameHeight)), SKAction.runBlock(self.actionsBall)])))
        }
    }

    // MARK: - Update
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

    }
}

提前谢谢你,

路易斯。

EN

回答 1

Stack Overflow用户

发布于 2016-10-04 07:45:20

在SKScene类的didMove方法中,添加以下内容:

代码语言:javascript
复制
physicsWorld.speed = CGFloat(2.0)

最有可能的情况是您没有将其作为CGFloat传递,或者它没有包含在正确的方法中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38040873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档