Endless scrolling (repeating) background in Spritekit game - Swift

时光怂恿深爱的人放手 提交于 2019-12-20 12:38:40

问题


I want to Create a endless scrolling background for my spritekit game, iT should consist of one or two images probably, which repeat themselves? I found these one and two examples, but they are in obj. C.

I have no idea how I can achieve this in Swift. And is it possible to set the speed manually?

Ps: I don't have the skill to convert obj. C into swift (newbie to Xcode dev.)


回答1:


I found a way to do this, somehow i managed to convert this obj. C to swift

You have to declare the two node's publicly

let background1 = SKSpriteNode(imageNamed: "bg1")
let background2 = SKSpriteNode(imageNamed: "bg2") 

In the "didMoveToView" method

background1.anchorPoint = CGPointZero
background1.position = CGPointMake(0, 0)
background1.zPosition = -15
self.addChild(background1)

background2.anchorPoint = CGPointZero
background2.position = CGPointMake(0, background1.size.height - 1)
background2.zPosition = -15
self.addChild(background2)

And in the "override func update(currentTime: CFTimeInterval)" method you add

background1.position = CGPointMake(background1.position.x, background1.position.y - 2)
background2.position = CGPointMake(background2.position.x, background2.position.y - 2)

            if(background1.position.y < -background1.size.height)
            {
                background1.position = CGPointMake(background2.position.x, background1.position.y + background2.size.height )
            }

            if(background2.position.y < -background2.size.height)
            {
                background2.position = CGPointMake(background1.position.x, background2.position.y + background1.size.height)

            }

i don't know if it's the most efficient way of doing this. The other questions mentioned a For loop. But this is easier in my opinion.



来源:https://stackoverflow.com/questions/26347559/endless-scrolling-repeating-background-in-spritekit-game-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!