作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在制作无限滚动条时遇到问题。 Google 结果显示我一定是使用了低效代码,因为我在模拟器和 iPhone 5(物理设备)上得到了大约 14-15 FPS,我得到了相同的结果。我将不胜感激有关使我的代码更高效的一些提示。也许合并功能?缩小图像并缩放它们?当前背景图像为 1136x750。平台大约为 200x75。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let backgroundVelocity : CGFloat = 6.0
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.whiteColor()
self.initializingScrollingBackground()
self.startPlatforms()
// Making self delegate of physics world
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.moveBackground()
self.movePlatforms()
}
func startPlatforms(){
for var n = 0; n < 1000; ++n {
let plat = SKSpriteNode(imageNamed: "platform")
plat.position = CGPoint(x: n * (Int(plat.size.width)+125), y: 90)
plat.anchorPoint = CGPointZero
plat.name = "platform"
self.addChild(plat)
}
}
func movePlatforms() {
self.enumerateChildNodesWithName("platform", usingBlock: { (node, stop) -> Void in
if let plat = node as? SKSpriteNode {
plat.position = CGPoint(x: plat.position.x - self.backgroundVelocity, y: plat.position.y)
if(plat.position.x <= -200 ){
node.removeFromParent()
}
}
})
}
func initializingScrollingBackground() {
for var index = 0; index < 2; ++index {
let bg = SKSpriteNode(imageNamed: "bg")
bg.position = CGPoint(x: index * Int(bg.size.width), y: -55)
bg.anchorPoint = CGPointZero
bg.name = "background"
self.addChild(bg)
}
}
func moveBackground() {
self.enumerateChildNodesWithName("background", usingBlock: { (node, stop) -> Void in
if let bg = node as? SKSpriteNode {
bg.position = CGPoint(x: bg.position.x - self.backgroundVelocity, y: bg.position.y)
// Checks if bg node is completely scrolled off the screen, if yes, then puts it at the end of the other node.
if bg.position.x <= -bg.size.width {
bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y)
}
}
})
}
}
最佳答案
编写游戏时要记住的一个基本事实:
Every code you want to run from the update method (so every frame) must be fast.
现在,让我们看看您的代码。
如果你有一堆节点一起移动,而不是手动移动每个节点,你应该添加一个公共(public)的 parent
给他们,然后只移动 parent
.
所以这个
func startPlatforms(){
for var n = 0; n < 1000; ++n {
let plat = SKSpriteNode(imageNamed: "platform")
plat.position = CGPoint(x: n * (Int(plat.size.width)+125), y: 90)
plat.anchorPoint = CGPointZero
plat.name = "platform"
self.addChild(plat)
}
}
变成这样
private let platformParent = SKNode()
func createPlatforms() {
self.addChild(platformParent)
for n in 0...999 {
let plat = SKSpriteNode(imageNamed: "platform")
plat.position = CGPoint(x: n * (Int(plat.size.width)+125), y: 90)
plat.anchorPoint = CGPointZero
plat.name = "platform"
platformParent.addChild(plat)
}
}
现在你不需要移动每个平台,你可以运行一个SKAction
只移动 parent 。 children
会自动跟随他。
所以这个
func movePlatforms() {
self.enumerateChildNodesWithName("platform", usingBlock: { (node, stop) -> Void in
if let plat = node as? SKSpriteNode {
plat.position = CGPoint(x: plat.position.x - self.backgroundVelocity, y: plat.position.y)
if(plat.position.x <= -200 ){
node.removeFromParent()
}
}
})
}
变成这样
// to be called only once!
func beginMovingParentPlatform() {
let moveToLeft = SKAction.moveToX(-200, duration: 5) // please change the 2 params (-200 and 5) as you need
self.platformParent.runAction(moveToLeft) {
self.removeAllChildren()
}
}
当然还有这个
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.moveBackground()
self.movePlatforms()
}
变成这样
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.moveBackground()
// self.movePlatforms()
}
现在您的代码应该会更快。
您可以将相同的逻辑应用于 moveBackground
.
记住,不要手动移动节点。使用 SKActions
,它们比您(或我)可以编写的代码更优化。
关于ios - Swift Infinite Scroller FPS 代码效率低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148584/
我是一名优秀的程序员,十分优秀!