[swift] Scrolling Background 구현하기
스프라이트 키트를 사용해서 아래와 같이 하면 된다.
import SpriteKit
class GameScene: SKScene {
// 전역 선언
let backRoad1 = SKSpriteNode(imageNamed: "grass_bg_02")
let backRoad2 = SKSpriteNode(imageNamed: "grass_bg_02")
let backFlower1 = SKSpriteNode(imageNamed: "grass_bg_03")
let backFlower2 = SKSpriteNode(imageNamed: "grass_bg_03")
var deltaRoad:CGFloat = 120.0
var deltaFlower:CGFloat = 20.0
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// 배경
let imageField = UIImage(named: "grass_bg_01")
let textureField = SKTexture(image: imageField!)
let backField = SKSpriteNode(texture: textureField, size: self.frame.size)
backField.anchorPoint = CGPointZero
backField.zPosition = -3
self.addChild(backField)
// 길
backRoad1.anchorPoint = CGPointZero
backRoad1.position = CGPointZero
backRoad1.zPosition = -2
self.addChild(backRoad1)
backRoad2.anchorPoint = CGPointZero
backRoad2.position = CGPointMake(0, backRoad1.size.height - 1)
backRoad2.zPosition = -2
self.addChild(backRoad2)
// 꽃
backFlower1.anchorPoint = CGPointZero
backFlower1.position = CGPointZero
backFlower1.zPosition = -2
self.addChild(backFlower1)
backFlower2.anchorPoint = CGPointZero
backFlower2.position = CGPointMake(0, backFlower2.size.height - 1)
backFlower2.zPosition = -2
self.addChild(backFlower2)
deltaRoad = 60.2
deltaFlower = 30.2
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
// 흐르는 배경
backRoad1.position = CGPointMake(backRoad1.position.x, backRoad1.position.y - deltaRoad)
backRoad2.position = CGPointMake(backRoad2.position.x, backRoad2.position.y - deltaRoad)
if(backRoad1.position.y < -backRoad1.size.height) {
backRoad1.position = CGPointMake(backRoad2.position.x, backRoad2.size.height + backRoad2.position.y)
}
if(backRoad2.position.y < -backRoad2.size.height) {
backRoad2.position = CGPointMake(backRoad1.position.x, backRoad1.size.height + backRoad1.position.y)
}
backFlower1.position = CGPointMake(backFlower1.position.x, backFlower1.position.y - deltaFlower)
backFlower2.position = CGPointMake(backFlower2.position.x, backFlower2.position.y - deltaFlower)
if(backFlower1.position.y < -backFlower1.size.height) {
backFlower1.position = CGPointMake(backFlower2.position.x, backFlower2.size.height + backFlower2.position.y)
}
if(backFlower2.position.y < -backFlower2.size.height) {
backFlower2.position = CGPointMake(backFlower1.position.x, backFlower1.size.height + backFlower1.position.y)
}
}
}
댓글
댓글 쓰기