嗨我有一個行動序列,永遠運行,在側面滾動遊戲中生成障礙。但不是從我的紋理生成隨機圖片和高度,它保持它第一次運行SKActions時產生的相同的那些...SKAction不按預期工作
有人可以解決這個問題嗎?我儘量不使用
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?它不適合sprite套件。改用waitForDuration來使用SKAction? – rakeshbs 2015-02-11 01:43:14
,你能解釋一下你在代碼中試圖做什麼嗎? – rakeshbs 2015-02-11 01:45:16
嗨@rakeshbs我想每x秒調用一次這個函數來產生新的隨機大小障礙物。如果我使用repeatActionForever,那麼它將只使用第一次生成的相同randomMountain,而不是重新生成新的。你能告訴我如何.waitForDuration會改變? – 2015-02-11 02:13:29