当我在CGRect
中分配SKScene
参数时,它会消失:
import Foundation
import SpriteKit
import UIKit
var username = defaults.string(forKey: "usernameSaved")
class SetupScene: SKScene{
var myTextField: UITextField = UITextField();
override func didMove(to view: SKView) {
super.didMove(to: view)
let background = SKSpriteNode(imageNamed: "pixelBackground")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.size = self.size
background.zPosition = -10
self.addChild(background)
self.view?.addSubview(myTextField)
myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 230.00, height: 33.00));
myTextField.backgroundColor = UIColor.white
myTextField.placeholder = "Enter Name Here:"
myTextField.borderStyle = UITextBorderStyle.line
myTextField.textAlignment = .center
myTextField.font = UIFont(name: "04b_25", size: 25)
myTextField.translatesAutoresizingMaskIntoConstraints = false
myTextField.center = (self.view?.center)!
}
但是,当我在SKScene
之前设置参数时,如下所示,它会显示在屏幕的左上角。
import Foundation
import SpriteKit
import UIKit
var username = defaults.string(forKey: "usernameSaved")
class SetupScene: SKScene{
var myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 230.00, height: 33.00));
override func didMove(to view: SKView) {
super.didMove(to: view)
let background = SKSpriteNode(imageNamed: "pixelBackground")
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.size = self.size
background.zPosition = -10
self.addChild(background)
self.view?.addSubview(myTextField)
myTextField.backgroundColor = UIColor.white
myTextField.placeholder = "Enter Name Here:"
myTextField.borderStyle = UITextBorderStyle.line
myTextField.textAlignment = .center
myTextField.font = UIFont(name: "04b_25", size: 25)
myTextField.translatesAutoresizingMaskIntoConstraints = false
myTextField.center = (self.view?.center)!
}
我的目标是能够在SKScene
开始后对其进行修改,因为我希望其高度为self.size.height/2
。但是,我无法在SKScene
之前执行此操作,因为它会给我错误:
自我。还没有存在。
但是我无法在场景建立后找到一种方法来编辑它的位置。