我正在为我的游戏添加一个老式的高分输入屏幕,用户点击每个字母输入他们的名字。
每个字母,符号或短语('DEL','SP'等)都是单1
,而且很难点击','和'。'字符和一些符号虽然。通过通常的SKLabelNode
为了使拘留更容易我计划在每个字母后面放置一个更大的节点并更新touchesBegan
以检测标签本身或另一个标签上的触摸,在这种情况下只需使用子节点进行触摸(这将是字母或短语)。
在我这样做之前,我想知道是否有一种简单的方法可以达到我想要的效果,可以通过检测最近的touchesBegan
触摸等来实现。
答案 0 :(得分:1)
建立在@KnightOfDragon所说的基础之上,我只想为每个键创建一个SKSpriteNode的子类。这样就可以让你的代码客观化,漂亮!
这里可以做的事情还有很多,但是我匆匆忙忙地把它鞭打了。它没有所有字母,也不知道如何处理del,space,return,但我认为你的代码已经知道如何处理它们。
Key类使用协议发送按下了哪个键,因此您不必担心捕获场景中的每个关键区域
所以我输入了其余的键并为间距添加了一些空白填充选项。我没有和你一样的字体,但看起来很酷
import SpriteKit
protocol KeyDelegate: NSObjectProtocol {
func keyWasPressed(sender: Key)
}
enum KeyType: Int {
case a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, comma, period, del, space, ret, dash, lb, rb, pound, exclaim, quest, filler, halfFiller
var height: CGFloat {
return 60
}
var width: CGFloat {
switch self {
case .del: return 150
case .space: return 180
case .ret: return 150
case .halfFiller: return 30
default: return 60
}
}
var keyValue: String {
switch self {
case .filler: return ""
default: return String(describing: self)
}
}
var keyText: String {
switch self {
case .del: return "DEL"
case .space: return "SP"
case .ret: return "ENT"
case .lb: return "("
case .rb: return ")"
case .exclaim: return "!"
case .comma: return ","
case .period: return "."
case .dash: return "-"
case .pound: return "#"
case .quest: return "?"
case .filler, .halfFiller: return ""
default: return String(describing: self).uppercased()
}
}
var keyTextColor: SKColor {
switch self {
case .del, .space, .ret: return .red
case .lb, .rb, .exclaim, .dash, .pound, .quest: return .green
default: return .blue
}
}
var filler: Bool {
switch self {
case .filler, .halfFiller: return true
default: return false
}
}
}
class Key: SKSpriteNode {
var keyType: KeyType = .a
private var keyValue = ""
var keyText: String = ""
weak var keyDelegate: KeyDelegate!
init(keyType: KeyType) {
//let backgroundColor: SKColor = keyType.filler ? .clear : .red
super.init(texture: nil, color: .clear, size: CGSize.zero)
self.isUserInteractionEnabled = true
self.size = CGSize(width: keyType.width, height: keyType.height)
self.anchorPoint = CGPoint(x: 0, y: 0)
self.keyType = keyType
self.keyValue = keyType.keyValue
self.keyText = keyType.keyText
guard !keyType.filler else { return }
// let square = SKShapeNode(rectOf: size)
// square.strokeColor = .white
// square.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
// square.zPosition = 1
// addChild(square)
let titleLabel = SKLabelNode(fontNamed: "04b_19")
titleLabel.fontColor = keyType.keyTextColor
titleLabel.fontSize = 56
titleLabel.horizontalAlignmentMode = .center
titleLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center
titleLabel.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
titleLabel.zPosition = 10
titleLabel.text = self.keyText
self.addChild(titleLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getKeyValue() -> String {
return self.keyValue
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard !keyType.filler else { return }
self.keyDelegate.keyWasPressed(sender: self)
}
}
class GameScene: SKScene, KeyDelegate {
private var titleLabel = SKLabelNode()
override func didMove(to view: SKView) {
titleLabel.fontColor = .white
titleLabel.fontSize = 68
titleLabel.horizontalAlignmentMode = .left
titleLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.baseline
titleLabel.position = CGPoint(x: 220, y: 800)
titleLabel.zPosition = 10
titleLabel.text = ""
self.addChild(titleLabel)
let keys: [[KeyType]] = [[.q, .w, .e, .r, .t, .y, .u, .i, .o, .p],
[.halfFiller, .a, .s, .d, .f, .g, .h, .j, .k, .l],
[.halfFiller, .filler, .z, .x, .c, .v, .b, .n, .m, .comma, .period],
[.del, .filler, .halfFiller, .space, .filler, .halfFiller, .ret],
[.filler, .filler, .dash, .lb, .rb, .pound, .exclaim, .quest]]
let padding: CGFloat = 8
let startPoint = CGPoint(x: 30, y: 700)
var xOffset: CGFloat = 0
var yOffset: CGFloat = 0
var keyHeight: CGFloat = 0
for row in keys {
for keyType in row {
print("keyType \(keyType)")
let key = Key(keyType: keyType)
key.position = CGPoint(x: startPoint.x + xOffset, y: startPoint.y + yOffset)
key.keyDelegate = self
addChild(key)
xOffset += key.keyType.width + padding
keyHeight = key.keyType.height
}
xOffset = 0
yOffset -= (keyHeight + padding)
}
}
//MARK:- KeyDelegate Func
func keyWasPressed(sender: Key) {
let text = titleLabel.text!
titleLabel.text = text + sender.getKeyValue()
}
}