setTexture或者setRect可以用来切图嘛?

论坛里搜索了很多,发现有不少人是这么用的, 已有spriteFrame和texture, 设置rect之后,显示的就是rect区域的内容了?

不过我自己测试是无效的,还得新建sprite才可以… 不知道是不是我理解错了…正确的切图方式是怎么样的.

我们的需求类似取图集里的某张图的需求,知道坐标,大小,想用那种图新生成一个sprite.

1赞

@jare 可以帮忙看看吗,谢谢

你要把你写的代码贴出来,不然没人知道如何回复你

@Knox 谢谢,代码就是直接调用setTexture或者setRect方法,比如:

mySprite.setTexture(wholeTexture ,new cc.Rect(0,0,100,100) )

wholeTexture是一整张图片,希望sprite显示的时候只显示从0,0开始的长宽为100的正方形区域.

但是实际显示的还是一整张图.

你试看看用 mySprite.spriteFrame,setRect(new Rect(0,0,100,100));

试过也不行…

那我帮你呼叫 @panda

sprite.setTexture 这个接口是不存在的

spriteFrame.setRect 也确实不会主动更新 uv 数据,有一个办法是

var spriteFrame = sprite.spriteFrame;
sprite.spriteFrame = null;
spriteFrame.setRect(cc.rect(0, 0, 100, 100));
sprite.spriteFrame = spriteFrame;

或者调用完 setRect 之后,调用 sprite._applySpriteFrame(),不过涉及到私有方法调用,不是非常推荐

4赞

谢谢 @Knox@panda

嗯不用私有方法是最好的了.

image image image
重置后rect后,uv按照rect 宽高居中显示,并不是按照原始位置显示,并且图片package后,rect x ,y 会更混乱