简体   繁体   中英

Taking and sharing Screenshot using Swift in SpriteKit

I just made a simple game using Swift in SpriteKit. At the end scene, it shows Score, Share and Restart button.

The function for sharing screenshot of the end scene is still pending. I found a very simple solution here, https://www.frispgames.com/sharing-a-screenshot-on-ios-using-swift-and-sprite-kit/

I'm not a pro, therefore all my attempts so far to implement this solution is in vain.

I'm trying to call shareScore() function (as written in that solution) when the player taps shareButton() but it isn't working.

Here's my code for the EndScene

var restartBtn = SKSpriteNode()
var shareBtn = SKSpriteNode()
var scoreLbl = SKLabelNode()
var score = Int()


override func didMoveToView(view: SKView) {

    scene!.scaleMode = .AspectFill

    self.view?.backgroundColor = UIColor.grayColor()

    scoreLabel()

    shareBtn = SKSpriteNode(imageNamed: "Share")
    shareBtn.position = CGPointMake(self.frame.width / 2, self.frame.height / 3)
    shareBtn.setScale(0.0005)
    self.addChild(shareBtn)

    restartBtn = SKSpriteNode(imageNamed: "Restart")
    restartBtn.position = CGPointMake(self.frame.width / 2, self.frame.height / 4)
    restartBtn.setScale(0.0005)

    self.addChild(restartBtn)
}


func scoreLabel() {

    let scoreDefault = NSUserDefaults.standardUserDefaults()
    score = scoreDefault.valueForKey("score") as! NSInteger

    scoreLbl.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    scoreLbl.fontSize = 40
    scoreLbl.setScale(0.001)
    scoreLbl.text = "Score: \(score)"

    self.addChild(scoreLbl)
}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {

        let location = touch.locationInNode(self)

        if restartBtn.containsPoint(location) {

            let gameScene = GameScene(fileNamed: "GameScene")
            let transition = SKTransition.fadeWithDuration(0.5)
            let skView = self.view! as SKView
            skView.ignoresSiblingOrder = true
            gameScene?.scaleMode = .AspectFill
            skView.presentScene(gameScene!, transition: transition)

        } else if shareBtn.containsPoint(location) {

            // Function for sharing screenshot should be called here
        }
    }

}

You didn't show how you are using shareScore() function so I have created one simple example for you from the tutorial you given and it's working fine and below is complete sample code:

import SpriteKit

class GameScene: SKScene {

    let sprite = SKSpriteNode(imageNamed:"Spaceship")

    override func didMoveToView(view: SKView) {

        sprite.xScale = 0.5
        sprite.yScale = 0.5
        sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))

        self.addChild(sprite)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch in touches {

            let location = touch.locationInNode(self)

             if sprite.containsPoint(location) {

                let postText: String = "Check out my score! Can you beat it?"
                let postImage: UIImage = getScreenshot(scene!)
                let activityItems = [postText, postImage]
                let activityController = UIActivityViewController(
                    activityItems: activityItems,
                    applicationActivities: nil
                )

                let controller: UIViewController = scene!.view!.window!.rootViewController!

                controller.presentViewController(
                    activityController,
                    animated: true,
                    completion: nil
                )
            }
        }

    }

    func getScreenshot(scene: SKScene) -> UIImage {
        let snapshotView = scene.view!.snapshotViewAfterScreenUpdates(true)
        let bounds = UIScreen.mainScreen().bounds

        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)

        snapshotView.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)

        let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return screenshotImage;
    }
}

And when you click on Spaceship image you will get result like:

在此输入图像描述

Now you can easily share it.

Sample code for more Info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM