【发布时间】:2017-01-10 22:09:21
【问题描述】:
我正在使用 Swift 和 SpriteKit 构建我的第一个游戏,并且想要添加背景。游戏发生在太空中,所以我想让背景中的星星以不同的速度移动。目前,我希望通过让较大的星星在屏幕上的移动速度比较小的星星更快来获得 3D 外观。有没有一种有效的方法来做到这一点,而不是像这样创建一个 SKNode 子类并将其作为子类添加到DidMoveToView 的开头?看起来这种方法非常密集,但我想我会在一遍又一遍地回收同一个图像之前尝试一下。
class BackGroundAnimation:SKNode{
let theView:SKView
init(aView:SKView){
theView = aView
super.init()
animate()
}
func animate(){
for _ in 1...200{
let randomSize = random(1, max: 3)
var randomPosx = random(1,max: 1000)
randomPosx = randomPosx/1000.0
var randomPosy = random(1,max: 1000)
randomPosy = randomPosy/1000.0
let star:SKSpriteNode = SKSpriteNode(texture:starTexture)
star.setScale(randomSize/60.0)
star.position = CGPoint(x:(theView.scene?.size.width)! * randomPosx,y:(theView.scene?.size.width)! * randomPosy)// (self.scene.size.width)*randomPosx, y:(self.scene.size.height) * randomPosy)
//star.position = CGPoint(x: 200,y: 200)
star.physicsBody = SKPhysicsBody(circleOfRadius: star.size.width/2 )
star.physicsBody?.collisionBitMask = 0
star.physicsBody?.categoryBitMask = 0
star.physicsBody?.contactTestBitMask = 0
star.physicsBody?.linearDamping = 0
star.physicsBody?.velocity = CGVector(dx:1 * randomSize, dy:0)
star.name = "star"
//addChild(star)
self.addChild(star)
self.moveToParent(self.scene!)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
任何帮助都会很棒。
【问题讨论】:
-
我相信您当前使用的方法是实现此类背景的正确方法。但是,如果您在真实设备(不是模拟器)上发现极端滞后和/或运行缓慢(由于背景),我会尝试废弃其中的一部分并将其降低到您真正需要的部分背景
-
您可以使用粒子,然后以不同的速度渲染 3 个粒子层,仅此而已。我的意思截图:twitter.com/JozemiteApps/status/678734299521155072.
-
查看 SKEmitterNode
标签: swift xcode background sprite-kit