Swift SpritKit : background image move

Swift Spirte Kit

Background Image move

如何連續移動背景圖,先看原理

先將 img-1 從 view 之中移動到 view 的下方直到看不到在移動回到 view 之中 再設定一組 img-2 從 view 的上面移動到 view 之中再回到 view 的上方 記住兩組的移動時間必須一至的

如何開始

宣告兩組變數

var background = SKSpriteNode()
var background02 = SKSpriteNode()

設定第一組圖片

background = SKSpriteNode(imageNamed: "image name")
background.size = CGSize(width: self.size.width, height: self.size.height)

background.position.y = self.size.height
background.zPosition = -1
self.addChild(background)

let move1 = SKAction.moveTo(y: self.frame.midY, duration: 30)
let move2 = SKAction.moveTo(y: self.size.height, duration: 0)


let seq = SKAction.sequence([move1,move2])
background.run(SKAction.repeatForever(seq))

設定第二組圖片

background02 = SKSpriteNode(imageNamed: "country-platform-back")
background02.size = CGSize(width: self.size.width, height: self.size.height)

background02.zPosition = -1
background02.position.y = self.frame.midY  //+ background02.size.height
self.addChild(background02)

let move3 = SKAction.moveTo(y: -self.size.height, duration: 30)
let move4 = SKAction.moveTo(y: self.frame.midY , duration: 0)

let seq1 = SKAction.sequence([move3,move4])
background02.run(SKAction.repeatForever(seq1))

留言

這個網誌中的熱門文章

Easy Triangle Calculator

Swift Sprite Kit easy to create bullet

Powershell transfert UPS Worldship address book to csv file for UPS web import