解释
我试图在触摸屏幕时加快游戏速度(并在再次触摸时降低速度)。我让它工作了,但不是完全的。
当触摸屏幕时,所有操作都会被移除并再次运行(这样它就可以读取新的gameSpeed)。但是已经产生的精灵不会应用于新的操作,所以它仍然会在最后一个gameSpeed之前运行。有没有办法让所有的精灵都加速?
代码
这是我做的测试代码(你可以从here下载):
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 */
}
}提前谢谢你,
路易斯。
发布于 2016-10-04 07:45:20
在SKScene类的didMove方法中,添加以下内容:
physicsWorld.speed = CGFloat(2.0)最有可能的情况是您没有将其作为CGFloat传递,或者它没有包含在正确的方法中。
https://stackoverflow.com/questions/38040873
复制相似问题