SpriteKit(11) - 瓦片地图

瓦片地图

import SpriteKit
class TileMap : SKScene {
    override func didMove(to view: SKView) {
        self.size = UIScreen.main.bounds.size
        self.addChild(createTileMap())
    }
    
    func createTileMap() -> TileMapNode {
        return TileMapNode(altesName: "scenery",
                           tileSize: CGSize(width: UIScreen.main.bounds.size.width/15,
                                            height: UIScreen.main.bounds.size.height/15),
                           tileCodes: [
                            "xxxxxxxxxxxxxxx",
                            "x-------------x",
                            "x-------------x",
                            "x-------------x",
                            "x-------------x",
                            "x----xxxxxx---x",
                            "x----x--------x",
                            "x----x--------x",
                            "x----xxxxx----x",
                            "x--------x----x",
                            "x--------x----x",
                            "x-------xx----x",
                            "x-------xx----x",
                            "x-------xx----x",
                            "xxxxxxxxxxxxxxx",
                            ])
    }

}


class TileMapNode : SKNode {
    var tileSize : CGSize = CGSize() //保存瓷砖大小
    var altes : SKTextureAtlas?//添加纹理集
    init(tileSize : CGSize) {
        super.init()
        self.tileSize = tileSize
    }
    
    //初始化
    convenience init(altesName : String, tileSize : CGSize, tileCodes : [String]) {
        self.init(tileSize: tileSize)
        altes = SKTextureAtlas(named: altesName)
        for row in 0..<tileCodes.count {

            let line = tileCodes[row]
            for (col,code) in line.enumerated()  {
                if let tile = nodeForCode(tileCode: code) {
                    tile.position = postionForRow(row: row, col: col)
                    self.addChild(tile)
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func nodeForCode(tileCode : Character) -> SKNode?{
        if altes == nil {
            return nil
        }
        var tile : SKNode?
        switch tileCode {
        case "x":tile = SKSpriteNode(texture: SKTexture(imageNamed: "wall"))
        case "o":tile = SKSpriteNode(texture: SKTexture(imageNamed: "grass"))
        default:print("unkown tile code \(tileCode)")
        }
        
        if let sprite = tile as? SKSpriteNode  {
            sprite.blendMode = .replace
        }
        return tile
    }
    
    func postionForRow(row : Int , col: Int) -> CGPoint {
        let x = CGFloat(col) * tileSize.width + tileSize.width/2
        let y = CGFloat(row) * tileSize.height + tileSize.height/2
        return CGPoint(x: x, y: y)
    }
}
瓦片地图
  • 这里的地图和设计的倒置了,这个就不纠结位置了.
  • 地图的设置其实也是根据对应的布局添加固定的节点.
  • 这里是是一种是设计的思路,添加节点和前面的的知识样.
  • 这里还可以通过加载Txt文件,TML文件到地图中
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,118评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,881评论 18 139
  • 11.15 姓名:韩艾辰 第76天 【学习:60分钟】 【冥想(静定):20分钟】 【找到/服务老师:每天至少为老...
    韩艾辰阅读 290评论 0 0
  • 我所理解的博客 春节是我们华人最重要的节日。每年春节,我家里的晚辈会去祭奠已经离世的长辈,表达怀念之情。只不过,我...
    徐向明在简书阅读 1,272评论 8 26