Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SpriteFrame

A SpriteFrame support several types

  1. Rectangle sprite frame
  2. Sliced 9 sprite frame
  3. Mesh sprite frame It mainly contains:
  • texture: A TextureBase or RenderTexture that will be used by render process
  • rectangle: A rectangle of the texture
  • Sliced 9 border insets: The distance of each side from the internal rect to the sprite frame rect
  • vertices: Vertex list for the mesh type sprite frame
  • uv: The quad uv
  • uvSliced: The sliced 9 uv
example
import { loader } from 'cc';
// First way to use a SpriteFrame
const url = "assets/PurpleMonster/icon/spriteFrame";
loader.loadRes(url, (err, spriteFrame) => {
  const node = new Node("New Sprite");
  const sprite = node.addComponent(Sprite);
  sprite.spriteFrame = spriteFrame;
  node.parent = self.node;
});
// Second way to use a SpriteFrame
const self = this;
const url = "test_assets/PurpleMonster";
loader.loadRes(url, (err, imageAsset) => {
 if(err){
   return;
 }
 const node = new Node("New Sprite");
 const sprite = node.addComponent(Sprite);
 const spriteFrame = new SpriteFrame();
 const tex = imageAsset._texture;
 spriteFrame.texture = tex;
 sprite.spriteFrame = spriteFrame;
 node.parent = self.node;
});
// Third way to use a SpriteFrame
const self = this;
const cameraComp = this.getComponent(Camera);
const renderTexture = new RenderTexture();
rendetTex.reset({
  width: 512,
  height: 512,
  depthStencilFormat: RenderTexture.DepthStencilFormat.DEPTH_24_STENCIL_8
});
cameraComp.targetTexture = renderTexture;
const spriteFrame = new SpriteFrame();
spriteFrame.texture = renderTexture;

Hierarchy

Index

Constructors

constructor

Properties

Protected _atlasUuid

_atlasUuid: string = ""

Protected _capInsets

_capInsets: number[] = [0, 0, 0, 0]

Protected _flipUv

_flipUv: boolean = false

Protected _name

_name: string

_native

_native: string = ""

Serializable url for native asset. For internal usage.

default

""

_objFlags

_objFlags: number

Protected _offset

_offset: Vec2 = new Vec2()

Protected _originalSize

_originalSize: Size = new Size()

Protected _rect

_rect: Rect = new Rect()

Protected _rotated

_rotated: boolean = false

Protected _texture

_uuid

_uuid: string

内部使用。

loaded

loaded: boolean = true

Whether the asset is loaded or not

uv

uv: number[] = []

UV for quad vertices

uvHash

uvHash: number = 0

uvSliced

uvSliced: IUV[] = []

UV for sliced 9 vertices

vertices

vertices: IVertices | null = null

Vertex list for the mesh type sprite frame

Static preventDeferredLoadDependents

preventDeferredLoadDependents: boolean = false

Indicates whether its dependent raw assets can support deferred load if the owner scene (or prefab) is marked as asyncLoadAssets.

default

false

Static preventPreloadNativeObject

preventPreloadNativeObject: boolean = false

Indicates whether its native object should be preloaded from native url.

default

false

Accessors

Private nativeAsset

  • get nativeAsset(): any
  • set nativeAsset(obj: any): void
  • The underlying native asset of this asset if one is available.
    This property can be used to access additional details or functionality releated to the asset.
    This property will be initialized by the loader if _native is available.

    default

    null

    Returns any

  • The underlying native asset of this asset if one is available.
    This property can be used to access additional details or functionality releated to the asset.
    This property will be initialized by the loader if _native is available.

    default

    null

    Parameters

    • obj: any

    Returns void

textureSource

atlasUuid

  • get atlasUuid(): string
  • set atlasUuid(value: string): void

height

  • get height(): number

insetBottom

  • get insetBottom(): number
  • set insetBottom(value: number): void

insetLeft

  • get insetLeft(): number
  • set insetLeft(value: number): void

insetRight

  • get insetRight(): number
  • set insetRight(value: number): void

insetTop

  • get insetTop(): number
  • set insetTop(value: number): void

isValid

  • get isValid(): boolean
  • Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
    When an object's destroy is called, it is actually destroyed after the end of this frame. So isValid will return false from the next frame, while isValid in the current frame will still be true. If you want to determine whether the current frame has called destroy, use isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.

    default

    true

    readonly
    example
    import { Node, log } from 'cc';
    const node = new Node();
    log(node.isValid);    // true
    node.destroy();
    log(node.isValid);    // true, still valid in this frame
    // after a frame...
    log(node.isValid);    // false, destroyed in the end of last frame

    Returns boolean

name

  • get name(): string
  • set name(value: string): void

nativeUrl

  • get nativeUrl(): string

offset

  • get offset(): Vec2
  • set offset(value: Vec2): void
  • The offset of the sprite frame center. Sprite frame in an atlas texture could be trimmed for clipping the transparent pixels, so the trimmed rect is smaller than the original one, the offset defines the distance from the original center to the trimmed center.

    Returns Vec2

  • The offset of the sprite frame center. Sprite frame in an atlas texture could be trimmed for clipping the transparent pixels, so the trimmed rect is smaller than the original one, the offset defines the distance from the original center to the trimmed center.

    Parameters

    Returns void

originalSize

  • get originalSize(): Size
  • set originalSize(value: Size): void

rect

  • get rect(): Rect
  • set rect(value: Rect): void
  • Returns the rect of the sprite frame in the texture. If it's an atlas texture, a transparent pixel area is proposed for the actual mapping of the current texture.

    Returns Rect

  • Returns the rect of the sprite frame in the texture. If it's an atlas texture, a transparent pixel area is proposed for the actual mapping of the current texture.

    Parameters

    Returns void

rotated

  • get rotated(): boolean
  • set rotated(rotated: boolean): void

texture

width

  • get width(): number

Methods

calculateSlicedUV

  • _calculateSlicedUV(): void

calculateUV

  • _calculateUV(): void

deserialize

  • deserialize(serializeData: any, handle: any): void

destroyImmediate

  • _destroyImmediate(): void

destruct

  • _destruct(): void
  • Clear all references in the instance.

    NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }

    Returns void

Protected refreshTexture

serialize

  • serialize(exporting?: any): any

Private setRawAsset

  • setRawAsset(filename: string, inLibrary?: boolean): void

Protected textureLoaded

  • _textureLoaded(): void

checkRect

Optional createNode

destroy

  • destroy(): boolean

emit

  • emit(type: EventType, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void
  • Trigger an event directly with the event name and necessary arguments.

    Parameters

    • type: EventType

      event type

    • Optional arg0: any
    • Optional arg1: any
    • Optional arg2: any
    • Optional arg3: any
    • Optional arg4: any

    Returns void

getGFXSampler

getGFXTexture

getOffset

getOriginalSize

getRect

  • Returns the rect of the sprite frame in the texture. If it's an atlas texture, a transparent pixel area is proposed for the actual mapping of the current texture.

    deprecated

    since v1.2, please use rect

    Parameters

    • Optional out: Rect

    Returns Rect

hasEventListener

  • hasEventListener(type: string, callback?: Function, target?: undefined | object): boolean
  • Checks whether there is correspond event listener registered on the given event.

    Parameters

    • type: string

      Event type.

    • Optional callback: Function

      Callback function when event triggered.

    • Optional target: undefined | object

      Callback callee.

    Returns boolean

isRotated

  • isRotated(): boolean

off

  • Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.

    example

    import { log } from 'cc'; // register fire eventListener var callback = eventTarget.on('fire', function () { log("fire in the hole"); }, target); // remove fire event listener eventTarget.off('fire', callback, target); // remove all fire event listeners eventTarget.off('fire');

    Type parameters

    • TFunction: Function

    Parameters

    • type: EventType

      A string representing the event type being removed.

    • Optional callback: TFunction

      The callback to remove.

    • Optional thisArg: any

    Returns void

on

  • on<TFunction>(type: EventType, callback: TFunction, thisArg?: any, once?: undefined | false | true): typeof callback
  • Register an callback of a specific event type on the EventTarget. This type of event should be triggered via emit.

    example

    import { log } from 'cc'; eventTarget.on('fire', function () { log("fire in the hole"); }, node);

    Type parameters

    • TFunction: Function

    Parameters

    • type: EventType

      A string representing the event type to listen for.

    • callback: TFunction

      The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

    • Optional thisArg: any

      The target (this object) to invoke the callback, can be null

    • Optional once: undefined | false | true

    Returns typeof callback

    • Just returns the incoming callback so you can save the anonymous function easier.

onLoaded

  • onLoaded(): void

once

  • once<TFunction>(type: EventType, callback: TFunction, thisArg?: any): typeof callback
  • Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

    example

    import { log } from 'cc'; eventTarget.once('fire', function () { log("this is the callback and will be invoked only once"); }, node);

    Type parameters

    • TFunction: Function

    Parameters

    • type: EventType

      A string representing the event type to listen for.

    • callback: TFunction

      The callback that will be invoked when the event is dispatched. The callback is ignored if it is a duplicate (the callbacks are unique).

    • Optional thisArg: any

    Returns typeof callback

removeAll

  • removeAll(typeOrTarget: string | object): void
  • Removes all callbacks registered in a certain event type or all callbacks registered with a certain target

    Parameters

    • typeOrTarget: string | object

      The event type or target with which the listeners will be removed

    Returns void

reset

Private serialize

  • serialize(): void

setOffset

  • setOffset(offset: Vec2): void

setOriginalSize

  • setOriginalSize(size: Size): void

setRect

  • setRect(rect: Rect): void

setRotated

  • setRotated(rotated: boolean): void

targetOff

  • targetOff(typeOrTarget: string | object): void
  • Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.

    Parameters

    • typeOrTarget: string | object

      The target to be searched for all related listeners

    Returns void

textureLoaded

  • textureLoaded(): boolean

toString

  • toString(): string
  • Returns the string representation of the object.
    The Asset object overrides the toString() method of the Object object.
    JavaScript calls the toString() method automatically
    when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.

    For assets of the native type, it will return this.nativeUrl.
    Otherwise, an empty string is returned.
    This method may be overwritten by subclasses.

    method

    toString

    Returns string

Static deferredDestroy

  • _deferredDestroy(): void

Static createWithImage

Static deserialize

  • deserialize(data: any): any

Static isRawAssetType

  • isRawAssetType(ctor: Function): boolean

Generated using TypeDoc