[英]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.