gpt4 book ai didi

iphone - SKAction 未按预期工作

转载 作者:行者123 更新时间:2023-11-30 10:18:53 26 4
gpt4 key购买 nike

嗨,我有一系列 Action 会永远运行以在横向卷轴游戏中产生障碍。但它不是从我的纹理生成随机图片和高度,而是保留第一次运行 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)

}

最佳答案

不要每次都加载纹理,而是将纹理缓存在数组中一次,例如以下代码中的山脉

使用带有等待时间的SKAction序列而不是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 提供一个 key ,然后使用该 key 将其从节点中删除。

 self.runAction(generateObstaclesPeriodically, withKey: "generateObstaclesPeriodically")
self.removeActionForKey("generateObstaclesPeriodically")

关于iphone - SKAction 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com