2016-04-14 31 views
1

我想創建一個馬里奧風格的旋轉平臺,保持水平。斯威夫特SpriteKit馬里奧風格旋轉平臺,保持水平

enter image description here

我迄今所做的是創建一個簡單的類用於測試目的。

class PlatformRound: SKNode { 

    let platform: Platform 

// MARK: - Init 
init(barSize: CGSize, color: SKColor, pos: CGPoint) { 

    /// Base 
    let base = SKShapeNode(circleOfRadius: 6) 
    //base.name = Platform.Name.normal 
    base.fillColor = SKColor.darkGrayColor() 
    base.strokeColor = base.fillColor 
    base.position = pos 
    let rotatingAction = SKAction.rotateByAngle(CGFloat(-M_PI), duration: 8) 
    base.runAction(SKAction.repeatActionForever(rotatingAction)) 

    /// Bar 
    let bar = Platform(size: barSize, color: color, pos: CGPointMake(0, 0 - (barSize.height/2)), ofType: .Normal) 
    bar.zPosition = -200 

    /// Platform that supposed to stay horizontal 
    let platformSize = CGSizeMake(40, GameplayConfig.World.platformHeight) 
    let platformPos = CGPointMake(0, 0 - (bar.size.height/2)) 
    platform = Platform(size: platformSize, color: color, pos: platformPos, ofType: .Normal) 

    super.init() 

    addChild(base) 
    base.addChild(bar) 
    bar.addChild(platform) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 
} 

我正在創建一個我可以旋轉的roundBase。我比創建一個從基地落下的酒吧,這是添加到基節點。最後,我創建了一個應該始終保持水平的平臺。 我正在使用另一個Platform子類來創建欄和平臺,但它們與此問題無關。

如何使平臺保持水平。我嘗試了下面的工作。

1)在我gameScene更新我不斷更新的平臺位置或zRotation

platformRound.platform.zRotation = ... 

2)創建一旦平臺被添加時設置比使用性能不斷更新zRotation一個zRotation屬性。

3)試圖玩弄physicsJoints

我肯定有,我缺少一個簡單的方法。我將不勝感激任何幫助。

+0

你在說這個https://www.youtube.com/watch?v=Igjf-W87bW4還是? – Whirlwind

+0

嘿,謝謝你的回覆。那正是我所說的。 – crashoverride777

+0

這應該是*容易*。儘管如此,如果添加創建PlatformRound實例的代碼部分以及圍繞圓周旋轉的代碼(如果你這樣做的話),這將會很有幫助。或者我可以用'SKSpriteNodes'來給你一個簡單的例子,但你可能需要修改它以適應你當前的設置。 – Whirlwind

回答

2

這應該工作:

class GameScene: SKScene{ 

    override func didMoveToView(view: SKView) { 

     backgroundColor = .blackColor() 

     let centerSprite = SKSpriteNode(color: .whiteColor(), size: CGSize(width: 10, height: 10)) 

     centerSprite.zPosition = 3 

     let platform = SKSpriteNode(color: .orangeColor(), size: CGSize(width: 70, height: 20)) 
     platform.zPosition = 2 
     platform.name = "platform" 

     let container = SKNode() 
     container.position = CGPoint(x: frame.midX, y: frame.midY) 

     container.addChild(centerSprite) //Just for debugging 
     container.addChild(platform) 
     let radius = 120 

     let chain = SKSpriteNode(color: .grayColor(), size: CGSize(width: 3, height: radius)) 
     chain.position = CGPoint(x: 0, y: radius/2) 
     container.addChild(chain) 


     platform.position = CGPoint(x: 0, y: radius) 

     let rotatingAction = SKAction.rotateByAngle(CGFloat(-M_PI), duration: 8) 
     container.runAction(SKAction.repeatActionForever(rotatingAction), withKey: "rotating") 

     addChild(container) 

    } 

    override func didEvaluateActions() { 

     self.enumerateChildNodesWithName("//platform") { node,stop in 

      if let parent = node.parent{ 
       node.zRotation = -parent.zRotation 
      } 
     } 
    } 
} 

我所做的,就是我已經加入平臺節點到包含節點和應用旋轉該容器。稍後,在didEvaluateActions中,我調整了平臺的旋轉角度(使其父級的zRotation具有負值)。就是這樣。

這是我所看到的結果:

Rotating Platform

需要的調整,否則該平臺將最終達到與其父一同旋轉(注意如何白色,中心sprite時隨容器節點一起旋轉)。