gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:00:46 26 4
gpt4 key购买 nike

这段代码运行没有任何问题,但在 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 中,因此您需要将其添加到负责 GameOverSKView 中> 场景。

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

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

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

调用您创建的操作:

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

关于ios - 如何使用 Swift 2.0 和 Sprite Kit 在 Facebook 上分享分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025229/

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