如何将文字打印到游戏画面上

我有这个if语句,所有的代码完美的工作,暂停function和所有,但…游戏已经暂停后,我想要在屏幕上打印一些文本给用户,但它只打印在控制台,可以请让我知道如何在游戏设备屏幕上打印文字,谢谢!

if score[0] == 10 { pauseGame() print("HEY") sleep(5) let skView = self.view! skView.presentScene(scene) } else if score [1] == 10 { pauseGame() print("HEY") sleep(5) let skView = self.view! skView.presentScene(scene) } 

您不能使用类似于打印语句的方式将文本打印到视图(游戏屏幕),这仅仅是因为iOS具有graphics用户界面,需要您指定在屏幕上放置文本的位置,尺寸/字体/颜色等等。 也就是说把文字放在屏幕上比较容易,你可以这样做我的一个文本标签。 下面是你如何用UILabel做到这一点,把这个代码放在if语句中:

 let textLabel = UILabel(frame: CGRect(x: 20, y: 20, width: 100, height: 20)) //You can change the postitioning obviously, keep in mind (x: 0,y: 0) is actually the top left corner textLabel.font = UIFont.systemFont(ofSize: 10) //textLabel.font = UIFont(name: "ChalkboardSE-Bold", size: 10) //If you want a custom font textLabel.textColor = UIColor.green textLabel.textAlignment = .center self.view.addSubview(textLabel) 

有关可能的字体列表,请参阅此网页 ,并将“ChalkboardSE-Bold”replace为所需字体的名称。