【问题标题】:How to transfer score from Game Scene to GameViewController如何将分数从游戏场景传输到 GameViewController
【发布时间】:2016-02-21 12:40:04
【问题描述】:

我创建了分享按钮,用于在 Facebook、Twitter 等网站上分享我的分数。 在 GameViewController 中创建的按钮和我在 Game Scene 中创建的分数和高分。

我确实分享了分数,但我不知道如何将我的分数从 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)"

}

【问题讨论】:

  • 你能指定 GameViewController 和你的 GameScene 之间的关系吗?您的 GameViewController 是否引用了 GameScene?
  • 是的,GameViewController 展示了我的 GameScene。
  • 我尝试分享我的分数 我在 Twitter 和 Facebook 上获得了游戏,但我不知道如何将分数从 Game Scene 移动到 GameViewController

标签: ios uiviewcontroller sprite-kit swift2


【解决方案1】:

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

}

【讨论】:

  • 好的,谢谢。你知道怎么用 Delegate 做吗?因为在与委托或帖子一起使用时
  • 如果你想回传一些东西(在另一个方向),你需要使用一个委托。例如,如果 GameScene 需要 GameViewController 来做某事,您可以使用委托进行通信,但在这种情况下,通信从 GameViewController 开始,所以它直接从 A -> B 而不是 B -> A,所以恕我直言,委托在这里没有意义。
  • 我明白了,谢谢 :) 但效果不佳,我现在将编辑我的帖子。
  • 如果我想使用我的 vars(积分标签和高分标签)如何将其更改为我需要的?
  • 我看到你更新了你的问题。问题是您访问 pointsLabel 和 highScoreLabel 但您需要从 GameScene 访问 currentScore 和 highScore 属性。这将是 Ints,但您可以通过执行 String(currentScore) 将它们转换为字符串,或将它们附加到另一个字符串,如 "This is an example of \(currentScore) appending"
猜你喜欢
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
相关资源
最近更新 更多