Sharing highscore with social media

浪尽此生 提交于 2019-12-06 15:38:15

问题


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:


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)
      }


来源:https://stackoverflow.com/questions/27621157/sharing-highscore-with-social-media

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!