使用代码生成BoxCollider2D,但是发现位置不对,怀疑和锚点有关,但是调整锚点也不对,这块该怎么操作?背景图是使用tilemap制作的,是不是有哪些参数不对?
代码如下:
this.tiledMap = this.node.getComponent(TiledMap);
const brickLayer = this.tiledMap.getLayer(“brick”)
const tileSize = this.tiledMap.getTileSize();
const layerSize = brickLayer.getLayerSize();
for (let i = 0; i < layerSize.width; i++) {
for (let j = 0; j < layerSize.height; j++) {
const tiled = brickLayer.getTiledTileAt(i, j, true);
if (tiled.grid !== 0) { // 非空瓦片
const node = tiled.node;
// node.group = "collidable"; // 分组管理碰撞体
// 添加刚体组件(静态障碍物)
const rigidbody = node.addComponent(RigidBody2D);
rigidbody.type = ERigidBody2DType.Static
// 添加碰撞盒组件
const collider = node.addComponent(BoxCollider2D);
collider.offset = new Vec2(tileSize.width / 2, tileSize.height / 2);
collider.size = tileSize;
// collider.enabled = true;
collider.apply();
// node.getComponent(UITransform).setAnchorPoint(new Vec2(0.5, 0.5));
}
}
}