斯威夫特SpriteKit

编程入门 行业动态 更新时间:2024-10-27 11:24:44
本文介绍了斯威夫特SpriteKit - 如何在 GameScene 中呈现警报视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要帮助在游戏场景中呈现警报视图.我目前正在努力这样做,因为 GameScene.Swift 不是标准的 ViewController.如果有帮助,我需要这样做,因为我需要用户输入一个值,该值用作我游戏中球 Sprite Kit 节点的坐标.输入只是一个标准整数,所以这不是问题.也欢迎任何其他关于我如何做到这一点的想法,而不是通过警报视图.

I need help presenting an alert view in the game scene. Im currently struggling to do so as GameScene.Swift isnt a standard ViewController. If it helps I need to do so as I need the user to input a value which is used as a coordinate for the ball Sprite Kit Node in my game. The input is only a standard integer so that isnt an issue. Any other idea of how I can do this which isnt through an alert view is also welcome.

override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() let view = self.view as! SKView if view.scene == nil { view.showsFPS = false view.showsNodeCount = false let gameScene = GameScene(size: view.bounds.size) gameScene.scaleMode = SKSceneScaleMode.AspectFill view.presentScene(gameScene) } }

在 GameViewController 文件中

That is in the GameViewController file

var vc : GameViewController! override init(size: CGSize) { super.init(size: size) let alertController = UIAlertController(title: "Bll Starting Position", message: "Please Enter a X Coordinate Value IN Range 0 to 345 ", preferredStyle: .Alert) alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Value Must Be In Range 0 To 345" textField.autocapitalizationType = UITextAutocapitalizationType.None textField.autocorrectionType = UITextAutocorrectionType.No textField.clearsOnBeginEditing = true textField.clearsOnInsertion = true textField.clearButtonMode = UITextFieldViewMode.Always let cancelBtn = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) let confirmBtn = UIAlertAction(title: "Confirm", style: .Default, handler: { (confirmView) in if let field = alertController.textFields![0] as? UITextField { } }) alertController.addAction(confirmBtn) alertController.addAction(cancelBtn) self.vc.presentViewController(alertController, animated: true, completion: nil) }

谢谢

推荐答案

可能有以下几种选择:

1) 快速解决方案.不要使用 UIAlertController,使用 UIAlertView.像这样:

1) Quick solution. Do not use UIAlertController, use UIAlertView. Like that:

alert.show()

但是,UIAlertView 已被弃用,因此依赖它不太安全.

However, UIAlertView is deprecated so it's not quite safe to rely on it.

2) 更好的解决方案.让您的 SKScene 子类持有对用于呈现场景的视图控制器的引用,并在创建场景时为其分配视图控制器:

2) A better solution. Make your SKScene subclass hold a reference to the view controller which you use to present the scene and when you create the scene assign it the view controller:

myScene.viewController = self

然后你就可以使用它了.

And then you can use it.

self.viewController.presentViewController(alertController, animated: true, completion: nil)

更多推荐

斯威夫特SpriteKit

本文发布于:2023-11-27 19:32:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斯威夫特   SpriteKit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!