मेरा सुझाव है कि आप ग्रिड को SKSpriteNode
के बनावट के रूप में कार्यान्वित करें क्योंकि स्प्राइट किट एक ड्रॉ कॉल में ग्रिड प्रस्तुत करेगा। कैसे एक ग्रिड बना सकते हैं और ग्रिड
class GameScene: SKScene {
override func didMove(to: SKView) {
if let grid = Grid(blockSize: 40.0, rows:5, cols:5) {
grid.position = CGPoint (x:frame.midX, y:frame.midY)
addChild(grid)
let gamePiece = SKSpriteNode(imageNamed: "Spaceship")
gamePiece.setScale(0.0625)
gamePiece.position = grid.gridPosition(row: 1, col: 0)
grid.addChild(gamePiece)
}
}
}
अद्यतन करने के लिए एक खेल टुकड़ा जोड़ने के लिए है
class Grid:SKSpriteNode {
var rows:Int!
var cols:Int!
var blockSize:CGFloat!
convenience init?(blockSize:CGFloat,rows:Int,cols:Int) {
guard let texture = Grid.gridTexture(blockSize: blockSize,rows: rows, cols:cols) else {
return nil
}
self.init(texture: texture, color:SKColor.clear, size: texture.size())
self.blockSize = blockSize
self.rows = rows
self.cols = cols
}
class func gridTexture(blockSize:CGFloat,rows:Int,cols:Int) -> SKTexture? {
// Add 1 to the height and width to ensure the borders are within the sprite
let size = CGSize(width: CGFloat(cols)*blockSize+1.0, height: CGFloat(rows)*blockSize+1.0)
UIGraphicsBeginImageContext(size)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
let bezierPath = UIBezierPath()
let offset:CGFloat = 0.5
// Draw vertical lines
for i in 0...cols {
let x = CGFloat(i)*blockSize + offset
bezierPath.move(to: CGPoint(x: x, y: 0))
bezierPath.addLine(to: CGPoint(x: x, y: size.height))
}
// Draw horizontal lines
for i in 0...rows {
let y = CGFloat(i)*blockSize + offset
bezierPath.move(to: CGPoint(x: 0, y: y))
bezierPath.addLine(to: CGPoint(x: size.width, y: y))
}
SKColor.white.setStroke()
bezierPath.lineWidth = 1.0
bezierPath.stroke()
context.addPath(bezierPath.cgPath)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return SKTexture(image: image!)
}
func gridPosition(row:Int, col:Int) -> CGPoint {
let offset = blockSize/2.0 + 0.5
let x = CGFloat(col) * blockSize - (blockSize * CGFloat(cols))/2.0 + offset
let y = CGFloat(rows - row - 1) * blockSize - (blockSize * CGFloat(rows))/2.0 + offset
return CGPoint(x:x, y:y)
}
}
और यहाँ:
निर्धारित करने के लिए जो ग्रिड यहाँ कैसे ऐसा करने के लिए की एक उदाहरण है वर्ग को छुआ था, इसे init
self.isUserInteractionEnabled = true
और इस Grid
वर्ग के लिए:
override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let position = touch.location(in:self)
let node = atPoint(position)
if node != self {
let action = SKAction.rotate(by:CGFloat.pi*2, duration: 1)
node.run(action)
}
else {
let x = size.width/2 + position.x
let y = size.height/2 - position.y
let row = Int(floor(x/blockSize))
let col = Int(floor(y/blockSize))
print("\(row) \(col)")
}
}
}
क्यों नहीं का उपयोग CGContext (क्वार्ट्ज) लाइनों आकर्षित करने के लिए? CGPaths को देखो। आपको केवल एक ही देखभाल करने की आवश्यकता है यह सुनिश्चित कर लें कि निर्देशांक सह-शामिल हों। आखिरकार, sprites sprites हैं ... –
हे, माइकल एल! सलाह के लिये धन्यवाद। मुझे नहीं पता था कि उन CGPaths को एक कुशल तरीके से कैसे उपयोग करें, लेकिन 0x141E के उत्तर ने इसे पूरी तरह से किया। – Alex
क्या ग्रिड को छेद स्क्रीन पर आवश्यकता होती है क्योंकि आपको स्क्रीन के लेंस को विभाजित करना चाहिए, जो आप चाहते हैं कि ग्रिड के लंगर को फेंक दें। – Cing