用Swift做个游戏Lecture 03 —— 实现foreground的持续移动
本文任务
持续移动地面任务一需要解决的问题:
问题一的解决思路是每次渲染完毕进入 问题二的解决思路是实例化两个Foreground,相邻紧挨,以约定好的速度向左移动,当第一个节点位置超出屏幕范围(对玩家来说是不可见)时,改变其坐标位置,添加到第二个节点尾部,如此循环实现无缝连接,参考图为: 代码实现: 找到GameScene类中的 func setupForeground() {
for i in 0..<2{
let foreground = SKSpriteNode(imageNamed: "Ground")
foreground.anchorPoint = CGPoint(x: 0,y: 1)
// 改动1
foreground.position = CGPoint(x: CGFloat(i) * size.width,y: playableStart)
foreground.zPosition = Layer.Foreground.rawValue
// 改动2
foreground.name = "foreground"
worldNode.addChild(foreground)
}
}
注意我们采用 Foreground匀速移动,自然速度值需要固定,姑且这里设为150.0,请在 对于Foreground的位置更新自然也是在方法 func updateForeground(){
//1
worldNode.enumerateChildNodesWithName("foreground") { (node,stop) -> Void in
//2
if let foreground = node as? SKSpriteNode{
//3
let moveAmt = CGPointMake(-self.kGroundSpeed * CGFloat(self.dt),0)
foreground.position += moveAmt
//4
if foreground.position.x < -foreground.size.width{
foreground.position += CGPoint(x: foreground.size.width * CGFloat(2),y: 0)
}
}
}
}
讲解:
ok,将 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |