调用函数 touchesBegan 时 SpriteKit 游戏延迟
Posted
技术标签:
【中文标题】调用函数 touchesBegan 时 SpriteKit 游戏延迟【英文标题】:SpriteKit game lag when the function touchesBegan is called 【发布时间】:2015-07-17 17:58:03 【问题描述】:我正在创建我的第一个游戏,类似于 Flappy Bird。我希望它像真正的游戏一样在触摸屏幕时启动。但它滞后了大约一秒半,结果是你甚至不玩就死了。这是我的代码:
override func didMoveToView(view: SKView)
/* Setup your scene here */
//Here i init some stuff
distanceToMove = CGFloat(self.frame.size.width + 140)
movePipes = SKAction.repeatActionForever(SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(1.2)))
removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes,removePipes])
let delay = SKAction.waitForDuration(NSTimeInterval(0.6))
let spawn = SKAction.runBlock(() in self.initPipes())
let spawnAndDelay = SKAction.sequence([spawn,delay])
spawnAndDelayForever = SKAction.repeatActionForever(spawnAndDelay)
TouchesBegan:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
/* Called when a touch begins */
let touch = touches.first! as UITouch!
let touchLocation = touch.locationInNode(self)
if state == GameState.Starting
state = GameState.Playing
instNode.hidden = true
if state == GameState.Playing
runAction(spawnAndDelayForever)
addChild(pipes)
initScore()
管道初始化:
func initPipes()
let upper = UInt32(self.size.height - 250)
let pY = arc4random_uniform(upper) + 200
let pipePair = SKNode()
pipePair.position = CGPoint(x: self.frame.size.width + 70, y: 0)
//PIPE 1
let pipe1 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
pipe1.anchorPoint = CGPointMake(0, 0)
pipe1.position = CGPoint(x: 0, y: Int(pY))
pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(70/2, 700/2))
pipe1.physicsBody?.dynamic = false
pipe1.physicsBody?.affectedByGravity = false
pipe1.physicsBody?.categoryBitMask = PipeCategory
pipe1.physicsBody?.contactTestBitMask = PlayerCategory
pipe1.physicsBody?.collisionBitMask = PlayerCategory
pipePair.addChild(pipe1)
//PIPE 2
let pipe2 = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(70, 700))
pipe2.anchorPoint = CGPointMake(0,1)
pipe2.position = CGPoint(x: 0, y: pipe1.position.y - 150)
pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(70, 700), center: CGPointMake(35, -700/2))
pipe2.physicsBody?.dynamic = false
pipe2.physicsBody?.affectedByGravity = false
pipe2.physicsBody?.categoryBitMask = PipeCategory
pipe2.physicsBody?.contactTestBitMask = PlayerCategory
pipe2.physicsBody?.collisionBitMask = PlayerCategory
pipePair.addChild(pipe2)
//SCORE
let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: CGSize(width: 50, height: 150))
scoreSprite.anchorPoint = CGPointMake(0, 1)
scoreSprite.position = CGPointMake(pipe1.position.x + 10, pipe1.position.y)
scoreSprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 150), center: CGPointMake(25, -75))
scoreSprite.physicsBody?.categoryBitMask = GapCategory
scoreSprite.physicsBody?.contactTestBitMask = PlayerCategory
scoreSprite.physicsBody?.collisionBitMask = 0
scoreSprite.physicsBody?.dynamic = false
pipePair.addChild(scoreSprite)
pipePair.runAction(moveAndRemove)
pipes.addChild(pipePair)
这很简单:在 initPipes() 中,我创建管道并运行移动和移除操作。在 touchesBegan 中,我调用了生成它们的动作。但是当我触摸屏幕时它很迟钝。
【问题讨论】:
有什么更新吗?我对完全空的 touchesBegan 方法也有类似的问题…… 你找到这个人的解决方案了吗?这也扼杀了我的游戏体验。有时我的游戏在点击屏幕时会出现延迟,让玩家输掉游戏。与硬币和其他东西的碰撞也滞后......所以wtf在这里发生:S你也知道为什么声音有点滞后吗?好像不能同时播放 2 个声音。 【参考方案1】:通过Time ProfilerInstrument 运行您的应用程序,找出延迟的来源。它会为您提供详细的结果(直至单独的代码行),让您知道问题出在哪里。
这将比这里的人们猜测的要准确得多。
【讨论】:
以上是关于调用函数 touchesBegan 时 SpriteKit 游戏延迟的主要内容,如果未能解决你的问题,请参考以下文章
touchesBegan 和 touchesEnded 未在 UIButton 上调用