gpt4 book ai didi

ios - 如何将分数从游戏场景传输到 GameViewController

转载 作者:行者123 更新时间:2023-11-29 10:20:39 25 4
gpt4 key购买 nike

我创建了分享按钮,用于在 Facebook 、推特等平台上分享我的分数。在 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)

}

游戏 View Controller :

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

最佳答案

由于您的 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"
...

关于ios - 如何将分数从游戏场景传输到 GameViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35536235/

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