gpt4 book ai didi

swift - 与社交媒体分享高分

转载 作者:行者123 更新时间:2023-11-30 10:19:49 25 4
gpt4 key购买 nike

我希望当玩家完成关卡后,他可以单击一个按钮(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()
}
}
}
}
}

最佳答案

这对我有用:

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

关于swift - 与社交媒体分享高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27621157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com