【问题标题】:Xcode Swift SpriteKit SkSpriteNode Y value isn't moving but X isXcode Swift SpriteKit SkSpriteNode Y 值没有移动,但 X 是
【发布时间】:2017-02-09 15:48:06
【问题描述】:

在移动动作中,Y 值没有变化,我不知道为什么,因为 X 值在变化。 changeY 变量使用正确的 5.0 或 -5.0 值完成其工作,但“玩家”SkSpriteNode 上的 Y 值没有改变。请帮忙。

 override func update(_ currentTime: TimeInterval) {

    let percent = touchLocation.x / size.width
    let newAngle = percent * 180 - 180
    print(newAngle)
    if playButtonPressed{

        var changeY = 0.0
        if newAngle >= -180{
            changeY = 5.0
        }
        else{
            changeY = -5.0
        }
        player.zRotation = CGFloat(newAngle) * CGFloat(M_PI) / 180.0
        print(changeY)
        var move = SKAction.moveBy(x: -(player.position.x+1), y: CGFloat(changeY), duration: 2000.0)
        player.run(move)
    }
}

【问题讨论】:

    标签: swift xcode sprite skaction


    【解决方案1】:

    我并不完全清楚你的最终目标是什么,但你的计算似乎是这样的。尤其是运动持续时间为 2000 秒这一事实。超过 30 分钟(!) x 计算还表明您实际上计划使用 moveTo 而不是 moveBy,就像您目前正在做的那样。

    以下内容基于您的代码,但由于我不确定您的实际目标是什么触摸在屏幕的右侧...

    override func update(_ currentTime: TimeInterval) {
        if playButtonPressed {
            let percent = touchLocation.x / size.width
            let newAngle = percent * 180 - 180
            player.zRotation = CGFloat(newAngle) * CGFloat(M_PI) / 180.0
    
            let moveRightAndUp = touchLocation.x>size.width/2
            let changeY: CGFloat = moveRightAndUp ? 5.0 : -5.0
            let changeX: CGFloat = moveRightAndUp ? 1.0 : -1.0
            let move = SKAction.moveBy(x: changeX, y: changeY, duration: 1/60)
            player.run(move)
        }
    }
    

    【讨论】:

    • 好的,所以当我添加 y 开始工作时,但我想知道是否有办法让“播放器”节点以恒定的速度移动,它只是朝 newAngle 的方向移动。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多