嗨我有一系列的动作永远在侧滚动游戏中产生障碍。但是,不是从我的纹理生成随机图片和高度,而是保留了它在第一次运行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)
}
答案 0 :(得分:1)
不是每次都加载纹理,而是将纹理一次缓存在一个数组中,例如以下代码中的mountains
。
使用等待时间SKAction sequence
代替NSTimer重复执行函数。
您在设置山中不需要repeatActionForever
操作,因为生成的障碍物在使用removeParent
离开屏幕后会被移除。
var mountains : [SKTexture] = []
override func didMoveToView(view: SKView) {
mountains = [SKTexture(imageNamed:"RedMountain.png"),SKTexture(imageNamed:"OrangeMountain.png"),SKTexture(imageNamed:"BeigeMountain.png")]
let generateMountains = SKAction.runBlock { () -> Void in
self.setUpMountains()
}
let generateObstaclesPeriodically = SKAction.repeatActionForever (SKAction.sequence([generateMountains,SKAction.waitForDuration(4.0)]))
self.runAction(generateObstaclesPeriodically)
}
func setUpMountains() {
let gapHeight = user.size.height * 3
var movementAmount = arc4random() % UInt32(self.frame.size.height / 3.0)
var randomNumber = arc4random() % 3
let mountainTexture = mountains[randomNumber]
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
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 removeMountain = SKAction.removeFromParent()
var moveAndRemoveMountain = SKAction.sequence([moveMountain, removeMountain])
randomMountain.runAction(moveAndRemoveMountain)
}
要删除SKAction,您可以在添加时为SKAction
提供密钥,然后使用密钥将其从节点中删除。
self.runAction(generateObstaclesPeriodically, withKey: "generateObstaclesPeriodically")
self.removeActionForKey("generateObstaclesPeriodically")
答案 1 :(得分:0)
@rakeshbs感谢很多似乎有效,但我将其更改为
mountains = [SKTexture(imageNamed:"RedMountain.png"),SKTexture(imageNamed:"OrangeMountain.png"),SKTexture(imageNamed:"BeigeMountain.png")]
let generateMountains = SKAction.runBlock { () -> Void in
self.setUpMountains()
}
let generateObstaclesPeriodically = SKAction.repeatActionForever(SKAction.sequence([generateMountains,SKAction.waitForDuration(4.0)]))
self.runAction(generateObstaclesPeriodically)