Reputation: 283
I created share button for share my score in Facebook , twitter and more. the button created in GameViewController and my score and high score i created in Game Scene.
I did share score but i don't know how to transfer my score from GameScene to GameViewController.
The Score and High Score in Game Scene :
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)"
}
Upvotes: 1
Views: 225
Reputation: 2600
Since your GameViewController is presenting your GameScene you can just hold a reference to it and get the score and high score from properties in your GameScene.
Something like this:
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
}
}
Update:
You could use this values in your pressedShareButton
like this:
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"
...
}
Upvotes: 2