我创建了分享按钮,用于在Facebook,Twitter等分享我的分数。 在GameViewController中创建的按钮以及我在游戏场景中创建的得分和高分。
我确实分享了分数,但我不知道如何将我的分数从GameScene转移到GameViewController。
游戏场景中的得分和高分:
func addPointsLabels() {
pointsLabel = MLPointsLabel(num: 0)
pointsLabel.fontColor = UIColor.whiteColor()
pointsLabel.fontName = "Avenir Next Bold"
pointsLabel.fontSize = 40.0
pointsLabel.position = CGPointMake(self.view!.frame.size.width/2, self.view!.frame.size.height/2 + 180)
pointsLabel.name = "pointsLabel"
addChild(pointsLabel)
highscoreLabel = MLPointsLabel(num: 0)
highscoreLabel.fontColor = UIColor(red: 0.0/255.0, green: 161.0/255.0, blue: 156.0/255.0, alpha: 1.0)
highscoreLabel.name = "highscoreLabel"
highscoreLabel.fontSize = 25.0
highscoreLabel.fontName = "Avenir Next Bold"
highscoreLabel.position = CGPointMake(self.view!.frame.size.width/2 + 90, self.view!.frame.size.height/2 + 100)
addChild(highscoreLabel)
}
GameViewController:
class scene: SKScene {
var currentScore: Int = 0
var highScore: Int = 0
func updateScore(withScore score: Int) {
currentScore = score
highScore = currentScore > score ? currentScore : score
}
}
class GameViewController: UIViewController , MyGameDelegate {
var scene: GameScene!
var ShareButton = UIButton()
var myDelegate : MyGameDelegate!
override func viewDidLoad() {
super.viewDidLoad()
// Configure the view
let skView = view as! SKView
//skView.multipleTouchEnabled = false
// Create and configure the scene
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
// NSLog("width: %f", skView.bounds.size.width)
// NSLog("height: %f", skView.bounds.size.height)
// Present the scenee
skView.presentScene(scene)
}
func addShareButton() {
ShareButton.hidden = false
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// //create Share Button
ShareButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 - 80, self.view!.frame.size.height/2 + 60, 60, 60))
ShareButton.setImage(UIImage(named: "ShareButton.png"), forState: UIControlState.Normal)
ShareButton.addTarget(self, action: "pressedShareButton:", forControlEvents: .TouchUpInside)
self.view!.addSubview(ShareButton)
}
func pressedShareButton(sender: UIButton!) {
// Now you can get your score and high score like this:
let currentScore = scene.pointsLabel
let highScore = scene.highscoreLabel
UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
view!.drawViewHierarchyInRect(view!.frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
let myText = "WOW! I made \(currentScore) points playing #RushSamurai! Can you beat my score? https://itunes.apple.com/us/app/rush-samurai/id1020813520?ls=1&mt=8"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [myText,image], applicationActivities: nil)
//New Excluded Activities Code
if #available(iOS 9.0, *) {
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
} else {
// Fallback on earlier versions
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
}
activityVC.popoverPresentationController?.sourceView = view
activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
presentViewController(activityVC, animated: true, completion: nil)
}
MLPointsLable:
class MLPointsLabel: SKLabelNode {
var number = 0
//var gameoverscore = 0
init(num: Int) {
super.init()
fontColor = UIColor.whiteColor()
fontName = "Avenir Heavy"
fontSize = 24.0
number = num
text = "\(num)"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func increment() {
number++
text = "\(number)"
}
func setTo(num: Int) {
self.number = num
text = "\(self.number)"
}
答案 0 :(得分:2)
由于您的GameViewController正在展示您的GameScene,您可以只保留对它的引用,并从GameScene中的属性中获得分数和高分。
这样的事情:
class GameViewController: UIViewController {
var gameScene: GameScene!
override func viewDidLoad() {
super.viewDidLoad()
// Hold a reference to your GameScene after initializing it.
gameScene = SKScene(...)
}
}
class GameScene: SKScene {
var currentScore: Int = 0
var highScore: Int = 0
func updateScore(withScore score: Int) {
currentScore = score
highScore = currentScore > score ? currentScore : score
}
}
<强>更新强>
您可以在pressedShareButton
中使用此值:
func pressedShareButton(sender: UIButton!) {
let currentScore = scene.currentScore
let highScore = scene.highScore
...
let myText = "WOW! I made \(currentScore) points playing #RushSamurai! Can you beat my score? https://itunes.apple.com/us/app/rush-samurai/id1020813520?ls=1&mt=8"
...
}