【问题标题】:SKAction not working when I use it in a custom transition当我在自定义过渡中使用 SKAction 时,它不起作用
【发布时间】:2015-11-29 23:08:12
【问题描述】:

我正在制作游戏,并且正在制作自定义过渡。过渡基本上是每个 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有关,有人可以帮我找出问题所在。谢谢!

【问题讨论】:

  • 您能否详细说明到底发生了什么?所有这些SKNodes 都有物理实体吗?
  • 我有 8 个 SKSpriteNode,它们没有物理实体。我将在几分钟内提供场景的代码。

标签: xcode swift sprite-kit skaction


【解决方案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。虽然它们中的一些仍然会下落并轻微旋转,但它们会产生什么反应?
  • 没关系,我给了背景一个物理体
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
相关资源
最近更新 更多