ios - Swift GameKit 在 Arc 中移动 Sprite

标签 ios swift sprite-kit gamekit

我需要一些帮助来让玩家在我的平台游戏中跳跃。我遇到的问题是他跳过了他不应该做的柱子(我的物理设置正确)。我怀疑这是因为我下面的代码非常低效。我知道有更好的方法可以让他沿着路径移动,但我不确定该怎么做。

var xStep = CGFloat(18)
var yStep = CGFloat(30)

var x = CGFloat(playerJump.position.x)
var y = CGFloat(playerJump.position.y)

let follow = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y += yStep
let follow2 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y += yStep
let follow3 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y += yStep
let follow4 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y += yStep
let follow5 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y -= yStep
let follow6 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y -= yStep
let follow7 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y -= yStep
let follow8 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)
x += xStep
y -= yStep
let follow9 = SKAction.move(to: CGPoint(x: x, y:y), duration: 0.1)

let group = SKAction.group([
    SKAction.repeat(jumpAnimation!, count: 1),
           SKAction.sequence([follow,follow2,follow3,follow4,follow5,follow6,follow7,follow8,follow9])
])
if playerJump.parent == nil {
    addChild(playerJump)
}
playerJump.run(SKAction.sequence([group]), completion: {
    self.player.position=self.playerJump.position
    self.playerJump.removeAllActions()
    self.playerJump.removeFromParent()
    self.addChild(self.player)
})

在此先感谢您的帮助。

更新 当我增加 xStep 中的值时,问题就开始了。角色跳跃的总距离将使其越过柱子。这就是为什么我认为我的代码有问题。这是正在发生的事情的视频。 Jumping Video

更新 2 这是我的最新代码,它仍然把玩家放在柱子的另一边。

@objc func jumpTapped() {
    var xStep = CGFloat(18)
    let yStep = CGFloat(12)

    jumping=true
    xStep = player.xScale * xStep
    var x = player.position.x
    var y = player.position.y


    var moveAnimation = [SKAction]()

    for i in 0...8 {
        x += xStep
        if i < 5 {
            y += yStep
        } else {
            y -= yStep
        }
        let move = SKAction.move(to: CGPoint(x: x, y: y), duration: 0.1)
        moveAnimation.append(move)
    }

    let group = SKAction.group([jumpAnimation!, SKAction.sequence(moveAnimation)])

    player.physicsBody?.affectedByGravity = false
    player.removeAllActions()
    player.run(group) {
        self.endJump()
    }
}

func endJump() {
    player.removeAllActions()
    player.physicsBody?.affectedByGravity = true
    player.run(SKAction.repeatForever(self.playerAnimation!))
    jumping=false
}

最佳答案

看起来你正在将 player.position 设置为 self.playerJump.position

这可能是你的问题,它跳出了柱子所在的地方。你也可以说 player.position.x = player.position.x + 500 这会让你在柱子的另一边同时忽略任何物理。

为什么要添加 Sprite 并在其上运行 Action ?为什么不直接在玩家 Sprite 上运行动画和 Action ?

就清理代码而言,您可以试试这个。您还可以做其他事情,例如让角色沿着贝塞尔曲线路径跳跃,但这可能适合您正在做的事情。但我非常怀疑这会对您的玩家跳过柱子的问题产生任何影响。

一些小花絮给你。

Sprite 位置已经是 CGFloat 不需要转换它们

简写完成代码语法是 object.run(someAction) {//完成后做某事 }

运行重复为 1 的 Action 是没有意义的

运行单个 Action 的序列 (SKAction.sequence([group])) 也是毫无意义的

var xStep = CGFloat(18)
var yStep = CGFloat(30)

var x = playerJump.position.x
var y = playerJump.position.y
var moveAnimation: [SKAction]!

for _ in 0...9 {
    x += xStep
    y += yStep
    moveAnimation.append(SKAction.move(to: CGPoint(x: x, y: y), duration: 0.1))
}

let group = SKAction.group([jumpAnimation, SKAction.sequence(moveAnimation)])

guard playerJump.parent else { addChild(playerJump)}

playerJump.run(group) {
    self.player.position = self.playerJump.position
    self.playerJump.removeAllActions()
    self.playerJump.removeFromParent()
    self.addChild(self.player)
}

Edit try adding the below function to your code

func didBegin(_ contact: SKPhysicsContact) {

    let contactAName = contact.bodyA.node?.name
    let contactBName = contact.bodyB.node?.name

    if (contactAName == "pillar") || (contactBName == "pillar") {
        //assuming only player can hit pillar so don't need to check if one of the contacts is player
        endJump()
    }
}

关于ios - Swift GameKit 在 Arc 中移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48269244/

相关文章:

swift - Spritekit SKShapeNode 随机缩放,Swift

swift - 如何将 child SKSpriteNode 定位在他们的 parent 里面

iOS 将 IP 地址转换为整数并向后转换

ios - 使用 Storyboard的 UIPageViewController 示例

ios - 检索失败后获取 JSON 数据

ios - 等待多个 Alamofire 请求完成

ios - 如何根据 Swift 中的条件返回不同枚举的数组?

swift - 尾随闭包语法如何与 PublicSubject<Void>.subscribe 一起使用?

ios - 修复 NSInvalidArgumentException : *** setObjectForKey: object cannot be nil (key: updated_objectIDs)

ios - 节点多于我应有的