【问题标题】:Sharing highscore with social media与社交媒体分享高分
【发布时间】:2014-12-23 13:15:19
【问题描述】:

我希望当玩家完成关卡后,他可以单击一个按钮(shareButton),这样会弹出一个 UIActivityviewcontroller,玩家可以在其中与社交媒体分享其高分。

但是我的问题是,按钮在GameScene中,但是UIActivityviewcontroller只能在GameViewController中调用..我该怎么做呢?


游戏从 GameMenuScene 开始,点击播放时会移动到 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)
            }
}

游戏场景:

这是 GameViewController(如您所见,第一个场景是 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
}
}

这里是检测到分享按钮触摸的 GameScene 部分:

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

【问题讨论】:

    标签: swift uiviewcontroller sprite-kit social-networking uiactivityviewcontroller


    【解决方案1】:

    这对我有用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 2015-03-21
      • 2012-08-19
      • 1970-01-01
      • 2016-07-22
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多