简体   繁体   English

仅当玩家沿Y轴向上行驶时才移动地视差

[英]Only move groundparallax when player heading upwards in Y-axis

So essentially, Im making a platformjumper game. 因此,基本上,我正在制作PlatformJumper游戏。 when the player collides with a platform, the player gets a forcebump in upwards in y-axis. 当玩家与平台碰撞时,玩家会在y轴上向上获得力撞。 The code down below that i got gives simulates the view following the player. 下面给出的代码模拟了播放器后面的视图。 It also follows the player when hes heading downwards in y-axis. 当玩家沿y轴向下行驶时,它也会跟随玩家。 I dont want that. 我不想要那个。 Only want the midground, foreground and background to move down when the player is heading upwards. 当玩家向上行驶时,只希望中地,前景和背景向下移动。

Gamescene.swift: 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))
    }

} }

You could check the Player Position. 您可以检查玩家位置。

    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
}

Something like that should work .. 这样的东西应该工作..

Edit: Ah okay .. it's not really a "lag" .. the background just moves back to the "old" position when touching the platform.. try this: 编辑:嗯..这不是一个真正的“滞后” ..当触摸平台时,背景只是移回到“旧”位置。

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)))
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM