与社交媒体分享高分 [英] Sharing highscore with social media
问题描述
我希望当玩家完成关卡后,他可以点击一个按钮(shareButton),这样一个 UIActivityviewcontroller 就会弹出,玩家可以在其中与社交媒体分享其高分.
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.
但我的问题是,按钮在 GameScene 中,而 UIActivityviewcontroller 只能在 GameViewController 中调用..我该怎么做?
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?
游戏从 GameMenuScene 开始,点击播放时会移动到 GameScene:
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)
}
}
游戏场景:
这是 GameViewController(如您所见,第一个场景是 GameMenuScene:
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
}
}
这里是 GameScene 中检测到共享按钮触摸的部分:
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()
}
}
}
}
}
推荐答案
这对我有用:
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)
}
这篇关于与社交媒体分享高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!