【问题标题】:Locking scene to a sprite node swift xcode将场景锁定到精灵节点swift xcode
【发布时间】:2015-03-16 17:44:55
【问题描述】:

基本上,我想移动角色(玩家)并让“相机”或场景在世界中跟随他们。问题是 Apple/xcode 没有内置相机节点,如果我尝试移动场景,我会收到一条错误消息,说“SKScene 无法移动”。我不想只是移动背景并将播放器锁定到位,因为我希望能够对播放器节点施加力。我已访问 Apple 网站的高级场景处理页面并按照示例进行操作,但它不起作用。我只需要朝着正确的方向轻推。谢谢!

代码:

(我删除了一些不相关的代码)

class PlayScene: SKScene, SKPhysicsContactDelegate {

let world = SKNode()

let player = SKShapeNode(circleOfRadius: 25)

var cannonRotation = 0.0
var fired = false

override func didMoveToView(view: SKView) {

    physicsWorld.contactDelegate = self

    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    self.addChild(world)

    //Adds the background to the world

    //Adds the elements to the scene
    player.fillColor = UIColor.whiteColor()
    player.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMinY(self.frame)+65+25)
    player.zPosition = 2
    player.physicsBody = SKPhysicsBody(circleOfRadius: 25)
    player.physicsBody?.dynamic = true
    player.physicsBody?.restitution = 0.9
    player.physicsBody?.categoryBitMask = BodyType.player.rawValue

    //Adds the physics properties to GameScene
    let physicsGround = SKPhysicsBody(edgeFromPoint: CGPoint(x: CGRectGetMinX(self.frame), y: CGRectGetMinY(self.frame)+65), toPoint: CGPoint(x: CGRectGetMaxX(self.frame), y: CGRectGetMinY(self.frame)+65))
    self.physicsBody = physicsGround

}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

    powerLevel()
    totalPower = powerReached * playCannon.powerMultiplier

    self.addChild(player)
    player.physicsBody?.applyImpulse(CGVector(dx: CGFloat(Float(totalPower)*cosf(Float(cannonRotation))), dy: CGFloat(Float(totalPower)*sinf(Float(cannonRotation)))))

    fired = true

}

【问题讨论】:

    标签: ios iphone swift sprite-kit


    【解决方案1】:

    重写 didsimulatePhysics,并在该函数中调用此

    self.centerOnNode(player);
    

    这是做什么的,它调用我们稍后将创建的函数,并发送一个 SKNode 的参数,在您的情况下它是 player 节点。接下来创建一个名为centerOnNede的方法并添加以下参数

    func centerOnNode(node: SKNode) {
    
    }
    

    在centerOnNode 函数中添加以下代码行。

     var cameraPosition: CGPoint = node.scene?.convertPoint(node.position, fromNode: node.parent!)
        cameraPosition.x = 0
    
        let posX = node.parent?.position.x
        let posY = node.parent?.position.y
    
        node.parent?.position = CGPointMake(posX! - cameraPosition.x, posY! - cameraPosition.y)
    

    这将使相机以节点为中心并更新其位置。从这里转换的代码。 How to make camera follow SKNode in Sprite Kit?

    【讨论】:

      【解决方案2】:

      Apple Adventure 参考游戏中有how to do this 的详细示例。它有 Swift 和 Objective-C 的例子。

      总的来说,他们推荐的是你

      1. 将所有节点放在后台节点 (SKNode) 容器中。
      2. 实现 didSimulatePhysics 并将世界移动到与玩家相反的方向。这将使播放器保持在屏幕中间。如果您希望相机撞到您的世界边缘,则需要特殊的逻辑。

      相机节点会很有帮助,但尚未在 SpriteKit 中实现。

      【讨论】:

        【解决方案3】:

        从 iOS 9 开始,现在有一个 SKCameraNode 可以为您完成所有繁重的工作。

        只需在您的 didMoveToView 函数中添加这三行代码:

            let cameraNode = SKCameraNode()
            self.addChild(cameraNode)
            self.camera = cameraNode
        

        然后在您的 didFinishUpdate 函数中,只需将 cameraNode 的位置设置为您希望它跟随的任何 SKSpriteNode。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-02
          相关资源
          最近更新 更多