我刚刚在SpriteKit中使用Swift制作了一个简单的游戏。在结束场景,它显示分数,分享和重启按钮。
用于共享结束场景的屏幕截图的功能仍处于待处理状态。我在这里找到了一个非常简单的解决方案https://www.frispgames.com/sharing-a-screenshot-on-ios-using-swift-and-sprite-kit/
我不是专业人士,因此到目前为止我实施此解决方案的所有尝试都是徒劳的。
当玩家点击shareScore()
但它无法正常工作时,我正试图调用shareButton()
函数(如该解决方案中所述)。
这是我的EndScene代码
var restartBtn = SKSpriteNode()
var shareBtn = SKSpriteNode()
var scoreLbl = SKLabelNode()
var score = Int()
override func didMoveToView(view: SKView) {
scene!.scaleMode = .AspectFill
self.view?.backgroundColor = UIColor.grayColor()
scoreLabel()
shareBtn = SKSpriteNode(imageNamed: "Share")
shareBtn.position = CGPointMake(self.frame.width / 2, self.frame.height / 3)
shareBtn.setScale(0.0005)
self.addChild(shareBtn)
restartBtn = SKSpriteNode(imageNamed: "Restart")
restartBtn.position = CGPointMake(self.frame.width / 2, self.frame.height / 4)
restartBtn.setScale(0.0005)
self.addChild(restartBtn)
}
func scoreLabel() {
let scoreDefault = NSUserDefaults.standardUserDefaults()
score = scoreDefault.valueForKey("score") as! NSInteger
scoreLbl.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
scoreLbl.fontSize = 40
scoreLbl.setScale(0.001)
scoreLbl.text = "Score: \(score)"
self.addChild(scoreLbl)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if restartBtn.containsPoint(location) {
let gameScene = GameScene(fileNamed: "GameScene")
let transition = SKTransition.fadeWithDuration(0.5)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
gameScene?.scaleMode = .AspectFill
skView.presentScene(gameScene!, transition: transition)
} else if shareBtn.containsPoint(location) {
// Function for sharing screenshot should be called here
}
}
}
答案 0 :(得分:3)
您没有展示如何使用shareScore()
功能,因此我根据您提供的教程为您创建了一个简单的示例,并且工作正常,以下是完整的示例代码:< / p>
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
override func didMoveToView(view: SKView) {
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(sprite)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if sprite.containsPoint(location) {
let postText: String = "Check out my score! Can you beat it?"
let postImage: UIImage = getScreenshot(scene!)
let activityItems = [postText, postImage]
let activityController = UIActivityViewController(
activityItems: activityItems,
applicationActivities: nil
)
let controller: UIViewController = scene!.view!.window!.rootViewController!
controller.presentViewController(
activityController,
animated: true,
completion: nil
)
}
}
}
func getScreenshot(scene: SKScene) -> UIImage {
let snapshotView = scene.view!.snapshotViewAfterScreenUpdates(true)
let bounds = UIScreen.mainScreen().bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
snapshotView.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshotImage;
}
}
当您点击太空飞船图像时,您将获得如下结果:
现在您可以轻松分享。
Sample代码了解更多信息。