2017-03-19 3 views
2

내 응용 프로그램에 위쪽에서 아래쪽으로 이동하여 반복되는 배경 이미지가 있습니다. x 시간이 지나면 다른 이미지가 화면에 표시되기를 원합니다.스위프트 게임 시간 또는 점수가있는 장면 전환 배경?

이렇게 할 수 있을까요?

너무 많은 레이어를 피하기 위해 초기 bg를 제거하는 것이 가장 좋은 방법은 언제입니까?

override func didMove(to view: SKView) { 

    let bgTexture = SKTexture(imageNamed: "bg1.png") 
    let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4) 
    let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0) 
    let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation])) 

    var i: CGFloat = 0 

    while i < 3 { 

     bg = SKSpriteNode(texture: bgTexture) 
     bg.position = CGPoint(x: self.frame.midX, y: bgTexture.size().height * i) 
     bg.size.width = self.frame.width 
     bg.zPosition = -2 

     bg.run(moveBGForever) 
     self.addChild(bg) 

     i += 1 


    } 

    bg2Timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.moveBG2), userInfo: nil, repeats: false) 

} 

func moveBG2() { 

    let bg2Texture = SKTexture(imageNamed: "bg2.png") 
    let moveBG2animation = SKAction.move(by: CGVector(dx: 0, dy: -bg2Texture.size().height * 2), duration: 8) 
    let shiftBG2Animation = SKAction.move(by: CGVector(dx: 0, dy: bg2Texture.size().height), duration: 0) 
    let moveBG2Forever = SKAction.repeatForever(SKAction.sequence([moveBG2animation, shiftBG2Animation])) 

    var j: CGFloat = 0 

    while j < 3 { 

     bg2 = SKSpriteNode(texture: bg2Texture) 
     bg2.position = CGPoint(x: self.frame.midX, y: bg2Texture.size().height + bg2Texture.size().height * j) 
     bg2.size.width = self.frame.width 
     bg2.zPosition = -1.5 


     bg2.run(moveBG2Forever) 
     self.addChild(bg2) 

     j += 1 


    } 



} 

답변

3

내가 생각해 낸 아이디어입니다.

리팩터링이 코드 조각 : 방법에

let bgTexture = SKTexture(imageNamed: "bg.png") 
    let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4) 
    let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0) 
    let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation])) 

:

func runBgActions(bgTextureName: String) { 
    let bgTexture = SKTexture(imageNamed: bgTextureName) 
    bg.texture = bgTexture 
     let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4) 
     let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0) 
     let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation])) 
    bg.removeAllActions() 
    bg.run(moveBGForever) 
} 

그런 다음, 다른 작업을 생성하고 bg, 어쩌면 장면 자체가 아니라 노드에서 실행합니다.

let bg1Action = SKAction.run { runBgActions(bgTextureName: "bg1") } 
let bg2Action = SKAction.run { runBgActions(bgTextureName: "bg2") } 
let bg3Action = SKAction.run { runBgActions(bgTextureName: "bg3") } 
let waitAction = SKAction.wait(forDuration: 10) 
let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction]) 
let repeatForeverAction = SKAction.repeatForever(sequence) 
self.run(repeatForeverAction) // assuming self is the scene here 

다른 방법은 같은 시간에 대기 및 이동 배경 작업을 실행 SKAction.group을 사용하는 것입니다,하지만 난 아주 직관을 구현하는 것을 찾을 수 있습니다. 이 큰 @Sweeper 보이는

var bg = SKSpriteNode() 

func runBgActions(bgTextureName: String) { 
    let bgTexture = SKTexture(imageNamed: bgTextureName) 
    bg.texture = bgTexture 
    bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY) 
    let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -(bgTexture.size().height - self.frame.height)), duration: 4) 
    let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height - self.frame.height), duration: 0) 
    let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation])) 
    bg.removeAllActions() 
    bg.run(moveBGForever) 
} 

override func didMove(to view: SKView) { 
    var i: CGFloat = 0 

    bg = SKSpriteNode(imageNamed: "bg1") // replace this with your first background's texture name 
    bg.position = CGPoint(x: self.frame.minX, y: self.frame.minY) 
    bg.anchorPoint = CGPoint(x: 0, y: 0) 
    bg.size.width = self.frame.width 
    bg.zPosition = -2 
    self.addChild(bg) 

    i += 1 
    let bg1Action = SKAction.run { self.runBgActions(bgTextureName: "bg1") } 
    let bg2Action = SKAction.run { self.runBgActions(bgTextureName: "bg2") } 
    let bg3Action = SKAction.run { self.runBgActions(bgTextureName: "bg3") } 
    let waitAction = SKAction.wait(forDuration: 10) 
    let sequence = SKAction.sequence([bg1Action, waitAction, bg2Action, waitAction, bg3Action, waitAction]) 
    let repeatForeverAction = SKAction.repeatForever(sequence) 
    self.run(repeatForeverAction) 
} 
+0

난 그냥 제대로 구현 사투를 벌인거야 :

는 코드를보고 난 후에, 이것은 내가 가지고 올 수 있었던 것입니다. 각 코드를 어디에 배치하는 것이 가장 좋은지 잘 모르겠습니다. 그리고 지금은 어떤 순서입니까? 나는 물건을 계속 움직일 것이다. 그리고 나는 내가 거기에 도착할 것이다라고 확신한다. 감사! –

+0

나는 그것이 거의 정확하다고 생각한다. 수정 된 질문을 참조하십시오. 나는 bgTexture를 선언 할 필요가있다. "오류가 발생했을 때"bgTexture "" –

+0

@AlexIngram "오류가 발생했을 때 함수의 이름이 로컬 변수와 충돌하기 때문이다. 지금 편집되었습니다. – Sweeper