【发布时间】:2015-02-11 00:11:46
【问题描述】:
您好,我有一个动作序列,在横向滚动游戏中会一直运行以产生障碍。但不是从我的纹理生成随机图片和高度,而是保持与第一次运行 SKActions 时生成的相同......
有人可以解决这个问题吗?我尽量不使用
NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("setUpMountains"), userInfo: nil, repeats: true)
由于某种原因它降低了我的 FPS
func setUpMountains() {
let gapHeight = user.size.height * 3
var movementAmount = arc4random() % UInt32(self.frame.size.height / 3.0)
var randomNumber = arc4random() % 3
if randomNumber == 0 {
mountainTexture = SKTexture(imageNamed:"RedMountain.png")
} else if randomNumber == 1 {
mountainTexture = SKTexture(imageNamed:"OrangeMountain.png")
} else {
mountainTexture = SKTexture(imageNamed:"BeigeMountain.png")
}
var randomMountain = SKSpriteNode(texture: mountainTexture)
randomMountain.position = CGPointMake( CGRectGetMidX(self.frame) + self.frame.width, CGRectGetMidY(self.frame) - gapHeight - CGFloat(movementAmount))
randomMountain.zPosition = 8
randomMountain.physicsBody = SKPhysicsBody(texture: mountainTexture, size: mountainTexture.size())
randomMountain.physicsBody?.dynamic = false
// randomMountain.physicsBody?.categoryBitMask = objectCategory
movingObjects.addChild(randomMountain)
//spawning mountains
var distanceToMove = self.frame.size.width + mountainTexture.size().width
var moveMountain = SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval (distanceToMove * 0.01))
var replaceMountain = SKAction.moveByX(distanceToMove, y: 0, duration: 0)
//var removeMountain = SKAction.removeFromParent()
var moveAndRemoveMountain = SKAction.repeatActionForever(SKAction.sequence([moveMountain, replaceMountain]))
randomMountain.runAction(moveAndRemoveMountain)
}
【问题讨论】:
-
你为什么要使用 NSTimer?它不适用于精灵套件。将 SKAction 与 waitForDuration 一起使用?
-
你能解释一下你在代码中想要做什么吗?
-
嘿@rakeshbs 我试图每隔 x 秒调用一次此函数以生成新的随机大小的障碍物。如果我使用 repeatActionForever 那么它将只使用第一次生成的相同 randomMountain 而不是重新生成新的。你能告诉我 .waitForDuration 会如何改变吗?
-
我正在使用 NSTimer 延迟调用选择器...但它破坏了我的 FPS
-
好的。您不想在添加新山之前删除现有山吗?
标签: iphone swift ios8 sprite-kit