【问题标题】:Only move groundparallax when player heading upwards in Y-axis仅当玩家在 Y 轴上向上移动时才移动地面视差
【发布时间】:2016-04-27 15:40:11
【问题描述】:

所以本质上,我正在制作一个跳台游戏。当玩家与平台发生碰撞时,玩家会在 y 轴上获得一个向上的冲击力。我得到的下面的代码模拟了玩家跟随的视图。当玩家在 y 轴上向下移动时,它也会跟随玩家。我不想要那个。只希望中景、前景和背景在玩家向上移动时向下移动。

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

}

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案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
    }
    

    类似的东西应该可以工作..

    编辑: 啊好吧..这不是真正的“滞后”..背景只是在触摸平台时移回“旧”位置..试试这个:

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

    【讨论】:

    • 我尝试了类似的方法,但是一旦玩家与平台发生碰撞,就会出现延迟。不像内存滞后,当它从平台启动时,它会完全关闭
    • 您能展示代码或您所做的与我的“解决方案”不同的事情吗?
    • 我只是将我的代码替换为您的代码并添加变量。除此之外,我的平台在与 sknode 平台碰撞时会给出“player.physicsbody?.velocity.dy = CGFloat(700)”。
    • 如果没有我的“解决方案”,它不会“落后”吗?如果是这种情况,我不知道它可能是什么.. 每一帧你检查 player.position.y 是否高于前一个 player.position.y .. 如果是 = background/midground/foreground 改变 y位置..如果没有=没有发生..应该没有任何问题..也许其他人有想法?哦.. 你用var PlayerPosition1 = CGFloat() 将 PlayerPosition1 声明为 CGFloat?
    • 是的,我做到了。 dropbox.com/s/7v7ep05yuwe5p16/SwiftJump.xcodeproj.zip?dl=0 这是完整的项目。将您的代码添加到其中
    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多