2017-04-02 47 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) 
    } 
} 

}

問題是背景水平移動,在到達屏幕末端之後disa ppear,然後開始從屏幕開始再次移動

enter image description here

我想它垂直工作而消失

回答

0

背景水平移動,因爲moveBy(shiftBackground,replaceBackground)的申報工作在x斧(水平方向)。你需要在垂直方向上工作。

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

在第一行,背景從它的位置移動到它的位置 - 它的高度。在第二行,它返回到第一個位置(我認爲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) 

我不知道,但我想,如果你只想要一個解放運動,你可以刪除次序列:

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