Macos 单击时获取矩阵中的SKSpriteNode
为了尝试SpriteKit和Swift for OS X(不适用于iOS),我想创建一个扫雷游戏 游戏背后的所有逻辑都已经完成了(并且证明了它是有效的,我是作为一个大学项目完成的,但是使用了CLI界面),但是我遇到的主要问题是UI 我有一个10x10瓷砖矩阵,在用户单击屏幕后,我无法获得正确的瓷砖 我的代码如下所示:Macos 单击时获取矩阵中的SKSpriteNode,macos,swift,sprite-kit,Macos,Swift,Sprite Kit,为了尝试SpriteKit和Swift for OS X(不适用于iOS),我想创建一个扫雷游戏 游戏背后的所有逻辑都已经完成了(并且证明了它是有效的,我是作为一个大学项目完成的,但是使用了CLI界面),但是我遇到的主要问题是UI 我有一个10x10瓷砖矩阵,在用户单击屏幕后,我无法获得正确的瓷砖 我的代码如下所示: class Tile { var sprite: SKSpriteNode var isBomb: Bool var isUnlocked: Bool
class Tile {
var sprite: SKSpriteNode
var isBomb: Bool
var isUnlocked: Bool
var row: Int
var col: Int
init(sprite: SKSpriteNode, row: Int, col: Int){
// Default assignations
self.sprite.name = "\(row):\(col)"
}
}
class GameScene: SKScene {
var board: [[Tile]] = [[Tile]]()
let ROWS: Int = 10
let COLS: Int = 10
override func didMoveToView(view: SKView){
for row in 0..<ROWS {
board.append([Tile]())
for col in 0..<COLS{
var tileSprite = SKSpriteNode(imageNamed: "tile")
var x: CGFloat = (tileSprite.size.height * CGFloat(row))+(tileSprite.size.height/2)
var y: CGFloat = (tileSprite.size.width * CGFloat(col))+(tileSprite.size.width/2)
tileSprite.position = CGPoint(x: x, y: y)
// I also add it here, just for ensuring that the name is set
tileSprite.name = "\(row):\(col)"
board[row].append(Tile(sprite: tileSprite, row: row, col: col))
addChild(tileSprite)
}
}
}
func getRowColFromNode(node: SKSpriteNode) -> (row: Int, col: Int){
var row: Int = 0
var col: Int = 0
/*var xPosition = node.position.x
var yPosition = node.position.y
row = Int(yPosition/node.size.height)
col = Int(xPosition/node.size.width)*/
if let name = node.name {
println("NAME: \(name)")
var rowString: String
var colString: String
var nameSplit = name.componentsSeparatedByString(":")
rowString = nameSplit[0]
colString = nameSplit[1]
row = rowString.toInt()!
col = colString.toInt()!
println("DETECTEDROW: \(row)")
println("DETECTEDCOL: \(col)")
}
return (row, col)
}
override func mouseDown(theEvent: NSEvent) {
var touchedNode: SKSpriteNode = nodeAtPoint(theEvent.locationInWindow) as SKSpriteNode
var row: Int
var col: Int
(row, col) = getRowColFromNode(touchedNode)
println("ROW: \(row)")
println("COL: \(col)")
}
}
类平铺{
变量sprite:SKSpriteNode
炸弹:布尔
变量isUnlocked:Bool
变量行:Int
变量列:Int
init(精灵:SKSpriteNode,行:Int,列:Int){
//默认转让
self.sprite.name=“\(行):\(列)”
}
}
类游戏场景:SKScene{
var板:[[Tile]]=[[Tile]]()
让行:Int=10
设COLS:Int=10
覆盖func didMoveToView(视图:SKView){
对于0..中的行,我建议您重新考虑一下您的结构。为什么不将您的tiles作为SKSpriteNode的一个子类(而不是包含一个子类)
然后在您的设置中:
for row in 0..<ROWS {
board.append([Tile]())
for col in 0..<COLS{
var tileSprite = TileSprite(imageNamed: "tile")
var x: CGFloat = (tileSprite.size.height * CGFloat(row))+(tileSprite.size.height/2)
var y: CGFloat = (tileSprite.size.width * CGFloat(col))+(tileSprite.size.width/2)
tileSprite.position = CGPoint(x: x, y: y)
tileSprite.row = row
tileSprite.col = col
addChild(tileSprite)
}
}
您还可以将其展开,通过setter方法更改作为TileSprite元组属性的行/列。然后,该setter方法还可以负责计算精灵的位置,清理场景设置。问题是鼠标单击的位置在视图的坐标系中。您eed将它们转换为场景坐标以选择正确的节点。下面是一个如何执行此操作的示例
override func mouseDown(theEvent: NSEvent) {
// Convert the mouse-down event's location to scene coordinates
let location = theEvent.locationInNode(self)
let touchedNode = nodeAtPoint(location)
谢谢你的回答,但我还是遇到了一些问题:1。-当我单击时,我不知道为什么,但它在屏幕的另一个区域检测到单击,所以我得到了错误的磁贴。2。-SKSPriteNode子类化有点棘手,但我设法解决了它。3。-我尝试只在屏幕上打印NSEvent.locationOnScreen,并且它似乎工作正常好的,但是nodeAtPoint()函数没有返回正确的TIL最后返回0x141E的答案我设法解决了这个问题,但是感谢您的优化建议这就是问题所在,谢谢!顺便说一句,“location”声明应该是location:CGPoint
override func mouseDown(theEvent: NSEvent) {
let touchedNode = nodeAtPoint(theEvent.locationInWindow) as TileSprite
let row = touchedNode.row
let col = touchedNode.col
println("ROW: \(row)")
println("COL: \(col)")
}
override func mouseDown(theEvent: NSEvent) {
// Convert the mouse-down event's location to scene coordinates
let location = theEvent.locationInNode(self)
let touchedNode = nodeAtPoint(location)