从本质上讲,我正在制作一款跳台游戏。当玩家与平台碰撞时,玩家会在 y 轴上受到向上的冲击。我得到的下面的代码模拟了跟随玩家的 View 。当玩家在 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))
}
最佳答案
您可以查看玩家位置。
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)))
}
}
关于ios - 仅当玩家在 Y 轴上向上时移动地面视差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894563/