当我在自定义转换中使用 SKAction 时它不起作用

SKAction not working when I use it in a custom transition

我正在制作游戏,并且正在制作自定义过渡。 Transition 基本上是每个 Sprite 节点都会找到最近的一侧,无论是左侧还是右侧。如果它是水平居中的,那么它会找到最近的边缘,顶部或底部。然后 Sprite 节点将从边缘移到那一侧。如果它完全居中,那么它的尺寸会缩小。一切都离开场景后,场景发生变化,下一个场景淡入。场景中有八个项目。背景、标题、播放按钮、帮助按钮、选项按钮、排行榜按钮和商店按钮。当我单击场景中的任意位置时,过渡开始。我还没有创建新场景出现的部分。到目前为止这段代码:

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 有关,有人可以帮我找出问题所在。谢谢!

当你打电话时:

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

您正在对 SKNode 的物理 body 施加力。你提到他们没有物理身体,这意味着他们不会"pushed"被冲动

我建议改用 moveBy()。这将通过设置 position 来移动您的精灵,而不是通过推动。这方面的一个例子是:

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

我建议增加 dy 的数量,这样所有节点都可以完全移出屏幕。你需要看看有多少,因为我不知道你场景的大小。