当玩家在Y轴上向上移动时,仅移动地面视差

时间:2016-04-27 15:40:11

标签: ios swift sprite-kit

基本上,我正在制作一个平台跳跃游戏。当玩家与平台碰撞时,玩家在y轴上向上获得一个强制爆破。我得到的代码下面的代码模拟了播放器后面的视图。当他在y轴向下前进时,它也跟随玩家。我不想那样。只有当玩家向上移动时才希望midground,foreground和background向下移动。

Gamescene.swift:

class GameScene: SKScene, SKPhysicsContactDelegate {

var background:SKNode!
var midground:SKNode!
var foreground:SKNode!




required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}

override init(size: CGSize){
    super.init(size: size)

    background = createBackground()
    addChild(background)

    midground = createMidground()
    addChild(midground)

    foreground = SKNode()
    addChild(foreground)

    player = createPlayer()
    foreground.addChild(player)

}

override func update(currentTime: CFTimeInterval) {

    if player.position.y > 200{
        background.position = CGPoint(x: 0, y: -((player.position.y - 200)/10))
        midground.position = CGPoint(x: 0, y: -((player.position.y - 200)/4))
        foreground.position = CGPoint(x: 0, y: -((player.position.y) - 200))
    }

}

1 个答案:

答案 0 :(得分:1)

您可以查看玩家位置。

    var PlayerPosition1 = 0

override func update(currentTime: CFTimeInterval) {

    if player.position.y > 200{
        if player.position.y > PlayerPosition1{
            background.position = CGPoint(x: 0, y: -((player.position.y - 200)/10))
            midground.position = CGPoint(x: 0, y: -((player.position.y - 200)/4))
            foreground.position = CGPoint(x: 0, y: -((player.position.y) - 200))
        }
    }

    PlayerPosition1 = player.position.y
}

这样的事情应该有用......

编辑: 好吧..它不是真正的"滞后" ..背景只是回到了'#34; old"触摸平台时的位置..试试这个:

var PlayerMaxY = CGFloat()

    if player.position.y > 200 {
        if player.position.y > PlayerMaxY{
            PlayerMaxY = player.position.y
            background.position = CGPoint(x: 0, y: -((player.position.y - 200)/10))
            midground.position = CGPoint(x: 0, y: -((player.position.y - 200)/4))
            forefround.position = CGPoint(x: 0, y: -((player.position.y - 200)))
        }
    }