gpt4 book ai didi

swift - 使用 sprite 工具包垂直滚动背景的问题

转载 作者:行者123 更新时间:2023-11-28 10:24:05 25 4
gpt4 key购买 nike

我正在尝试垂直滚动游戏背景,它工作了一段时间,后来背景变空了。这是我试过的:

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我不知道我错过了什么。如果有其他更好的方法,请告诉我。

最佳答案

根据 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)
}
}

关于swift - 使用 sprite 工具包垂直滚动背景的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30095736/

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