Haa*_*adi 2 ios sprite-kit swift
我刚刚在 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
你没有展示你是如何使用shareScore()函数的,所以我从你给出的教程中为你创建了一个简单的例子,它工作正常,下面是完整的示例代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
当你点击 Spaceship 图片时,你会得到如下结果:
现在您可以轻松共享它。
更多信息的示例代码。
| 归档时间: |
|
| 查看次数: |
1630 次 |
| 最近记录: |