Cocos2dx Sprite&ImageView换图
Sprite
有两种方式:
- 使用setTexture(Texture2D)函数,可以重新设置精灵类的纹理图片。
/* Sets a new texture (from a filename) to the sprite.
*通过文件名- @memberof Sprite
- It will call
setTextureRect()
with the texture's content size.
*/
virtual void setTexture(const std::string &filename );
/** @overload
* 通过纹理
* The Texture's rect is not changed.
*/
virtual void setTexture(Texture2D *texture) ;
2.精灵帧
/** @{
* Sets a new SpriteFrame to the Sprite.
/
virtual void setSpriteFrame(const std::string &spriteFrameName);
virtual void setSpriteFrame(SpriteFrame newFrame);
ImageView
只有一种方式:
/**
* Load texture for imageview.
*
* @param fileName file name of texture.
* @param texType @see Widget::TextureResType
*/
void loadTexture(const std::string& fileName,TextureResType texType = TextureResType::LOCAL);
其中:LOCAL为本地的图片,就是没有生成plist。PLIST就是通过SpriteFram加载的plist文件
/**
* Texture resource type.
* - LOCAL: It means the texture is loaded from image.
* - PLIST: It means the texture is loaded from texture atlas.
*/
enum class TextureResType
{
LOCAL = 0,
PLIST = 1
};
可视区域
sprite:
先设置setTexture;放一个大图
再进行setTextureRect设置可视区域
例如:
mj->setTexture(handAll->getTexture());
mj->setTextureRect(CCRectMake(texture->getPixelsWide() * 9 / 38, 0, texture->getPixelsWide() / 38, texture->getPixelsHigh()));