2017-04-02 5 views
0

私はSpriteKitベースで初心者だと私は背景が動くようにしようが垂直
は私が知っていた作り方 この何コントローラーは垂直移動が背景

class GameViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let scene = GameScene(size: view.bounds.size) 
     let skView = view as! SKView 
     skView.showsFPS = true 
     skView.showsNodeCount = true 
     skView.ignoresSiblingOrder = true 
     scene.scaleMode = .resizeFill 
     skView.presentScene(scene) 
    } 

と、このシーンで

class GameScene: SKScene { 

var background = SKSpriteNode() 
override func didMove(to view: SKView) { 

    let backgroundTexture = SKTexture(imageNamed: "bbbgg.png") 

    let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5) 
    let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0) 
    let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground])) 

    for i in 1...3{ 
     background=SKSpriteNode(texture:backgroundTexture) 
     background.position = CGPoint(x: 0, y: 0) 
     background.size.height = self.frame.height 
     background.run(movingAndReplacingBackground) 

     self.addChild(background) 
    } 
} 

}

問題は背景が水平に移動して画面の最後に到着した後ppearは、その後、私はそれが

答えて

0

背景に消えずに縦方向に働きたい

enter image description here

再び画面の先頭から移動開始しますMoveByメソッドの宣言(shiftBackground、replaceBackgroundが)×斧で動作しますので、水平方向に移動(水平方向)。あなたはy軸(垂直)で作業する必要があります。

let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5) 
    let replaceBackground = SKAction.moveBy(x: 0, y: backgroundTexture.size().height, duration: 0) 

最初の行では、背景がその位置からその位置に移動します。 2行目で、最初の位置に戻ります(moveByは相対的です)。

_あなたは、関数SKAction.repeatForever(movingAndReplacingBackground)を使用しているため

背景が再び動きます。私はあなたがそれを取り除くことができると思います。

let movingAndReplacingBackground = SKAction.sequence([shiftBackground,replaceBackground]) 

編集:

私はループを忘れます。

ループでは、バックグラウンドに初期位置を指定します。

  background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * CGFloat(i)), y: self.frame.midY) 

0でそれを置き換えるしようと、真ん中のすぐ目に見える背景

 background.position = CGPoint(x: 0, y: self.frame.midY) 

または-height、0にしたい場合は、あなたがしたい場合は背景が画面の上部で表示されます。あなたはshiftBackgroundとreplaceBackground

 background.position = CGPoint(x: -backgroundTexture.size().height, y: 0) 

    and 

    let shiftBackground = SKAction.moveBy(x: 0, y: backgroundTexture.size().height, duration: 5) 

    let replaceBackground = SKAction.moveBy(x: 0, y: 0, duration: 0) 

を交換する必要があり、この場合、私はわからないんだけど、私が思うに、あなただけの1 mouvementをしたい場合、あなたは番目の配列を除去することができます

class GameScene: SKScene { 

    var background = SKSpriteNode() 
    override func didMove(to view: SKView) { 

     let backgroundTexture = SKTexture(imageNamed: "bbbgg.png") 

     let scrollByTopBackground = SKAction.moveBy(x: O, y: backgroundTexture.size().height, duration: 5) 

     background = SKSpriteNode(texture:backgroundTexture) 
     background.position = CGPoint(x: -backgroundTexture.size().height, y: self.frame.midY) 
     background.size.height = self.frame.height 
     background.run(scrollByTopBackground) 

     self.addChild(background) 
    } 
} 

あなたに答えるために編集してください、これを試してください...

class GameScene: SKScene { 

var background = SKSpriteNode() 
override func didMove(to view: SKView) { 

    let backgroundTexture = SKTexture(imageNamed: "bbbgg.png") 

    let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5) 
    let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0) 
    let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground])) 

    for i in 0...2 { 
     background=SKSpriteNode(texture:backgroundTexture) 
     background.position = CGPoint(x: 0, y: self.frame.midY + (backgroundTexture.size().height * CGFloat(i))) 
     background.size.height = self.frame.height 
     background.run(movingAndReplacingBackground) 

     self.addChild(background) 
    } 
} 
+0

私は、これらの変化が、何も私は '使用動きが、私はそれのために永遠に動きリピートしたくありません – amjad

+0

を始めることにした後、私は、アプリケーションを実行するときにも存在する背景最初にしたい、今表示されませんでしたrepeatForever'私はあなたのコメントの後に私のコードを更新する問題:今度は上から下に移動しますが、上からではなく画面の中央から動き始めます。また、動きの間にアプリケーションが数秒間停止すると思います。 – amjad

+0

あなたのコードは今何ですか? – Valentin

関連する問題