grahamcracker1234
grahamcracker1234

Reputation: 609

SKAction not working when I use it in a custom transition

I am making a game, and I am working on making a custom transition. The Transition is basically each Sprite Node will find the nearest side, either left or right. If it is horizontally centered then it finds the nearest edge, either top or bottom. The Sprite Node will then move off the edge to that side. If it is perfectly centered then it will shrink in size. After everything is moved off the scene is changed and the next scene fades in. There are eight items in the scene. The background, title, play button, help button, options button, leaderboard button, and a shop button. When I click anywhere in the scene the transition starts. I haven't created the part where the new scene comes in. This code so far:

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

Here are the console logs:

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

All of the console logs show that the transition went perfectly. So I am assuming that the problem has something to do with the SKAction, could someone please help me find what is wrong. Thanks!

Upvotes: 0

Views: 105

Answers (1)

Gliderman
Gliderman

Reputation: 1205

When you call:

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

You are applying a force to the physics body of the SKNode. You mentioned that they did not have physics bodies, which means they do not get "pushed" by the impulse.

I would recommend using moveBy() instead. This will move your sprites by setting their position, not by pushing. An example of this would be:

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

I would recommend bumping up the amount dy goes, so that all nodes can be completely moved off the screen. You will need to see how much, as I don't know the size of your scene.

Upvotes: 1

Related Questions