Sharing highscore with social media

2019-04-17 12:06发布

I Want that when the player is done with the level, he can click a button(shareButton) so an UIActivityviewcontroller pops up where the player can share its highscore with the social media.

But my problem is, the button is in the GameScene, but the UIActivityviewcontroller can only be called in the GameViewController.. how do I do this?


Game starts with GameMenuScene, when play is clicked it will move to GameScene:

class GameMenuScene: SKScene {
            if nodeAtPoint.name == "play"{
                NSUserDefaults.standardUserDefaults().setInteger(score+1, forKey: "currentLevel")
                NSUserDefaults.standardUserDefaults().synchronize()

                self.removeAllActions()
                self.removeAllChildren()
                var scene1:SKScene = GameScene(size: self.size)                         @@Updated : error: SKSCene does not have a member named "weakGameVC"
                self.view?.presentScene(scene1)
            }
}

GameScene:

Here is the GameViewController(as you can see, first scene is GameMenuScene:

import UIKit
import SpriteKit
import AVFoundation
import Social


class GameViewController: UIViewController, UITextFieldDelegate{

var player:SKSpriteNode = SKSpriteNode()
var scene:GameMenuScene!


override func viewDidLoad() {
    super.viewDidLoad()

    let skView = view as SKView
    skView.multipleTouchEnabled = false
    scene = GameMenuScene(size: skView.bounds.size)
    //scene.scaleMode = SKSceneScaleMode.ResizeFill
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.presentScene(scene)
}


 /*
func shareButton()  {
     var myShare = "aa"
     let activityVC:UIActivityViewController = UIActivityViewController(activityItems: ["aa"], applicationActivities: nil)
  presentViewController(activityVC, animated: true, completion: nil)
}
*/

override func prefersStatusBarHidden() -> Bool {
    return true
}
}

And here the part of GameScene where the touch in the share button is detected:

import SpriteKit
import Social


class GameScene: SKScene, SKPhysicsContactDelegate {

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        for touch: AnyObject in touches {

            let location: CGPoint! = touch.locationInNode(self)

            let nodeAtPoint = self.nodeAtPoint(location)

            if (nodeAtPoint.name != nil) {
              if   nodeAtPoint.name == "share"{
                    //shareButton()
}
}
}
}
}

1条回答
Explosion°爆炸
2楼-- · 2019-04-17 12:48

This work for me:

 if   nodeAtPoint.name == "share"{
                var myShare = "My best is \(highScoreText.text)"
                let controller = self.view?.window?.rootViewController as GameViewController

                let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)

                controller.presentViewController(activityVC, animated: true, completion: nil)
      }
查看更多
登录 后发表回答