Cocos2d-x  v4
SpriteFrame Class Reference

A SpriteFrame has: More...

Inherits Ref, and Clonable.

Public Member Functions

const RectgetRectInPixels () const
 Get rect of the sprite frame. More...
 
void setRectInPixels (const Rect &rectInPixels)
 Set rect of the sprite frame. More...
 
bool isRotated () const
 Is the sprite frame rotated or not. More...
 
void setRotated (bool rotated)
 Set rotated of the sprite frame. More...
 
const RectgetRect () const
 Get rect of the frame. More...
 
void setRect (const Rect &rect)
 Set rect of the frame. More...
 
const RectgetCenterRect () const
 Get center rect of the frame. More...
 
void setCenterRectInPixels (const Rect &centerRect)
 setCenterRect More...
 
bool hasCenterRect () const
 hasCenterRect More...
 
const Vec2getOffsetInPixels () const
 Get offset of the frame. More...
 
void setOffsetInPixels (const Vec2 &offsetInPixels)
 Set offset of the frame. More...
 
const Size & getOriginalSizeInPixels () const
 Get original size of the trimmed image. More...
 
void setOriginalSizeInPixels (const Size &sizeInPixels)
 Set original size of the trimmed image. More...
 
const Size & getOriginalSize () const
 Get original size of the trimmed image. More...
 
void setOriginalSize (const Size &sizeInPixels)
 Set original size of the trimmed image. More...
 
Texture2DgetTexture ()
 Get texture of the frame. More...
 
void setTexture (Texture2D *pobTexture)
 Set texture of the frame, the texture is retained. More...
 
const Vec2getOffset () const
 Get offset of the frame. More...
 
void setOffset (const Vec2 &offsets)
 Set offset of the frame. More...
 
const Vec2getAnchorPoint () const
 Get anchor point of the frame. More...
 
void setAnchorPoint (const Vec2 &anchorPoint)
 Set anchor point of the frame. More...
 
bool hasAnchorPoint () const
 Check if anchor point is defined for the frame. More...
 
virtual SpriteFrameclone () const override
 Returns a copy of the Ref.
 
void setPolygonInfo (const PolygonInfo &polygonInfo)
 Set the polygon info for polygon mesh sprites. More...
 
const PolygonInfogetPolygonInfo () const
 Get the polygonInfo for this sprite. More...
 
bool hasPolygonInfo () const
 Check if sprite frame is a polygon sprite. More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 Destructor. More...
 
- Public Member Functions inherited from Clonable
virtual ~Clonable ()
 @js NA @lua NA
 

Static Public Member Functions

static SpriteFramecreate (const std::string &filename, const Rect &rect)
 Create a SpriteFrame with a texture filename, rect in points. More...
 
static SpriteFramecreate (const std::string &filename, const Rect &rect, bool rotated, const Vec2 &offset, const Size &originalSize)
 Create a SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels. More...
 
static SpriteFramecreateWithTexture (Texture2D *pobTexture, const Rect &rect)
 Create a SpriteFrame with a texture, rect in points. More...
 
static SpriteFramecreateWithTexture (Texture2D *pobTexture, const Rect &rect, bool rotated, const Vec2 &offset, const Size &originalSize)
 Create a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. More...
 

Protected Member Functions

 SpriteFrame ()
 @lua NA
 
virtual ~SpriteFrame ()
 @lua NA
 
bool initWithTexture (Texture2D *pobTexture, const Rect &rect)
 Initializes a SpriteFrame with a texture, rect in points. More...
 
bool initWithTextureFilename (const std::string &filename, const Rect &rect)
 Initializes a SpriteFrame with a texture filename, rect in points; It is assumed that the frame was not trimmed.
 
bool initWithTexture (Texture2D *pobTexture, const Rect &rect, bool rotated, const Vec2 &offset, const Size &originalSize)
 Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. More...
 
bool initWithTextureFilename (const std::string &filename, const Rect &rect, bool rotated, const Vec2 &offset, const Size &originalSize)
 Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. More...
 

Additional Inherited Members

- Public Attributes inherited from Ref
unsigned int _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 
void * _scriptObject
 scriptObject, support for swift
 
bool _rooted
 When true, it means that the object was already rooted.
 

Detailed Description

A SpriteFrame has:

  • texture: A Texture2D that will be used by the Sprite
  • rectangle: A rectangle of the texture

You can modify the frame of a Sprite by doing:

sprite->setSpriteFrame(frame);

Member Function Documentation

◆ create() [1/2]

static SpriteFrame* create ( const std::string &  filename,
const Rect rect 
)
static

Create a SpriteFrame with a texture filename, rect in points.

It is assumed that the frame was not trimmed.

Parameters
filenameTexture file name.
rectA specified rect.
Returns
An autoreleased SpriteFrame object.

◆ create() [2/2]

static SpriteFrame* create ( const std::string &  filename,
const Rect rect,
bool  rotated,
const Vec2 offset,
const Size &  originalSize 
)
static

Create a SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels.

The originalSize is the size in pixels of the frame before being trimmed.

Parameters
filenameTexture filename
rectA specified rect.
rotatedIs rotated if true.
offsetA specified offset.
originalSizeA specified original size.
Returns
An autoreleased SpriteFrame object.

◆ createWithTexture() [1/2]

static SpriteFrame* createWithTexture ( Texture2D pobTexture,
const Rect rect 
)
static

Create a SpriteFrame with a texture, rect in points.

It is assumed that the frame was not trimmed.

Parameters
pobTextureThe texture pointer.
rectA specified rect.
Returns
An autoreleased SpriteFrame object.

◆ createWithTexture() [2/2]

static SpriteFrame* createWithTexture ( Texture2D pobTexture,
const Rect rect,
bool  rotated,
const Vec2 offset,
const Size &  originalSize 
)
static

Create a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.

The originalSize is the size in points of the frame before being trimmed.

Parameters
pobTextureThe texture pointer.
rectA specified rect.
rotatedIs rotated if true.
offsetA specified offset.
originalSizeA specified original size.
Returns
An autoreleased SpriteFrame object.

◆ getRectInPixels()

const Rect& getRectInPixels ( ) const
inline

Get rect of the sprite frame.

Returns
The rect of the sprite frame, in pixels.

◆ setRectInPixels()

void setRectInPixels ( const Rect rectInPixels)

Set rect of the sprite frame.

Parameters
rectInPixelsThe rect of the sprite frame, in pixels.

◆ isRotated()

bool isRotated ( ) const
inline

Is the sprite frame rotated or not.

Returns
Is rotated if true.

◆ setRotated()

void setRotated ( bool  rotated)
inline

Set rotated of the sprite frame.

Parameters
rotatedRotated the sprite frame if true.

◆ getRect()

const Rect& getRect ( ) const
inline

Get rect of the frame.

Returns
The rect of the sprite frame.

◆ setRect()

void setRect ( const Rect rect)

Set rect of the frame.

Parameters
rectThe rect of the sprite.

◆ getCenterRect()

const Rect& getCenterRect ( ) const
inline

Get center rect of the frame.

Useful to create 9-slice sprites

Returns
The center rect of the sprite frame in points

◆ setCenterRectInPixels()

void setCenterRectInPixels ( const Rect centerRect)

setCenterRect

Useful to implement "9 sliced" sprites. The sprite will be sliced into a 3 x 3 grid. The four corners of this grid are applied without performing any scaling. The upper- and lower-middle parts are scaled horizontally, and the left- and right-middle parts are scaled vertically. The center is scaled in both directions. Important: The scaling is based the Sprite's trimmed size.

Limitations: Does not work when the sprite is part of SpriteBatchNode.

Parameters
centerRectthe Rect in points

◆ hasCenterRect()

bool hasCenterRect ( ) const

hasCenterRect

Returns
Whether or not it has a centerRect

◆ getOffsetInPixels()

const Vec2& getOffsetInPixels ( ) const

Get offset of the frame.

Returns
The offset of the sprite frame, in pixels.

◆ setOffsetInPixels()

void setOffsetInPixels ( const Vec2 offsetInPixels)

Set offset of the frame.

Parameters
offsetInPixelsThe offset of the sprite frame, in pixels.

◆ getOriginalSizeInPixels()

const Size& getOriginalSizeInPixels ( ) const
inline

Get original size of the trimmed image.

Returns
The original size of the trimmed image, in pixels.

◆ setOriginalSizeInPixels()

void setOriginalSizeInPixels ( const Size &  sizeInPixels)
inline

Set original size of the trimmed image.

Parameters
sizeInPixelsThe original size of the trimmed image, in pixels.

◆ getOriginalSize()

const Size& getOriginalSize ( ) const
inline

Get original size of the trimmed image.

Returns
The original size of the trimmed image.

◆ setOriginalSize()

void setOriginalSize ( const Size &  sizeInPixels)
inline

Set original size of the trimmed image.

Parameters
sizeInPixelsThe original size of the trimmed image.

◆ getTexture()

Texture2D* getTexture ( )

Get texture of the frame.

Returns
The texture of the sprite frame.

◆ setTexture()

void setTexture ( Texture2D pobTexture)

Set texture of the frame, the texture is retained.

Parameters
pobTextureThe texture of the sprite frame.

◆ getOffset()

const Vec2& getOffset ( ) const

Get offset of the frame.

Returns
The offset of the sprite frame.

◆ setOffset()

void setOffset ( const Vec2 offsets)

Set offset of the frame.

Parameters
offsetsThe offset of the sprite frame.

◆ getAnchorPoint()

const Vec2& getAnchorPoint ( ) const

Get anchor point of the frame.

Returns
The anchor point of the sprite frame.

◆ setAnchorPoint()

void setAnchorPoint ( const Vec2 anchorPoint)

Set anchor point of the frame.

Parameters
anchorPointThe anchor point of the sprite frame.

◆ hasAnchorPoint()

bool hasAnchorPoint ( ) const

Check if anchor point is defined for the frame.

Returns
true if anchor point is available.

◆ setPolygonInfo()

void setPolygonInfo ( const PolygonInfo polygonInfo)

Set the polygon info for polygon mesh sprites.

Parameters
polygonInfotriangle mesh of the sprite

◆ getPolygonInfo()

const PolygonInfo& getPolygonInfo ( ) const

Get the polygonInfo for this sprite.

Returns
a reference to the polygonInfo structure

◆ hasPolygonInfo()

bool hasPolygonInfo ( ) const

Check if sprite frame is a polygon sprite.

Returns
true if polygonInfo is available

◆ initWithTexture() [1/2]

bool initWithTexture ( Texture2D pobTexture,
const Rect rect 
)
protected

Initializes a SpriteFrame with a texture, rect in points.

It is assumed that the frame was not trimmed.

◆ initWithTexture() [2/2]

bool initWithTexture ( Texture2D pobTexture,
const Rect rect,
bool  rotated,
const Vec2 offset,
const Size &  originalSize 
)
protected

Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.

The originalSize is the size in points of the frame before being trimmed.

◆ initWithTextureFilename()

bool initWithTextureFilename ( const std::string &  filename,
const Rect rect,
bool  rotated,
const Vec2 offset,
const Size &  originalSize 
)
protected

Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.

The originalSize is the size in pixels of the frame before being trimmed.

Since
v1.1

The documentation for this class was generated from the following file:
cocos2d::SpriteFrame::SpriteFrame
SpriteFrame()
@lua NA
cocos2d::SpriteFrame::createWithTexture
static SpriteFrame * createWithTexture(Texture2D *pobTexture, const Rect &rect)
Create a SpriteFrame with a texture, rect in points.