Cocos Creator API

1.0.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

SpriteFrame

Extends Asset
Module: cc

A cc.SpriteFrame has:

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

    You can modify the frame of a _ccsg.Sprite by doing:

Properties

_textureFilenameSetter String private readOnly

Use this property to set raw texture url during loading

insetTop Number

Top border of the sprite

insetBottom Number

Bottom border of the sprite

insetLeft Number

Left border of the sprite

insetRight Number

Right border of the sprite

rawUrl String readOnly

Inherited from Asset:

Returns the url of this asset's first raw file, if none of rawFile exists, it will returns an empty string.

rawUrls String[] readOnly

Inherited from Asset:

Returns the url of this asset's raw files, if none of rawFile exists, it will returns an empty array.

_rawFiles String[] private

Inherited from Asset:

在 lite 版的 Fireball 里,raw asset 并不仅仅是在 properties 里声明了 rawType 才有, 而是每个 asset 都能指定自己的 raw file url。这些 url 就存在 _rawFiles 字段中。 AssetLibrary 并不会帮你加载这些 url,除非你声明了 rawType。

_uuid String private

Inherited from RawAsset:

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

SpriteFrame
(
  • filename
  • rect
  • [rotated ]
  • [offset ]
  • [originalSize ]
)

Constructor of SpriteFrame class

name type description
filename String | Texture2D
rect Rect

If parameters' length equal 2, rect in points, else rect in pixels

rotated optional Boolean

Whether the frame is rotated in the texture

offset optional Vec2

The offset of the frame in the texture

originalSize optional Size

The size of the frame in the texture

examples:

// ----------------------------------------------------
// 1. Create a cc.SpriteFrame with image path
var url = cc.url.raw('resources/textures/grossini_dance.png');
var frame1 = new cc.SpriteFrame(url, cc.Rect(0, 0, 90, 128));

// ----------------------------------------------------
// 2. Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
var url = cc.url.raw('resources/textures/grossini_dance.png');
var texture = cc.textureCache.addImage(url);
var frame1 = new cc.SpriteFrame(texture, cc.Rect(0, 0, 90, 128));
var frame2 = new cc.SpriteFrame(texture, cc.Rect(0, 0, 90, 128), false, 0, cc.Size(90, 128));

// ----------------------------------------------------
// 3. load a cc.SpriteFrame with image path (Recommend)
var url = 'resources://test assets/PurpleMonster.png/PurpleMonster';
var self = this;
cc.loader.load(url, function (err, spriteFrame) {
        var node = new cc.Node("New Sprite");
        var sprite = node.addComponent(cc.Sprite);
        sprite.spriteFrame = spriteFrame;
        node.parent = self.node
    }
);

textureLoaded ( ) boolean

Returns whether the texture have been loaded

returns:

type: boolean

addLoadedEventListener
(
  • callback
  • target
)
deprecated

deprecated: since 3.1, please use EventTarget API instead

Add a event listener for texture loaded event.

name type description
callback Function
target Object

isRotated ( ) Boolean

Returns whether the sprite frame is rotated in the texture.

returns:

type: Boolean

setRotated
(
  • bRotated
)

Set whether the sprite frame is rotated in the texture.

name type description
bRotated Boolean

getRect ( ) Rect

Returns the rect of the sprite frame in the texture.

returns:

type: Rect

setRect
(
  • rect
)

Sets the rect of the sprite frame in the texture.

name type description
rect Rect

getOriginalSize ( ) Size

Returns the original size of the trimmed image.

returns:

type: Size

setOriginalSize
(
  • size
)

Sets the original size of the trimmed image.

name type description
size Size

getTexture ( ) Texture2D

Returns the texture of the frame.

returns:

type: Texture2D

_refreshTexture
(
  • texture
)

Sets the texture of the frame, the texture is retained automatically.

name type description
texture Texture2D

getOffset ( ) Vec2

Returns the offset of the frame in the texture.

returns:

type: Vec2

setOffset
(
  • offsets
)

Sets the offset of the frame in the texture.

name type description
offsets Vec2

clone ( ) SpriteFrame

Clone the sprite frame.

returns:

type: SpriteFrame

initWithTexture
(
  • texture
  • rect
  • [rotated =false]
  • [offset =cc.v2(0,0)]
  • [originalSize =rect.size]
)
Boolean

Initializes SpriteFrame with Texture, rect, rotated, offset and originalSize in pixels.
Please pass parameters to the constructor to initialize the sprite, do not call this function yourself.

name type description
texture String | Texture2D
rect Rect

if parameters' length equal 2, rect in points, else rect in pixels

rotated optional Boolean false
offset optional Vec2 cc.v2(0,0)
originalSize optional Size rect.size

returns:

type: Boolean

copyWithZone ( ) SpriteFrame

Copy the sprite frame

returns:

type: SpriteFrame

copy ( ) SpriteFrame

Copy the sprite frame

returns:

type: SpriteFrame

serialize ( ) String private

Inherited from Asset:

应 AssetDB 要求提供这个方法

returns:

type: String

createNode
(
  • callback
)

Inherited from Asset:

Create a new node using this asset in the scene.
If this type of asset dont have its corresponding node type, this method should be null.

name type description
callback Function
  • error String

    null or the error info

  • node Object

    the created node or null

_setRawFiles
(
  • rawFiles
)
private

Inherited from Asset:

Set raw file names for this asset.

name type description
rawFiles String[]

createNodeByInfo
(
  • Info
  • callback
)

Inherited from RawAsset:

Create a new node in the scene.
If this type of asset dont have its corresponding node type, this method should be null.

name type description
Info Object
callback Function
  • error String

    null or the error info

  • node Object

    the created node or null

isRawAssetType
(
  • ctor
)
Boolean private

Inherited from RawAsset:

name type description
ctor Function

returns:

type: Boolean

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index