2014-12-03 30 views
0

我已經評論了代碼。這是我的第一場比賽,所以我的知識很淺。我甚至不知道是否有更好的方法來做到這一點。爲什麼精靈節點的位置不變?

現在,我遇到的問題是,儘管動畫工作,我打印的位置屬性始終是0.爲什麼?我的想法是,在更新方法中,我將檢查position屬性,並且如果它在屏幕上的某個高度以上,我會將頂部精靈移動到底部(如隊列數據結構 - 從一端拉出並添加到另一端),並調整中間爲頂部和底部爲中間。這樣,我只需要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) 

} 

回答

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

被之前的任何SKActions正在運行執行。頂部實際上是zeroSprite,並且默認位置是0,0。

但是,即使您的SKA運行,它也不會影響頂部。這是因爲SKActions在numberHolderSprite節點上運行。這意味着numberHolderSprite的位置將會改變,而不是子節點。在下一個動畫循環處理之前,該動作也不會運行。

所以我猜這個代碼不會做你期望的。

+0

謝謝!你是對的。我在SKActions之後也有一個println(事實上也在更新方法中)。沒有任何數據表明數字的位置在變化。所以一個簡單的問題。我要求SpriteKit移動numberHolderSprite中的數字 - 而不是numberHolderSprite本身。那麼,爲什麼你說numberHolderSprite的位置會改變,而不是它的孩子的位置。 – sat 2014-12-03 16:08:30

+0

沒關係!我想我現在明白了。再次感謝! – sat 2014-12-03 17:03:30