2015-11-29 28 views
0

我正在製作遊戲,並且正在製作自定義轉場。過渡基本上每個Sprite Node都會找到最近的一側,無論是向左還是向右。如果它水平居中,那麼它會找到最近的邊緣,無論是頂部還是底部。 Sprite節點然後將移動到那邊的邊緣。如果它完全居中,那麼它的尺寸會縮小。在所有東西都離開後,場景被改變,下一個場景漸漸消失。場景中有八個項目。背景,標題,播放按鈕,幫助按鈕,選項按鈕,排行榜按鈕和商店按鈕。當我點擊場景中的任何地方時,過渡開始。我還沒有建立在新的場景來在這部分代碼至今:當我在自定義轉換中使用它時,SKAction不起作用

import SpriteKit 

enum transitionTypes { 
    case SlideOutside 
    case Fade 
} 

enum slideDirection { 
    case Left 
    case Right 
    case Up 
    case Down 
    case Shrink 
} 

func createTransition(transitionFrom currentScene: SKScene, to futureScene: SKScene, withTransition transition: transitionTypes) { 

    print("Started Transition") 

    switch transition { 

    case .SlideOutside: 

     print("Nodes Currently in Scene: \(currentScene.children)") 
     print("") 

     for child in currentScene.children as [SKNode] { 

      print("Started Transition For Node \(child.name)") 

      //This will keep the background from being effected 
      if child.name != "background" { 

       //Variable for which direction it should slide to 
       var direction : slideDirection = .Shrink 

       //Determines where the node should slide to 
       if child.position.x < currentScene.frame.size.width/2 { 
        direction = .Left 
       } else if child.position.x > currentScene.frame.size.width/2 { 
        direction = .Right 
       } else if child.position.x == currentScene.frame.size.width/2 { 
        if child.position.y < currentScene.frame.size.height/2 { 
         direction = .Down 
        } else if child.position.y > currentScene.frame.size.height/2 { 
         direction = .Up 
        } else if child.position.y == currentScene.frame.size.height/2 { 
         direction = .Shrink //Skrink will keep its position the same but have its size shrink, instead 
        } 
       } 

       print("Determined Direction To Slide To") 

       let slideAction : SKAction 
       //Slides the node in the direction specified 
       switch direction { 
       case .Left: 
        slideAction = SKAction.applyImpulse(CGVectorMake(-25, 0), duration: 1.5) 
        child.runAction(slideAction) 
        break 
       case .Right: 
        slideAction = SKAction.applyImpulse(CGVectorMake(25, 0), duration: 1.5) 
        child.runAction(slideAction) 
        break 
       case .Up: 
        slideAction = SKAction.applyImpulse(CGVectorMake(0, 25), duration: 1.5) 
        child.runAction(slideAction) 
        break 
       case .Down: 
        slideAction = SKAction.applyImpulse(CGVectorMake(0, -25), duration: 1.5) 
        child.runAction(slideAction) 
        break 
       default: //The default is Shrink 
        break 
       } 

       print("Added SKAction to Node") 

      } else { 
       print("Excluded Background Properly") 
      } 

      print("Finished Transition For Node \(child.name)") 
      print("") 
     } 

     print("Finished Transition") 

     break 

    default: //The Default will be Fade 
     break 
    } 
} //This Code is Copyrighted 

這裏是控制檯日誌:

Started Transition 
Nodes Currently in Scene: [<SKSpriteNode> name:'background' texture:[<SKTexture> 'Background 1' (2497 x 2497)] position:{187.5, 333.5} scale:{1.00, 1.00} size:{375, 667} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'title' texture:[<SKTexture> 'Title' (912 x 399)] position:{187.5, 500.25} scale:{1.00, 1.00} size:{284, 124} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'playButton' texture:[<SKTexture> 'Play Button' (311 x 312)] position:{187.5, 166.75} scale:{1.00, 1.00} size:{100, 100} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'optionsButton' texture:[<SKTexture> 'Options Button' (311 x 312)] position:{75, 216.75} scale:{1.00, 1.00} size:{75, 75} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'shopButton' texture:[<SKTexture> 'Shop Button' (311 x 311)] position:{300, 216.75} scale:{1.00, 1.00} size:{75, 75} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'helpButton' texture:[<SKTexture> 'Help Button' (311 x 311)] position:{75, 116.75} scale:{1.00, 1.00} size:{75, 75} anchor:{0.5, 0.5} rotation:0.00, <SKSpriteNode> name:'leaderboardButton' texture:[<SKTexture> 'Leaderboard Button' (311 x 312)] position:{300, 116.75} scale:{1.00, 1.00} size:{75, 75} anchor:{0.5, 0.5} rotation:0.00] 

Started Transition For Node Optional("background") 
Excluded Background Properly 
Finished Transition For Node Optional("background") 

Started Transition For Node Optional("title") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("title") 

Started Transition For Node Optional("playButton") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("playButton") 

Started Transition For Node Optional("optionsButton") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("optionsButton") 

Started Transition For Node Optional("shopButton") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("shopButton") 

Started Transition For Node Optional("helpButton") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("helpButton") 

Started Transition For Node Optional("leaderboardButton") 
Determined Direction To Slide To 
Added SKAction to Node 
Finished Transition For Node Optional("leaderboardButton") 

Finished Transition 

所有控制檯日誌表明,轉型進行得很完美。所以我認爲這個問題與SKAction有關,請問有人能幫我找到問題所在。謝謝!

+0

請您詳細說明究竟發生了什麼?做所有這些'SKNode'都有物理實體嗎? – Gliderman

+0

我有8個SKSpriteNodes,他們沒有物理實體。我將在幾分鐘內提供場景的代碼。 – grahamcracker1234

回答

1

當你撥打:

slideAction = SKAction.applyImpulse(CGVectorMake(0, -25), duration: 1.5) 

你施加力的SKNode的物理身體。你提到他們沒有物理組織,這意味着他們不會受到衝動的「推動」。

我會推薦使用moveBy()來代替。這會通過設置他們的position來移動你的精靈,而不是通過推動。這方面的一個例子是:

slideAction = SKAction.moveBy(CGVector(dx: 0, dy: -25), duration: 1.5) 

我會建議撞了dy雲量,使所有節點都可以被完全移出了屏幕。你需要看多少,因爲我不知道你的場景的大小。

+0

謝謝你這個工作...種。它解決了我的問題,但是一旦我運行它就會崩潰。所以我改變了重力爲0.儘管其中一些仍然下降並稍微旋轉,但是可能會對它們產生什麼反應? – grahamcracker1234

+0

沒關係,我給了背景一個物理體 – grahamcracker1234

+0

@ grahamcracker1234我很高興它爲你工作。 – Gliderman

相關問題