2017-03-19 10 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

関連する問題