ios - 使用 SpriteKit 绘制网格

标签 ios swift sprite-kit

绘制 grid like this 的最佳方法是什么?通过使用 SpriteKit 2D 游戏引擎? 要求:

  • 以编程方式输入列数和行数(5x5、10x3、3x4 等)。
  • 使用SKSpriteNodeSKShapeNode 以编程方式绘制它,因为只使用square like this 的图像对我来说似乎效率不高。
  • 正方形应具有固定大小(假设每个正方形为 40x40)。
  • 网格应在 View 中垂直和水平居中。

我打算使用 SKSpriteNode(来自图像)作为玩家在此网格中的不同方 block 中移动。
因此,我将在二维数组中保存每个方 block 的中心点 (x,y),然后从玩家的当前位置移动到该位置。如果您对此也有更好的建议,我很想听听。

我会很感激 Swift 中的解决方案(最好是 2.1),但 Objective-C 也可以。计划只在 iPhone 设备上使用它。
我的问题很接近to this one .感谢您的帮助。

最佳答案

我建议您将网格实现为 SKSpriteNode 的纹理,因为 Sprite Kit 将在单个绘制调用中渲染网格。以下是如何执行此操作的示例:

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)
    }
}

下面是如何创建网格并将游戏 block 添加到网格

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)
        }
    }
}

更新:

要确定触摸了哪个网格方 block ,请将其添加到 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)")
        }
    }
}

关于ios - 使用 SpriteKit 绘制网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33464925/

相关文章:

ios - 如何以编程方式从 UITableView 中删除一行并更新 UI

iOS 在 App Bundle 中打开 iBook 文件

c - 文件是为 i386 构建的,它不是在 Mac OSX 10.6 上为 iOS 4.2 编译 OpenCV2.2 时链接的体系结构 (x86_64)

ios - 添加到管理对象有序集

swift - 即使在 XCTest 文件中的类扩展中实现了方法之后,协议(protocol)的默认实现也始终会被调用

sprite-kit - 展开可选值 : PhysicsBody - Swift SpriteKit

ios - 使用 TrueDepth 摄像头拍摄照片时出现“没有事件且已启用的视频连接”错误

ios - 使用平滑滚动提前加载

swift - 如何最好地在 SpriteKit 中编写撤消函数?

swift - SKCropNode 奇怪的行为