【问题标题】:SpriteKit: Move Whole SceneSpriteKit:移动整个场景
【发布时间】:2016-02-11 18:47:23
【问题描述】:

我想在场景中添加特定数量的方块。当我这样做时,广场的一部分在视野之外。是否可以在 y 轴上移动整个场景?

【问题讨论】:

    标签: swift sprite-kit move scene


    【解决方案1】:

    我试过了,看起来场景本身无法移动。另一种方法是在代表整个世界的场景中添加一个 SKNode。将所有其他元素添加到世界节点。如果要移动整个场景,现在可以移动此节点。我正在我的博客中编写一个简短的教程。这是我当前的代码:

    import SpriteKit
    
    class GameScene: SKScene {
    
    // Declare the needed nodes
    var worldNode: SKNode?
    var spriteNode: SKSpriteNode?
    var nodeWidth: CGFloat = 0.0
    
    override func didMoveToView(view: SKView) {
    
        // Setup world
        worldNode = SKNode()
        self.addChild(worldNode!)
    
        // Setup sprite
        spriteNode = SKSpriteNode(imageNamed: "Spaceship")
        spriteNode?.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
        spriteNode?.xScale = 0.1
        spriteNode?.yScale = 0.1
        spriteNode?.zPosition = 10
        self.addChild(spriteNode!)
    
        // Setup backgrounds
        // Image of left and right node must be identical
        let leftNode = SKSpriteNode(imageNamed: "left")
        let middleNode = SKSpriteNode(imageNamed: "right")
        let rightNode = SKSpriteNode(imageNamed: "left")
    
        nodeWidth = leftNode.frame.size.width
    
        leftNode.anchorPoint = CGPoint(x: 0, y: 0)
        leftNode.position = CGPoint(x: 0, y: 0)
        middleNode.anchorPoint = CGPoint(x: 0, y: 0)
        middleNode.position = CGPoint(x: nodeWidth, y: 0)
        rightNode.anchorPoint = CGPoint(x: 0, y: 0)
        rightNode.position = CGPoint(x: nodeWidth * 2, y: 0)
    
        worldNode!.addChild(leftNode)
        worldNode!.addChild(rightNode)
        worldNode!.addChild(middleNode)
    
    
    }
    
    
    var xOrgPosition: CGFloat = 0
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    
        for touch in touches {
    
            let xTouchPosition = touch.locationInNode(self).x
            if xOrgPosition != 0.0 {
                let xNewPosition = worldNode!.position.x + (xOrgPosition - xTouchPosition)
    
                if xNewPosition <= -(2 * nodeWidth) {
                    // right end reached
                    worldNode!.position = CGPoint(x: 0, y: 0)
                    print("Right end reached")
                } else if xNewPosition >= 0 {
                    // left end reached
                    worldNode!.position = CGPoint(x: -(2 * nodeWidth), y: 0)
                    print("Left end reached")
                } else {
    
                    worldNode!.position = CGPoint(x: xNewPosition, y: 0)
                }
            }
            xOrgPosition = xTouchPosition
        }
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // Reset value for the next swipe gesture
        xOrgPosition = 0
    }
    
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
    

    }

    【讨论】:

    • 会不会像worldNode.addChild(otherNode)一样使用
    • 是的。我可以稍后发布示例
    • 是的,移动场景不是你想要做的(虽然可以)按照场景中所有对象的主节点开始的方法,然后移动主节点在场景内部周围移动所有节点。当您想在游戏中添加 HUD 时,这是一种很好的方法。您可以将主节点添加到场景中,然后将 HUD 节点添加到场景中,这样当您移动主节点时,只有连接到主节点的对象会移动,而 HUD 中的节点将保持静态
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多