图集
优质
小牛编辑
146浏览
2023-12-01
可以通过此模块将图集读入BK.SpriteSetCache中,可以获取图集中的小图位置信息,或是小图的BK.Sprite。
BK.SpriteSetCache.loadSet(Object)
从指定的文件中加载图集
手Q版本:7.6.3
Object参数
属性名 | 类型 | 是否必填 | 说明 |
---|---|---|---|
jsonPath | string | 是 | 图集json文件路径 |
texturePath | string | 是 | 图集图片路径 |
format | number | 否 | 默认为RGBA8888,6代表RGBA8888,4代表RGBA4444 |
minFilter | number | 否 | 缩小采样方式 (可选,默认为1) 0最近采样 1线性采样 |
magFilter | number | 否 | 放大采样方式 (可选,默认为1)0最近采样 1线性采样 |
uWrap | BK.RepeatMode | 否 | u轴重复方式 |
vWrap | BK.RepeatMode | 否 | v轴重复方式 |
BK.RepeatMode取值:
enum RepeatMode{
mirrorRepeat = 0, //镜像重复
repeatToEdge = 1, //重复至边缘
selfRepeat = 2 //重复整个图片
}
示例:
//加载图集
var texturePath = "GameRes://fighter.png";
var jsonPath = "GameRes://fighter.json";
BK.SpriteSetCache.loadSet({jsonPath:jsonPath,texturePath:texturePath});//加载图集
//通过图集生成普通sprite
var sp = BK.SpriteSetCache.getSprite({filename:"xrollSequence0000.png",width:200,height:200});
if(sp){
BK.Director.root.addChild(sp);
}
BK.SpriteSetCache.removeSet(texturePath)
移除图集,将不再使用的图集对象从BK.SpriteSetCache中移除
手Q版本:7.6.3
texturePath:图集图片路径
示例:
//加载图集
var texturePath = "GameRes://fighter.png";
var jsonPath = "GameRes://fighter.json";
BK.SpriteSetCache.loadSet({jsonPath:jsonPath,texturePath:texturePath});//加载图集
//其他操作...
BK.SpriteSetCache.removeSet(texturePath);//移除图集
BK.SpriteSetCache.getSprite(Object)
通过小图名称生成对应的Sprite
手Q版本:7.6.3
Object参数
属性名 | 类型 | 是否必填 | 说明 |
---|---|---|---|
filename | string | 是 | 图集中小图的filename |
width | number | 否 | 生成的Sprite宽度,默认为小图的width |
height | number | 否 | 生成的Sprite高度,默认为小图的height |
返回值:BK.Sprite
示例:
//加载图集
var texturePath = "GameRes://fighter.png";
var jsonPath = "GameRes://fighter.json";
BK.SpriteSetCache.loadSet({jsonPath:jsonPath,texturePath:texturePath});
//通过图集生成普通sprite
var sp = BK.SpriteSetCache.getSprite({filename:"xrollSequence0000.png",width:200,height:200});
if(sp){
BK.Director.root.addChild(sp);
}
BK.SpriteSetCache.getTextureInfoByFilename(filename)
通过图集中的小图名称获取BK.TextureInfo对象
手Q版本:7.6.3
filename:小图名称
返回值:BK.TextureInfo
BK.TextureInfo成员
属性名 | 类型 | 说明 |
---|---|---|
texturePath | string | 图集图片的路径 |
frameInfo | BK.SheetFrameInfo | 小图的位置信息、大小等 |
texture | BK.Texture | 大图的纹理对象 |
BK.SheetFrameInfo 成员
属性名 | 类型 | 说明 |
---|---|---|
filename | string | 小图的filename |
frame | BK.SheetRect | 小图的位置信息、大小等 |
rotated | BK.Texture | 图集文件中的rotated字段 |
trimmed | boolean | 图集文件中的trimmed字段 |
spriteSourceSize | BK.SheetRect | 图集文件中的spriteSourceSize对象 |
sourceSize | BK.SheetSize | 图集文件中的sourceSize对象 |
BK.SheetRect成员
属性名 | 类型 | 说明 |
---|---|---|
x | number | 小图在图集中横轴坐标 |
y | number | 小图在图集中纵轴坐标 |
width | number | 小图宽度 |
height | number | 小图高度 |
BK.SheetSize
属性名 | 类型 | 说明 |
---|---|---|
width | number | 小图在文件中声明的宽度 |
height | number | 小图在文件中声明的高度 |
示例:
//加载图集
var texturePath = "GameRes://fighter.png";
var jsonPath = "GameRes://fighter.json";
BK.SpriteSetCache.loadSet({jsonPath:jsonPath,texturePath:texturePath});
var textureInfo = BK.SpriteSetCache.getTextureInfoByFilename("rollSequence0000.png");
if(textureInfo){
BK.Script.log(0,0,'小图 width:'+textureInfo.frameInfo.frame.width);
BK.Script.log(0,0,'小图 height:'+textureInfo.frameInfo.frame.height);
BK.Script.log(0,0,'小图 位置:('+textureInfo.frameInfo.frame.x+","+textureInfo.frameInfo.frame.y+")");
BK.Script.log(0,0,'纹理路径 texturePath:'+textureInfo.texturePath);
var texture = textureInfo.texture;//大图纹理
}
BK.SpriteSetCache.getFrameInfoByFilename(filename)
通过图集中的小图名称获取BK.FrameInfo对象
手Q版本:7.6.3
filename:小图名称
返回值:BK.SheetFrameInfo
示例:
var frameInfo = BK.SpriteSetCache.getFrameInfoByFilename("rollSequence0000.png");
if(frameInfo){
BK.Script.log(0,0,"filename:"+frameInfo.filename+",height:"+frameInfo.sourceSize.height);
}
BK.SpriteSetCache.getTextureByFilename(filename)
通过图集中的小图名称获取大图的BK.Texture对象
手Q版本:7.6.3
filename:小图名称
返回值:BK.Texture
示例:
//大图纹理
var texture = BK.SpriteSetCache.getTextureByFilename("rollSequence0000.png");
if(texture){
BK.Script.log(0,0,"texture width:"+texture.size.width);
BK.Script.log(0,0,"texture height:"+texture.size.height);
}
BK.SpriteSetCache.getTexturePathByFilename(filename)
通过图集中的小图名称获取大图的png路径
手Q版本:7.6.3
filename:小图名称
返回值:string
示例:
var path = BK.SpriteSetCache.getTexturePathByFilename("rollSequence0000.png");
BK.Script.log(0,0,"texture path:"+path);