繁体   English   中英

如何使用Swift 2.0和Sprite Kit在Facebook上分享分数

[英]How to share score on Facebook using Swift 2.0 and Sprite Kit

这段代码可以正常运行,但是在GameOver场景中,它不会显示“共享到Facebook”按钮。 帮助将不胜感激。

import Foundation
import SpriteKit
import Social
import UIKit
import GameKit
import iAd
import AudioToolbox

class GameOver: SKScene {

let won:Bool

init(size: CGSize, won: Bool) {
    self.won = won
    super.init(size: size)
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

@IBOutlet weak var shareImg: UIImageView!

override func didMoveToView(view: SKView) {

    var background: SKSpriteNode
    if (won) {
        background = SKSpriteNode(imageNamed: "YouWin")
        runAction(SKAction.sequence([
            SKAction.waitForDuration(0.1),
            SKAction.playSoundFileNamed("win.wav",
                waitForCompletion: false)
            ]))
    } else {
        background = SKSpriteNode(imageNamed: "background1")

        var youLose: SKLabelNode!

        youLose = SKLabelNode(fontNamed: "Chalkduster")
        youLose.text = "Merry Christmas!"
        youLose.fontSize = 100
        youLose.fontColor = SKColor.redColor()
        youLose.horizontalAlignmentMode = .Left
        youLose.position = CGPoint(x: 550, y: 950)
        youLose.zPosition = 1
        addChild(youLose)

        scoreLabel = SKLabelNode(fontNamed: "Helvatica")
        scoreLabel.text = "You have \(score) Presents collected!"
        scoreLabel.fontSize = 60
        scoreLabel.fontColor = SKColor.redColor()
        scoreLabel.horizontalAlignmentMode = .Right
        scoreLabel.position = CGPoint(x: 1400, y: 750)
        scoreLabel.zPosition = 100
        addChild(scoreLabel)

        runAction(SKAction.sequence([
            SKAction.waitForDuration(0.2),
            SKAction.playSoundFileNamed("lose.mp3",
                waitForCompletion: false)
            ]))


    }

    background.position =
        CGPoint(x: self.size.width/2, y: self.size.height/2)
    self.addChild(background)

    let wait = SKAction.waitForDuration(7.0)
    let block = SKAction.runBlock {
        let myScene = GameScene(size: self.size)
        myScene.scaleMode = self.scaleMode
        let reveal = SKTransition.flipHorizontalWithDuration(0.5)
        self.view?.presentScene(myScene, transition: reveal)
        score = 0
    }
    self.runAction(SKAction.sequence([wait, block]))

}


// FACEBOOK BUTTON
@IBAction func facebookButt(sender: AnyObject) {

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let viewController = appDelegate.window!.rootViewController

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
        let fb = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        fb.setInitialText("I have collected \(score) presents!")
        fb.addImage(shareImg.image)
        viewController!.presentViewController(fb, animated: true, completion: nil)

    }else {
        let alert = UIAlertController(title: "Facebook",
            message: "Please login to your Facebook account in Settings",
            preferredStyle: .Alert)

        let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alert.addAction(action)
        viewController!.presentViewController(alert, animated: true, completion: nil)
    }
}


}

由于Facebook按钮只能添加到UIView ,因此您需要将其添加到SKView GameOver场景的GameOver中。

在didMoveToView()函数中,您可以执行以下操作:

let facebookButton = // setup button here. Set frame etc.
view.addSubview(facebookButton)

每个SKScene都有对其包含的视图的引用。

调用您创建的操作:

facebookButton.addTarget(self, action: "facebookButt:", forControlEvents: UIControlEvents.TouchUpInside)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM