我正在构建一个平台游戏,它有一个跳到移动平台上的SKSpriteNode角色。
当平台移动时,角色不会移动,最终会从平台上掉下来。如果我点击移动按钮,角色会移动得很好,但我想让角色在移动时‘粘’在平台上。
我还没有发布代码,因为代码正在按预期工作。我有一种感觉,这是我可以设置的属性?
编辑:
我有一个解决方案--只是不确定人们应该怎么做,所以我会在下面发帖,等待反馈。它很长很复杂,可能不需要这么做。
当用户按下向左或向右方向按钮时,我将场景从GameScene.swift移动到:
func moveGround(direction: String) {
if direction == "left" {
[snip...]
// move the platforms
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.position.x += Helper().kMovingDistance
}
}
// move all other nodes
self.enumerateChildNodesWithName("*") {
node, stop in
if let foundNode = node as? SKSpriteNode {
node.position.x += Helper().kMovingDistance
}
}
[snip...]
} else if direction == "right" {
[snip...]
// move the platforms
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.position.x -= Helper().kMovingDistance
}
}
// move all other nodes
self.enumerateChildNodesWithName("*") {
node, stop in
if let foundNode = node as? SKSpriteNode {
node.position.x -= Helper().kMovingDistance
}
}
[snip...]
}这样可以很好地移动场景。然后,当角色落在平台顶部时,我使用SKAction序列启动平台移动,并通过向GameScene发送反向序列来启动场景移动:
func startMoving() {
if !self.isMoving {
[snip...]
// get the platform actions and initiate the movements
let actions = self.getMovements()
let seq:SKAction = SKAction.sequence(actions)
// move the platform
self.runAction(seq, completion: { () -> Void in
self.completedPlatformActions()
})
// get the reverse actions and initiate then on the scene / ground
let reverseActions = self.getReverseMovements()
let reverseSeq:SKAction = SKAction.sequence(reverseActions)
delegate!.moveGroundWithPlatform(reverseSeq)
self.isMoving = true
}
}然后我有了一个功能,可以用来移动平台的地面,并向runAction添加一个键,这样如果用户停止与平台的联系,我就可以停止该操作,而不是平台的操作:
func moveGroundWithPlatform(seq: SKAction) {
[snip...]
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.runAction(seq, withKey: "groundSeq")
}
}
[snip...]
}然后我停止移动场景,但让平台的其余操作继续使用:
func stopMovingGroundWithPlatform() {
[snip...]
self.enumerateChildNodesWithName("platform") {
node, stop in
if let foundNode = node as? PlatformSprite {
node.removeActionForKey("groundSeq")
}
}
[snip...]
}丑陋的我知道-如果其他人有关于如何做得更好的建议,我很想知道:)
发布于 2014-11-26 18:27:47
如果希望摩擦力使角色与平台一起移动,则需要使用力、冲量或通过设置其速度来移动平台。我怀疑通过设置速度来控制平台会更容易一些。您可以通过以下方式在update方法中完成此操作
platform.physicsBody?.velocity = CGVectorMake(dx,dy)其中dx和dy分别控制平台在x和y方向上的速度。您还应该设置平台的物理体的摩擦力属性。
https://stackoverflow.com/questions/27144209
复制相似问题