使用精灵套件垂直滚动背景的问题
Posted
技术标签:
【中文标题】使用精灵套件垂直滚动背景的问题【英文标题】:Issue with scrolling background vertically with sprite kit 【发布时间】:2015-05-07 08:29:56 【问题描述】:我正在尝试垂直滚动游戏背景,它可以工作一段时间,后来背景变空。这是我尝试过的:
var background = SKSpriteNode(imageNamed: "bgPlayScene")
func addBG()
let backgroundTexture = SKTexture(imageNamed: "bgPlayScene")
let shiftBackground = SKAction.moveToY(-backgroundTexture.size().height, duration: 9)
let replaceBackground = SKAction.moveToY(backgroundTexture.size().height, duration: 0)
let movingAndReplacingBackground = SKAction.repeatActionForever(SKAction.sequence([shiftBackground,replaceBackground]))
for var i = 0; i<3; i++
println(i)
//defining background; giving it height and moving width
background=SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.size.width = self.frame.width
background.runAction(movingAndReplacingBackground)
self.addChild(background)
这就是我得到的:http://gyazo.com/3da3a267aeb030225fdb8c0d563276aa 我不知道我错过了什么。如果有其他更好的方法,请告诉我。
【问题讨论】:
你需要有两个滚动背景才能无限滚动:***.com/a/20020023/437146 【参考方案1】:正如iAnum 所建议的那样,我从post 得到了答案,这是我的代码:
这样我添加了 2 个背景。
func addScrollingBG()
bg1 = SKSpriteNode(imageNamed: "bgPlayScene")
bg1.anchorPoint = CGPointZero
bg1.position = CGPointMake(0, 0)
bg1.size = CGSize(width: frame.size.width, height: frame.size.height)
addChild(bg1)
bg2 = SKSpriteNode(imageNamed: "bgPlayScene")
bg2.anchorPoint = CGPointZero
bg2.position = CGPointMake(0, bg1.size.height + 1)
bg2.size = CGSize(width: frame.size.width, height: frame.size.height)
self.addChild(bg2)
这是我的更新方法:
override func update(currentTime: NSTimeInterval)
bg1.position = CGPointMake(bg1.position.x, bg1.position.y-4)
bg2.position = CGPointMake(bg2.position.x, bg2.position.y-4)
if bg1.position.y < -bg1.size.height
bg1.position = CGPointMake(bg1.position.x, bg2.position.y + bg2.size.height)
if bg2.position.y < -bg2.size.height
bg2.position = CGPointMake(bg2.position.x, bg1.position.y + bg1.size.height)
【讨论】:
以上是关于使用精灵套件垂直滚动背景的问题的主要内容,如果未能解决你的问题,请参考以下文章