2017-04-21 52 views
1

我想知道是否有人知道如何實現類似於飛揚的鳥遊戲的相機效果。它曾經看起來像移動相機在遊戲中發生地震。SpriteKit相機振動

謝謝

+0

請指定更多,「飛揚的鳥兒遊戲」? – Maetschl

+0

如果你想要一個搖動效果,你可以產生動作,隨機移動相機的方向隨機速度... – Whirlwind

+0

嗨@Whirlwind,你知道如何移動相機的方式(隨機方向和速度?) –

回答

0

我可以想到兩種有效的方法,可能對您有用。在將視圖切換到場景上的遊戲之前,可以隨機調整相機在update函數中的位置,或者使用着色器放大當前場景並執行類似操作。

至於使用更新,我可能會建議是這樣的:

創建以下枚舉和可變:

enum ShakeDirection: Int { 
    case up = 0 
    case down 
    case left 
    case right 
} 
var currentShakeDirection = ShakeDirection.up 

override func update(currentTime: CFTimeInterval) { 
    //Set this to true twice every 60 iterations (Since there are about 60 fps for most games, a random variable between 0 and 59 will be less than or equal to 1 about twice per second) 
    let switchDirection: Bool = (arc4_random(60) <= 1) 
    if switchDirection { 
     let rawIndex = Int(arc4random(4)) 
     currentShakeDirection = currentShakeDirection(rawValue: rawIndex) 
    } 
    switch currentShakeDirection { 
    case .up: 
     let x = camera.position.x 
     let y = camera.position.y + 1 
     camera.position = CGPoint(x: x, y: y) 
    case .down: 
     let x = camera.position.x 
     let y = camera.position.y - 1 
     camera.position = CGPoint(x: x, y: y) 
    case .left: 
     let x = camera.position.x - 1 
     let y = camera.position.y 
     camera.position = CGPoint(x: x, y: y) 
    case .right: 
     let x = camera.position.x + 1 
     let y = camera.position.y 
     camera.position = CGPoint(x: x, y: y) 
    } 
} 

這僅僅是一個可能的實現,有很多不同的選擇。這將會無限期地繼續下去,所以我會留給你設置一個方法來根據需要啓動和停止它。