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

标签 xcode swift sprite-kit skaction

我正在制作一款游戏,并且正在制作自定义过渡。 Transition 基本上是每个 Sprite Node 都会找到最近的一侧,无论是左边还是右边。如果它水平居中,那么它会找到最近的边缘,顶部或底部。然后 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 ,这意味着他们不会被冲动“插入”。

我建议使用moveBy()。这将通过设置 Sprite 的位置来移动 Sprite ,而不是通过插入。一个例子是:

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

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

关于xcode - 当我在自定义转换中使用 SKAction 时,SKAction 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989249/

相关文章:

Swift 类继承

swift - 将 SKSpriteNode 移动到触摸的位置

objective-c - 简单的 Twitter 程序无法运行

ios - Xcode 6、iPhone 6 和 iPhone 4s 生成错误

ios - Swift:GM Xcode 6.1 和 AFNetworking AFJSONResponseSerializer

c++ - Xcode 调试器不显示 C++ cout 输出

iOS swift : CGAffineTransformMakeRotation resets between roations

Swift:从泛型调用非泛型函数?

ios - 如何快速裁剪给定角度的图像

iOS SpriteKit - 游戏开始前倒计时?