我正在尝试创建一个背景论坛游戏,并有两个背景图像。我可以使用两个不同的SKSpriteNode(),SKTexture,SKAction.sequence..etc和一些数学来将它们拼接在一起,但它出现了一些不稳定,而且我似乎无法使数学恰到好处。它们应该能够被组合起来以产生长时间变化的背景,但它总是会抵消。
有人能告诉我如何将两种纹理合二为一,所以我不必用数学来打破我的脑袋。或者,如果有人在我的数学中发现了一些错误,请指出它们吗?两个背景都具有1200像素的相同宽度
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var bgTexture = SKTexture(imageNamed: "bg_2_midground_1.png")
var bgTexture2 = SKTexture(imageNamed: "bg_2_midground_2.png")
var totalBG = bgTexture.size().width + bgTexture2.size().width
var moveBG1 = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
//shift bg back to be able to repeat itself
var replaceBG1 = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0.0) //move immediately back to replace self
var moveBG1Forever = SKAction.repeatActionForever(SKAction.sequence([moveBG1,replaceBG1]))
var moveBG2 = SKAction.moveByX(-bgTexture2.size().width, y: 0, duration: 12)
//shift bg back to be able to repeat itself
var replaceBG2 = SKAction.moveByX(bgTexture2.size().width, y: 0, duration: 0.0) //move immediately back to replace self
var moveBG2Forever = SKAction.repeatActionForever(SKAction.sequence([moveBG2,replaceBG2]))
for var i:CGFloat = 0; i<2; i++ {
var bg = SKSpriteNode(texture: bgTexture)
var bg2 = SKSpriteNode(texture: bgTexture2)
bg.position = CGPointMake(bgTexture.size().width/2.0 + bgTexture.size().width * i + bgTexture2.size().width * i, CGRectGetMidY(self.frame))
bg2.position = CGPointMake(bgTexture.size().width/2.0 + bgTexture2.size().width + bgTexture2.size().width * 2 * i, CGRectGetMidY(self.frame))
bg.runAction(moveBG1Forever)
bg2.runAction(moveBG2Forever)
self.addChild(bg)
self.addChild(bg2)
println(bg.position)
println(bgTexture.size().width)
println(bg2.position)
println(bgTexture2.size().width)
}
}
答案 0 :(得分:2)
您可以使用update
功能将所有背景节点移动到一起,而不是使用SKActions,只要每个节点离开屏幕,就会将每个节点移动到最后。
为了使事情更易于管理,我们可以创建自定义背景SKNode。
class BackgroundNode : SKNode
{
override init() {
super.init()
var bgTexture1 = SKTexture(imageNamed: "b1.jpg")
var bgTexture2 = SKTexture(imageNamed: "b2.png")
let totalBG = bgTexture1.size().width + bgTexture2.size().width
for index in 0..<2
{
var bg1 = SKSpriteNode(texture: bgTexture1)
var bg2 = SKSpriteNode(texture: bgTexture2)
bg1.anchorPoint = CGPointMake(0, 0)
bg2.anchorPoint = CGPointMake(0, 0)
let i = CGFloat(index)
bg1.position = CGPointMake(i * bgTexture1.size().width + i * bgTexture2.size().width, 0)
bg2.position = CGPointMake((i+1) * bgTexture1.size().width + i * bgTexture2.size().width, 0)
self.addChild(bg1)
self.addChild(bg2)
lastNode = bg2
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var speedOfBackground : CGFloat = 300.0
var previousTime : CFTimeInterval = -1
var lastNode : SKSpriteNode!
func update(currentTime: CFTimeInterval) {
if previousTime != -1
{
var outOfBoundsSprite : SKSpriteNode? = nil
let deltaX = speedOfBackground * CGFloat(currentTime - previousTime)
for sknode in self.children
{
if let sprite = sknode as? SKSpriteNode
{
sprite.position = CGPointMake(sprite.position.x - deltaX, sprite.position.y)
if (sprite.position.x < -sprite.size.width)
{
outOfBoundsSprite = sprite
}
}
}
if (outOfBoundsSprite != nil)
{
outOfBoundsSprite?.position.x = lastNode.position.x + lastNode.size.width
lastNode = outOfBoundsSprite!
}
}
previousTime = currentTime
}
}
您可以更改speedOfBackground
属性以改变移动速度。
它可以像这样在SKScene中使用
class GameScene: SKScene ,SKPhysicsContactDelegate {
var background : BackgroundNode!
override func didMoveToView(view: SKView) {
background = BackgroundNode()
background.speedOfBackground = 500.0
self.addChild(background)
}
override func update(currentTime: CFTimeInterval) {
background.update(currentTime)
}
}
答案 1 :(得分:0)
@rakeshbs - 你的答案很完美,我确实决定扩大它。这将允许任何人在他们的项目中抛出Atlas并能够设置任意数量图像的滚动背景。但是,请注意一次加载到许多资产,如果你这样做,你需要将它们从游戏中删除,否则可能会发生不好的事情。 = P
(function(){
const bar, foo = bar = 1;
}());