在cocos creator中使用代码给TileMap地图块添加碰撞体
cc.Class({
extends: cc.Component,
properties: {
tiledMap:cc.TiledMap
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
//开启物理效果
cc.director.getPhysicsManager().enabled = true;
cc.director.getPhysicsManager().debugDrawFlags = true;
},
start () {
let tiledSize = this.tiledMap.getTileSize();
let layer = this.tiledMap.getLayer("这里填需要添加碰撞体的组名");
let layerSize = layer.getLayerSize();
for(let i = 0;i < layerSize.width;i++){
for (let j = 0; j < layerSize.height; j++) {
let tiled = layer.getTiledTileAt(i,j,true);
if (tiled.gid != 0) {
tiled.node.group = "这里填需要添加碰撞体的组名";
let body = tiled.node.addComponent(cc.RigidBody);
body.type = cc.RigidBodyType.Static;
let collider = tiled.node.addComponent(cc.PhysicsBoxCollider);
collider.offset = cc.v2(tiledSize.width/2,tiledSize.height/2);
collider.size = tiledSize;
collider.apply();
}
}
}
},
// update (dt) {},
});