为什么精灵节点的位置不会改变?

时间:2014-12-03 06:26:51

标签: sprite-kit

我评论了代码。这是我的第一场比赛,所以我的知识非常浅薄。我甚至不知道是否有更好的方法来做到这一点。

现在,我遇到的问题是虽然动画有效,但我打印的位置属性始终为0.为什么?我的想法是在更新方法中,我将检查位置属性,如果它在屏幕上方某个高度以上,我会将顶部精灵移动到底部(就像一个队列数据结构 - 从一端拔出,添加到另一端)并调整中间为顶部和底部为中间。这样,我只需要3个精灵就可以为数字滚轮设置动画。

override func didMoveToView(view: SKView) {
        /* Idea is to have a scrolling wheel of numbers animate. 
        The approach is to have 3 'number' sprite nodes inside a crop node. Only one sprite number node is visible at a given time
        because of the crop node's mask.
        Initially, number 0, 1 and 2 one under the other and a crop sprite to crop out everything but just one number
        that needs to be displayed. Then, we animate number 1 in and push out number 0 and then animate number 2 in and push
        out number 1. When the position of the sprite containing 0 moves above a certain position 'y', we move it to the bottom
        and have number 4 replace 0 so it can roll in next. So we have a continuous scrolling wheel of numbers */

        //A slot to hold the numbers
        let numSlot = SKSpriteNode(color: SKColor.clearColor(), size: CGSizeMake(100, 180))

        //Create a crop node
        let cn = SKCropNode.init()

        //Add the crop node mask so only 1 number is visible
        cn.maskNode = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 180))

        //Add the number slot sprite to the crop node sprite at a certain position
        numSlot.addChild(cn)
        numSlot.position = CGPointMake(size.width/2, size.height/2 - 25)
        self.addChild(numSlot)

        //The three images that should live inside the numSlot node (with only 1 visible at a given time)
        let zeroSprite = SKSpriteNode.init(imageNamed: "0.png", normalMapped: false)
        let oneSprite = SKSpriteNode.init(imageNamed: "1.png", normalMapped: false)
        let twoSprite = SKSpriteNode.init(imageNamed: "2.png", normalMapped: false)

        //Add the three numbers to the numberHolderSprite and then add the numberHolderSprite to the crop Nod
        let numberHolderSprite = SKSpriteNode.init()
        numberHolderSprite.addChild(zeroSprite)
        oneSprite.position = CGPointMake(0, -120)
        numberHolderSprite.addChild(oneSprite)
        twoSprite.position = CGPointMake(0, -240)
        numberHolderSprite.addChild(twoSprite)


        //Add the number holder sprite to the crop node
        cn.addChild(numberHolderSprite)

        top = zeroSprite
        middle = oneSprite
        bottom = twoSprite

        var numSlotSpriteAction1 = SKAction.moveToY(top!.position.y + 500, duration: 0.5)
        var numSlotSpriteAction2 = SKAction.moveToY(middle!.position.y + 500, duration: 0.5)
        var numSlotSpriteAction3 = SKAction.moveToY(bottom!.position.y + 500, duration: 0.5)

        println("Position \(top!.position.y)")

        var numSlotGroupAction = SKAction.group([numSlotSpriteAction1, numSlotSpriteAction2, numSlotSpriteAction3])
        numberHolderSprite.runAction(numSlotGroupAction)

         println("Position \(top!.position.y)")

        //Just a simple background image
        let bg = SKSpriteNode.init(imageNamed: "bg.png", normalMapped: false)
        bg.position = CGPointMake(size.width/2, size.height/2)
        self.addChild(bg)

}

1 个答案:

答案 0 :(得分:0)

println("Position \(top!.position.y)")

正在执行任何SKAction之前执行。 top实际上是zeroSprite,默认位置为0,0。

但是,即使您的SKActions运行,它也不会影响top。这是因为SKActions在numberHolderSprite节点上运行。这意味着numberHolderSprite的位置将改变,而不是子节点。在处理下一个动画循环之前,该动作也不会运行。

所以我猜这段代码不会做你期望它做的事。