2013-10-01 12:54:29 +00:00
/ * *
* @ author Richard Davey < rich @ photonstorm . com >
* @ copyright 2013 Photon Storm Ltd .
* @ license { @ link https : //github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
* /
/ * *
2013-10-25 15:54:40 +00:00
* Create a new ` Sprite ` object . Sprites are the lifeblood of your game , used for nearly everything visual .
2013-10-25 14:02:21 +00:00
* At its most basic a Sprite consists of a set of coordinates and a texture that is rendered to the canvas .
* They also contain additional properties allowing for physics motion ( via Sprite . body ) , input handling ( via Sprite . input ) ,
* events ( via Sprite . events ) , animation ( via Sprite . animations ) , camera culling and more . Please see the Examples for use cases .
*
2013-10-01 12:54:29 +00:00
* @ class Phaser . Sprite
* @ constructor
2013-10-25 14:02:21 +00:00
* @ param { Phaser . Game } game - A reference to the currently running game .
* @ param { number } x - The x coordinate ( in world space ) to position the Sprite at .
* @ param { number } y - The y coordinate ( in world space ) to position the Sprite at .
* @ param { string | Phaser . RenderTexture | PIXI . Texture } key - This is the image or texture used by the Sprite during rendering . It can be a string which is a reference to the Cache entry , or an instance of a RenderTexture or PIXI . Texture .
* @ param { string | number } frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index .
2013-10-01 12:54:29 +00:00
* /
2013-09-11 02:55:53 +00:00
Phaser . Sprite = function ( game , x , y , key , frame ) {
2013-08-30 03:20:14 +00:00
2013-08-31 20:50:34 +00:00
x = x || 0 ;
y = y || 0 ;
2013-09-11 02:55:53 +00:00
key = key || null ;
2013-08-31 20:50:34 +00:00
frame = frame || null ;
2013-10-01 12:54:29 +00:00
/ * *
* @ property { Phaser . Game } game - A reference to the currently running Game .
* /
2013-08-30 03:20:14 +00:00
this . game = game ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-01 15:39:39 +00:00
* @ property { boolean } exists - If exists = false then the Sprite isn ' t updated by the core game loop or physics subsystem at all .
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-08-30 03:20:14 +00:00
this . exists = true ;
2013-09-06 19:20:58 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-01 15:39:39 +00:00
* @ property { boolean } alive - This is a handy little var your game can use to determine if a sprite is alive or not , it doesn ' t effect rendering .
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-08-30 03:20:14 +00:00
this . alive = true ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Group } group - The parent Group of this Sprite . This is usually set after Sprite instantiation by the parent .
2013-10-01 12:54:29 +00:00
* /
2013-08-30 03:20:14 +00:00
this . group = null ;
2013-09-08 12:23:21 +00:00
2013-10-01 12:54:29 +00:00
/ * *
* @ property { string } name - The user defined name given to this Sprite .
* @ default
* /
2013-08-30 03:20:14 +00:00
this . name = '' ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { number } type - The const type of this object .
* @ default
2013-10-01 12:54:29 +00:00
* /
2013-09-12 20:54:41 +00:00
this . type = Phaser . SPRITE ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { number } renderOrderID - Used by the Renderer and Input Manager to control picking order .
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-09-08 12:23:21 +00:00
this . renderOrderID = - 1 ;
2013-10-01 12:54:29 +00:00
/ * *
* If you would like the Sprite to have a lifespan once 'born' you can set this to a positive value . Handy for particles , bullets , etc .
* The lifespan is decremented by game . time . elapsed each update , once it reaches zero the kill ( ) function is called .
2013-10-25 15:54:40 +00:00
* @ property { number } lifespan - The lifespan of the Sprite ( in ms ) before it will be killed .
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-09-10 00:26:50 +00:00
this . lifespan = 0 ;
2013-09-11 15:25:46 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Events } events - The Events you can subscribe to that are dispatched when certain things happen on this Sprite or its components .
2013-10-01 12:54:29 +00:00
* /
2013-09-11 15:25:46 +00:00
this . events = new Phaser . Events ( this ) ;
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . AnimationManager } animations - This manages animations of the sprite . You can modify animations through it ( see Phaser . AnimationManager )
2013-10-01 12:54:29 +00:00
* /
2013-09-11 15:25:46 +00:00
this . animations = new Phaser . AnimationManager ( this ) ;
/ * *
2013-10-01 12:54:29 +00:00
* @ property { InputHandler } input - The Input Handler Component .
* /
2013-09-11 15:25:46 +00:00
this . input = new Phaser . InputHandler ( this ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { string | Phaser . RenderTexture | PIXI . Texture } key - This is the image or texture used by the Sprite during rendering . It can be a string which is a reference to the Cache entry , or an instance of a RenderTexture or PIXI . Texture .
2013-10-01 12:54:29 +00:00
* /
2013-09-11 02:55:53 +00:00
this . key = key ;
2013-10-25 14:02:21 +00:00
/ * *
* @ property { Phaser . Frame } currentFrame - A reference to the currently displayed frame .
* /
this . currentFrame = null ;
2013-09-11 02:55:53 +00:00
if ( key instanceof Phaser . RenderTexture )
2013-09-03 05:02:47 +00:00
{
2013-09-11 02:55:53 +00:00
PIXI . Sprite . call ( this , key ) ;
2013-09-11 01:57:36 +00:00
2013-09-11 02:55:53 +00:00
this . currentFrame = this . game . cache . getTextureFrame ( key . name ) ;
2013-09-03 05:02:47 +00:00
}
2013-10-11 17:18:27 +00:00
else if ( key instanceof PIXI . Texture )
{
PIXI . Sprite . call ( this , key ) ;
this . currentFrame = frame ;
}
2013-09-11 01:57:36 +00:00
else
{
2013-09-11 02:55:53 +00:00
if ( key == null || this . game . cache . checkImageKey ( key ) == false )
2013-09-11 01:57:36 +00:00
{
2013-09-11 02:55:53 +00:00
key = '__default' ;
2013-10-24 20:21:00 +00:00
this . key = key ;
2013-09-11 01:57:36 +00:00
}
2013-09-11 02:55:53 +00:00
PIXI . Sprite . call ( this , PIXI . TextureCache [ key ] ) ;
2013-09-11 01:57:36 +00:00
2013-09-11 02:55:53 +00:00
if ( this . game . cache . isSpriteSheet ( key ) )
2013-09-11 01:57:36 +00:00
{
2013-09-11 02:55:53 +00:00
this . animations . loadFrameData ( this . game . cache . getFrameData ( key ) ) ;
2013-09-03 05:02:47 +00:00
2013-09-11 01:57:36 +00:00
if ( frame !== null )
{
if ( typeof frame === 'string' )
{
this . frameName = frame ;
}
else
{
this . frame = frame ;
}
}
}
else
{
2013-09-11 02:55:53 +00:00
this . currentFrame = this . game . cache . getFrame ( key ) ;
2013-09-11 01:57:36 +00:00
}
}
2013-09-10 10:09:25 +00:00
2013-11-01 04:58:08 +00:00
/ * *
* The rectangular area from the texture that will be rendered .
* @ property { Phaser . Rectangle } textureRegion
* /
this . textureRegion = new Phaser . Rectangle ( this . texture . frame . x , this . texture . frame . y , this . texture . frame . width , this . texture . frame . height ) ;
2013-09-03 14:35:40 +00:00
/ * *
2013-10-01 12:54:29 +00:00
* The anchor sets the origin point of the texture .
* The default is 0 , 0 this means the textures origin is the top left
* Setting than anchor to 0.5 , 0.5 means the textures origin is centered
* Setting the anchor to 1 , 1 would mean the textures origin points will be the bottom right
*
2013-10-25 15:54:40 +00:00
* @ property { Phaser . Point } anchor - The anchor around with Sprite rotation and scaling takes place .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . anchor = new Phaser . Point ( ) ;
2013-08-30 03:20:14 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { number } x - The x coordinate ( in world space ) of this Sprite .
2013-10-01 12:54:29 +00:00
* /
2013-09-02 22:22:24 +00:00
this . x = x ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { number } y - The y coordinate ( in world space ) of this Sprite .
2013-10-01 12:54:29 +00:00
* /
2013-09-02 22:22:24 +00:00
this . y = y ;
2013-08-30 03:20:14 +00:00
this . position . x = x ;
this . position . y = y ;
2013-11-01 02:07:21 +00:00
/ * *
* @ property { Phaser . Point } world - The world coordinates of this Sprite . This differs from the x / y coordinates which are relative to the Sprites container .
* /
this . world = new Phaser . Point ( x , y ) ;
2013-09-02 22:22:24 +00:00
/ * *
2013-10-01 12:54:29 +00:00
* Should this Sprite be automatically culled if out of range of the camera ?
2013-10-25 14:02:21 +00:00
* A culled sprite has its renderable property set to 'false' .
2013-10-01 12:54:29 +00:00
*
2013-10-25 15:54:40 +00:00
* @ property { boolean } autoCull - A flag indicating if the Sprite should be automatically camera culled or not .
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-09-02 22:22:24 +00:00
this . autoCull = false ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } scale - The scale of the Sprite when rendered . By default it ' s set to 1 ( no scale ) . You can modify it via scale . x or scale . y or scale . setTo ( x , y ) . A value of 1 means no change to the scale , 0.5 means "half the size" , 2 means "twice the size" , etc .
2013-10-01 12:54:29 +00:00
* /
2013-08-30 19:05:29 +00:00
this . scale = new Phaser . Point ( 1 , 1 ) ;
2013-10-01 12:54:29 +00:00
/ * *
* @ property { Phaser . Point } _cache - A mini cache for storing all of the calculated values .
* @ private
* /
2013-09-02 22:22:24 +00:00
this . _cache = {
2013-08-31 20:50:34 +00:00
2013-09-02 22:22:24 +00:00
dirty : false ,
// Transform cache
2013-10-04 17:09:56 +00:00
a00 : - 1 , a01 : - 1 , a02 : - 1 , a10 : - 1 , a11 : - 1 , a12 : - 1 , id : - 1 ,
2013-09-02 22:22:24 +00:00
2013-09-08 21:38:19 +00:00
// Input specific transform cache
2013-10-04 17:09:56 +00:00
i01 : - 1 , i10 : - 1 , idi : - 1 ,
2013-09-02 22:22:24 +00:00
// Bounds check
left : null , right : null , top : null , bottom : null ,
2013-11-01 02:07:21 +00:00
// delta cache
prevX : x , prevY : y ,
2013-10-04 13:41:15 +00:00
// The previous calculated position
2013-09-02 22:22:24 +00:00
x : - 1 , y : - 1 ,
// The actual scale values based on the worldTransform
scaleX : 1 , scaleY : 1 ,
// The width/height of the image, based on the un-modified frame size multiplied by the final calculated scale size
2013-09-03 14:35:40 +00:00
width : this . currentFrame . sourceSizeW , height : this . currentFrame . sourceSizeH ,
// The actual width/height of the image if from a trimmed atlas, multiplied by the final calculated scale size
2013-09-03 16:07:05 +00:00
halfWidth : Math . floor ( this . currentFrame . sourceSizeW / 2 ) , halfHeight : Math . floor ( this . currentFrame . sourceSizeH / 2 ) ,
2013-09-02 22:22:24 +00:00
2013-10-24 20:21:00 +00:00
// The width/height of the image, based on the un-modified frame size multiplied by the final calculated scale size
calcWidth : - 1 , calcHeight : - 1 ,
2013-09-03 14:35:40 +00:00
// The current frame details
2013-10-25 01:19:16 +00:00
// frameID: this.currentFrame.uuid, frameWidth: this.currentFrame.width, frameHeight: this.currentFrame.height,
frameID : - 1 , frameWidth : this . currentFrame . width , frameHeight : this . currentFrame . height ,
2013-09-02 22:22:24 +00:00
// If this sprite visible to the camera (regardless of being set to visible or not)
2013-10-24 03:27:28 +00:00
cameraVisible : true ,
// Crop cache
cropX : 0 , cropY : 0 , cropWidth : this . currentFrame . sourceSizeW , cropHeight : this . currentFrame . sourceSizeH
2013-09-02 22:22:24 +00:00
} ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } offset - Corner point defaults . Should not typically be modified .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . offset = new Phaser . Point ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } center - A Point containing the center coordinate of the Sprite . Takes rotation and scale into account .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 16:07:05 +00:00
this . center = new Phaser . Point ( x + Math . floor ( this . _cache . width / 2 ) , y + Math . floor ( this . _cache . height / 2 ) ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } topLeft - A Point containing the top left coordinate of the Sprite . Takes rotation and scale into account .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . topLeft = new Phaser . Point ( x , y ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } topRight - A Point containing the top right coordinate of the Sprite . Takes rotation and scale into account .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . topRight = new Phaser . Point ( x + this . _cache . width , y ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } bottomRight - A Point containing the bottom right coordinate of the Sprite . Takes rotation and scale into account .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . bottomRight = new Phaser . Point ( x + this . _cache . width , y + this . _cache . height ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Point } bottomLeft - A Point containing the bottom left coordinate of the Sprite . Takes rotation and scale into account .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . bottomLeft = new Phaser . Point ( x , y + this . _cache . height ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* This Rectangle object fully encompasses the Sprite and is updated in real - time .
* The bounds is the full bounding area after rotation and scale have been taken into account . It should not be modified directly .
* It ' s used for Camera culling and physics body alignment .
* @ property { Phaser . Rectangle } bounds
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . bounds = new Phaser . Rectangle ( x , y , this . _cache . width , this . _cache . height ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { Phaser . Physics . Arcade . Body } body - By default Sprites have a Phaser . Physics Body attached to them . You can operate physics actions via this property , or null it to skip all physics updates .
2013-10-01 12:54:29 +00:00
* /
2013-09-03 14:35:40 +00:00
this . body = new Phaser . Physics . Arcade . Body ( this ) ;
2013-09-01 05:21:39 +00:00
2013-10-10 08:03:38 +00:00
/ * *
* @ property { number } health - Health value . Used in combination with damage ( ) to allow for quick killing of Sprites .
* /
this . health = 1 ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { boolean } inWorld - This value is set to true if the Sprite is positioned within the World , otherwise false .
2013-10-01 12:54:29 +00:00
* /
2013-09-09 16:01:59 +00:00
this . inWorld = Phaser . Rectangle . intersects ( this . bounds , this . game . world . bounds ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { number } inWorldThreshold - A threshold value applied to the inWorld check . If you don ' t want a Sprite to be considered "out of the world" until at least 100 px away for example then set it to 100.
2013-10-01 12:54:29 +00:00
* @ default
* /
2013-09-09 16:01:59 +00:00
this . inWorldThreshold = 0 ;
2013-10-08 20:09:46 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { boolean } outOfBoundsKill - If true the Sprite is killed as soon as Sprite . inWorld is false .
2013-10-08 20:09:46 +00:00
* @ default
* /
this . outOfBoundsKill = false ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ property { boolean } _outOfBoundsFired - Internal flag .
2013-10-01 12:54:29 +00:00
* @ private
* @ default
* /
2013-09-09 12:29:33 +00:00
this . _outOfBoundsFired = false ;
2013-10-07 23:58:20 +00:00
/ * *
* A Sprite that is fixed to the camera ignores the position of any ancestors in the display list and uses its x / y coordinates as offsets from the top left of the camera .
* @ property { boolean } fixedToCamera - Fixes this Sprite to the Camera .
* @ default
* /
this . fixedToCamera = false ;
2013-10-30 03:46:52 +00:00
/ * *
* @ property { Phaser . Point } cameraOffset - If this Sprite is fixed to the camera then use this Point to specify how far away from the Camera x / y it ' s rendered .
* /
2013-10-29 04:07:26 +00:00
this . cameraOffset = new Phaser . Point ;
2013-10-30 03:46:52 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* You can crop the Sprites texture by modifying the crop properties . For example crop . width = 50 would set the Sprite to only render 50 px wide .
* The crop is only applied if you have set Sprite . cropEnabled to true .
2013-10-25 15:54:40 +00:00
* @ property { Phaser . Rectangle } crop - The crop Rectangle applied to the Sprite texture before rendering .
2013-10-25 14:02:21 +00:00
* @ default
* /
2013-10-24 03:27:28 +00:00
this . crop = new Phaser . Rectangle ( 0 , 0 , this . _cache . width , this . _cache . height ) ;
2013-10-25 14:02:21 +00:00
/ * *
* @ property { boolean } cropEnabled - If true the Sprite . crop property is used to crop the texture before render . Set to false to disable .
* @ default
* /
2013-10-24 03:27:28 +00:00
this . cropEnabled = false ;
2013-10-30 03:46:52 +00:00
this . updateCache ( ) ;
this . updateBounds ( ) ;
2013-08-30 03:20:14 +00:00
} ;
2013-09-03 05:02:47 +00:00
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
2013-08-30 03:20:14 +00:00
Phaser . Sprite . prototype = Object . create ( PIXI . Sprite . prototype ) ;
Phaser . Sprite . prototype . constructor = Phaser . Sprite ;
/ * *
2013-10-25 14:02:21 +00:00
* Automatically called by World . preUpdate . Handles cache updates , lifespan checks , animation updates and physics updates .
*
* @ method Phaser . Sprite # preUpdate
* @ memberof Phaser . Sprite
2013-10-01 12:54:29 +00:00
* /
2013-09-10 23:35:21 +00:00
Phaser . Sprite . prototype . preUpdate = function ( ) {
2013-08-30 03:20:14 +00:00
2013-11-13 06:49:24 +00:00
if ( ! this . exists || ! this . group . exists )
2013-09-06 19:20:58 +00:00
{
2013-09-08 12:23:21 +00:00
this . renderOrderID = - 1 ;
2013-09-06 19:20:58 +00:00
return ;
}
2013-09-10 00:26:50 +00:00
if ( this . lifespan > 0 )
{
this . lifespan -= this . game . time . elapsed ;
if ( this . lifespan <= 0 )
{
this . kill ( ) ;
return ;
}
}
2013-09-02 22:22:24 +00:00
this . _cache . dirty = false ;
2013-09-01 04:50:47 +00:00
2013-09-02 22:22:24 +00:00
if ( this . visible )
{
2013-09-08 12:23:21 +00:00
this . renderOrderID = this . game . world . currentRenderOrderID ++ ;
2013-09-20 12:55:33 +00:00
}
2013-09-08 12:23:21 +00:00
2013-10-04 18:00:55 +00:00
this . updateCache ( ) ;
2013-10-30 03:46:52 +00:00
2013-10-10 08:03:38 +00:00
this . updateAnimation ( ) ;
2013-10-04 17:09:56 +00:00
2013-10-30 03:46:52 +00:00
this . updateCrop ( ) ;
2013-10-24 03:27:28 +00:00
2013-10-04 17:09:56 +00:00
// Re-run the camera visibility check
2013-11-01 02:07:21 +00:00
if ( this . _cache . dirty || this . world . x !== this . _cache . prevX || this . world . y !== this . _cache . prevY )
2013-10-04 17:09:56 +00:00
{
2013-10-24 20:21:00 +00:00
this . updateBounds ( ) ;
2013-10-04 17:09:56 +00:00
}
if ( this . body )
{
this . body . preUpdate ( ) ;
}
2013-11-01 02:07:21 +00:00
} ;
2013-10-04 17:09:56 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* Internal function called by preUpdate .
*
* @ method Phaser . Sprite # updateCache
* @ memberof Phaser . Sprite
* /
2013-10-04 17:09:56 +00:00
Phaser . Sprite . prototype . updateCache = function ( ) {
2013-11-01 02:07:21 +00:00
this . _cache . prevX = this . world . x ;
this . _cache . prevY = this . world . y ;
2013-10-30 03:46:52 +00:00
if ( this . fixedToCamera )
{
this . x = this . game . camera . view . x + this . cameraOffset . x ;
this . y = this . game . camera . view . y + this . cameraOffset . y ;
}
this . world . setTo ( this . game . camera . x + this . worldTransform [ 2 ] , this . game . camera . y + this . worldTransform [ 5 ] ) ;
2013-09-02 22:22:24 +00:00
2013-10-30 03:46:52 +00:00
if ( this . worldTransform [ 1 ] != this . _cache . i01 || this . worldTransform [ 3 ] != this . _cache . i10 || this . worldTransform [ 0 ] != this . _cache . a00 || this . worldTransform [ 41 ] != this . _cache . a11 )
2013-09-20 12:55:33 +00:00
{
2013-10-30 03:46:52 +00:00
this . _cache . a00 = this . worldTransform [ 0 ] ; // scaleX a |a c tx|
this . _cache . a01 = this . worldTransform [ 1 ] ; // skewY c |b d ty|
this . _cache . a10 = this . worldTransform [ 3 ] ; // skewX b |0 0 1|
2013-09-20 12:55:33 +00:00
this . _cache . a11 = this . worldTransform [ 4 ] ; // scaleY d
2013-10-04 13:41:15 +00:00
2013-10-04 18:00:55 +00:00
this . _cache . i01 = this . worldTransform [ 1 ] ; // skewY c (remains non-modified for input checks)
this . _cache . i10 = this . worldTransform [ 3 ] ; // skewX b (remains non-modified for input checks)
2013-10-04 13:41:15 +00:00
this . _cache . scaleX = Math . sqrt ( ( this . _cache . a00 * this . _cache . a00 ) + ( this . _cache . a01 * this . _cache . a01 ) ) ; // round this off a bit?
2013-09-20 12:55:33 +00:00
this . _cache . scaleY = Math . sqrt ( ( this . _cache . a10 * this . _cache . a10 ) + ( this . _cache . a11 * this . _cache . a11 ) ) ; // round this off a bit?
2013-10-04 13:41:15 +00:00
this . _cache . a01 *= - 1 ;
2013-09-20 12:55:33 +00:00
this . _cache . a10 *= - 1 ;
2013-10-04 13:41:15 +00:00
2013-10-30 03:46:52 +00:00
this . _cache . id = 1 / ( this . _cache . a00 * this . _cache . a11 + this . _cache . a01 * - this . _cache . a10 ) ;
this . _cache . idi = 1 / ( this . _cache . a00 * this . _cache . a11 + this . _cache . i01 * - this . _cache . i10 ) ;
2013-09-02 22:22:24 +00:00
2013-09-20 12:55:33 +00:00
this . _cache . dirty = true ;
}
2013-09-02 22:22:24 +00:00
2013-10-30 03:46:52 +00:00
this . _cache . a02 = this . worldTransform [ 2 ] ; // translateX tx
this . _cache . a12 = this . worldTransform [ 5 ] ; // translateY ty
2013-11-01 02:07:21 +00:00
} ;
2013-10-10 08:03:38 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* Internal function called by preUpdate .
*
* @ method Phaser . Sprite # updateAnimation
* @ memberof Phaser . Sprite
* /
2013-10-10 08:03:38 +00:00
Phaser . Sprite . prototype . updateAnimation = function ( ) {
2013-10-30 03:46:52 +00:00
if ( this . animations . update ( ) || ( this . currentFrame && this . currentFrame . uuid != this . _cache . frameID ) )
2013-09-20 12:55:33 +00:00
{
2013-10-30 03:46:52 +00:00
this . _cache . frameID = this . currentFrame . uuid ;
2013-10-25 01:19:16 +00:00
this . _cache . frameWidth = this . texture . frame . width ;
this . _cache . frameHeight = this . texture . frame . height ;
2013-09-02 22:22:24 +00:00
2013-10-25 01:19:16 +00:00
this . _cache . width = this . currentFrame . width ;
this . _cache . height = this . currentFrame . height ;
2013-10-30 03:46:52 +00:00
2013-10-25 01:19:16 +00:00
this . _cache . halfWidth = Math . floor ( this . _cache . width / 2 ) ;
this . _cache . halfHeight = Math . floor ( this . _cache . height / 2 ) ;
2013-09-03 14:35:40 +00:00
2013-10-30 03:46:52 +00:00
this . _cache . dirty = true ;
2013-10-24 20:21:00 +00:00
}
2013-09-03 14:35:40 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-10-24 20:21:00 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Internal function called by preUpdate .
*
2013-10-30 03:46:52 +00:00
* @ method Phaser . Sprite # updateCrop
2013-10-25 14:02:21 +00:00
* @ memberof Phaser . Sprite
2013-10-24 20:21:00 +00:00
* /
2013-10-30 03:46:52 +00:00
Phaser . Sprite . prototype . updateCrop = function ( ) {
2013-10-24 20:21:00 +00:00
2013-10-30 03:46:52 +00:00
// This only runs if crop is enabled
if ( this . cropEnabled && ( this . crop . width != this . _cache . cropWidth || this . crop . height != this . _cache . cropHeight || this . crop . x != this . _cache . cropX || this . crop . y != this . _cache . cropY ) )
2013-10-25 02:49:14 +00:00
{
2013-10-30 03:46:52 +00:00
this . crop . floorAll ( ) ;
this . _cache . cropX = this . crop . x ;
this . _cache . cropY = this . crop . y ;
this . _cache . cropWidth = this . crop . width ;
this . _cache . cropHeight = this . crop . height ;
this . texture . frame = this . crop ;
this . texture . width = this . crop . width ;
this . texture . height = this . crop . height ;
this . texture . updateFrame = true ;
PIXI . Texture . frameUpdates . push ( this . texture ) ;
2013-10-25 02:49:14 +00:00
}
2013-10-25 01:19:16 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-10-30 03:46:52 +00:00
/ * *
* Internal function called by preUpdate .
*
* @ method Phaser . Sprite # updateBounds
* @ memberof Phaser . Sprite
* /
Phaser . Sprite . prototype . updateBounds = function ( ) {
2013-10-25 02:49:14 +00:00
this . offset . setTo ( this . _cache . a02 - ( this . anchor . x * this . width ) , this . _cache . a12 - ( this . anchor . y * this . height ) ) ;
2013-10-25 01:19:16 +00:00
2013-10-30 03:46:52 +00:00
this . getLocalPosition ( this . center , this . offset . x + ( this . width / 2 ) , this . offset . y + ( this . height / 2 ) ) ;
this . getLocalPosition ( this . topLeft , this . offset . x , this . offset . y ) ;
this . getLocalPosition ( this . topRight , this . offset . x + this . width , this . offset . y ) ;
this . getLocalPosition ( this . bottomLeft , this . offset . x , this . offset . y + this . height ) ;
this . getLocalPosition ( this . bottomRight , this . offset . x + this . width , this . offset . y + this . height ) ;
2013-10-24 20:21:00 +00:00
this . _cache . left = Phaser . Math . min ( this . topLeft . x , this . topRight . x , this . bottomLeft . x , this . bottomRight . x ) ;
this . _cache . right = Phaser . Math . max ( this . topLeft . x , this . topRight . x , this . bottomLeft . x , this . bottomRight . x ) ;
this . _cache . top = Phaser . Math . min ( this . topLeft . y , this . topRight . y , this . bottomLeft . y , this . bottomRight . y ) ;
this . _cache . bottom = Phaser . Math . max ( this . topLeft . y , this . topRight . y , this . bottomLeft . y , this . bottomRight . y ) ;
this . bounds . setTo ( this . _cache . left , this . _cache . top , this . _cache . right - this . _cache . left , this . _cache . bottom - this . _cache . top ) ;
this . updateFrame = true ;
if ( this . inWorld == false )
{
// Sprite WAS out of the screen, is it still?
this . inWorld = Phaser . Rectangle . intersects ( this . bounds , this . game . world . bounds , this . inWorldThreshold ) ;
if ( this . inWorld )
{
// It's back again, reset the OOB check
this . _outOfBoundsFired = false ;
}
}
else
{
// Sprite WAS in the screen, has it now left?
this . inWorld = Phaser . Rectangle . intersects ( this . bounds , this . game . world . bounds , this . inWorldThreshold ) ;
if ( this . inWorld == false )
{
this . events . onOutOfBounds . dispatch ( this ) ;
this . _outOfBoundsFired = true ;
if ( this . outOfBoundsKill )
{
this . kill ( ) ;
}
}
}
2013-10-30 03:46:52 +00:00
this . _cache . cameraVisible = Phaser . Rectangle . intersects ( this . game . world . camera . screenView , this . bounds , 0 ) ;
if ( this . autoCull )
{
// Won't get rendered but will still get its transform updated
this . renderable = this . _cache . cameraVisible ;
}
// Update our physics bounds
if ( this . body )
{
this . body . updateBounds ( this . center . x , this . center . y , this . _cache . scaleX , this . _cache . scaleY ) ;
}
2013-11-01 02:07:21 +00:00
} ;
2013-10-24 20:21:00 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Gets the local position of a coordinate relative to the Sprite , factoring in rotation and scale .
* Mostly only used internally .
2013-10-25 02:49:14 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # getLocalPosition
* @ memberof Phaser . Sprite
* @ param { Phaser . Point } p - The Point object to store the results in .
* @ param { number } x - x coordinate within the Sprite to translate .
* @ param { number } y - x coordinate within the Sprite to translate .
* @ param { number } sx - Scale factor to be applied .
* @ param { number } sy - Scale factor to be applied .
* @ return { Phaser . Point } The translated point .
2013-10-24 20:21:00 +00:00
* /
2013-10-30 03:46:52 +00:00
Phaser . Sprite . prototype . getLocalPosition = function ( p , x , y ) {
2013-10-24 20:21:00 +00:00
2013-10-30 03:46:52 +00:00
p . x = ( ( this . _cache . a11 * this . _cache . id * x + - this . _cache . a01 * this . _cache . id * y + ( this . _cache . a12 * this . _cache . a01 - this . _cache . a02 * this . _cache . a11 ) * this . _cache . id ) * this . scale . x ) + this . _cache . a02 ;
p . y = ( ( this . _cache . a00 * this . _cache . id * y + - this . _cache . a10 * this . _cache . id * x + ( - this . _cache . a12 * this . _cache . a00 + this . _cache . a02 * this . _cache . a10 ) * this . _cache . id ) * this . scale . y ) + this . _cache . a12 ;
2013-10-24 20:21:00 +00:00
2013-10-25 02:49:14 +00:00
return p ;
2013-10-24 20:21:00 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-10-24 20:21:00 +00:00
2013-10-25 02:49:14 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Gets the local unmodified position of a coordinate relative to the Sprite , factoring in rotation and scale .
* Mostly only used internally by the Input Manager , but also useful for custom hit detection .
2013-10-25 02:49:14 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # getLocalUnmodifiedPosition
* @ memberof Phaser . Sprite
* @ param { Phaser . Point } p - The Point object to store the results in .
* @ param { number } x - x coordinate within the Sprite to translate .
* @ param { number } y - x coordinate within the Sprite to translate .
* @ return { Phaser . Point } The translated point .
2013-10-25 02:49:14 +00:00
* /
2013-10-25 04:40:46 +00:00
Phaser . Sprite . prototype . getLocalUnmodifiedPosition = function ( p , gx , gy ) {
2013-10-24 20:21:00 +00:00
2013-10-30 03:46:52 +00:00
p . x = ( this . _cache . a11 * this . _cache . idi * gx + - this . _cache . i01 * this . _cache . idi * gy + ( this . _cache . a12 * this . _cache . i01 - this . _cache . a02 * this . _cache . a11 ) * this . _cache . idi ) + ( this . anchor . x * this . _cache . width ) ;
p . y = ( this . _cache . a00 * this . _cache . idi * gy + - this . _cache . i10 * this . _cache . idi * gx + ( - this . _cache . a12 * this . _cache . a00 + this . _cache . a02 * this . _cache . i10 ) * this . _cache . idi ) + ( this . anchor . y * this . _cache . height ) ;
2013-10-24 20:21:00 +00:00
2013-10-25 02:49:14 +00:00
return p ;
2013-09-02 22:22:24 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-10-24 03:27:28 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* Resets the Sprite . crop value back to the frame dimensions .
*
* @ method Phaser . Sprite # resetCrop
* @ memberof Phaser . Sprite
* /
2013-10-24 03:27:28 +00:00
Phaser . Sprite . prototype . resetCrop = function ( ) {
this . crop = new Phaser . Rectangle ( 0 , 0 , this . _cache . width , this . _cache . height ) ;
this . texture . setFrame ( this . crop ) ;
this . cropEnabled = false ;
2013-11-01 02:07:21 +00:00
} ;
2013-10-24 03:27:28 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* Internal function called by the World postUpdate cycle .
*
* @ method Phaser . Sprite # postUpdate
* @ memberof Phaser . Sprite
* /
2013-09-20 12:55:33 +00:00
Phaser . Sprite . prototype . postUpdate = function ( ) {
if ( this . exists )
{
2013-09-23 00:06:09 +00:00
// The sprite is positioned in this call, after taking into consideration motion updates and collision
2013-10-04 13:41:15 +00:00
if ( this . body )
{
this . body . postUpdate ( ) ;
}
2013-09-23 00:06:09 +00:00
2013-10-07 23:58:20 +00:00
if ( this . fixedToCamera )
{
2013-10-29 04:07:26 +00:00
this . _cache . x = this . game . camera . view . x + this . cameraOffset . x ;
this . _cache . y = this . game . camera . view . y + this . cameraOffset . y ;
2013-10-07 23:58:20 +00:00
}
else
{
this . _cache . x = this . x ;
this . _cache . y = this . y ;
}
2013-09-23 02:26:08 +00:00
2013-10-29 04:07:26 +00:00
this . world . setTo ( this . game . camera . x + this . worldTransform [ 2 ] , this . game . camera . y + this . worldTransform [ 5 ] ) ;
this . position . x = this . _cache . x ;
this . position . y = this . _cache . y ;
2013-09-20 12:55:33 +00:00
}
2013-11-01 02:07:21 +00:00
} ;
2013-09-20 12:55:33 +00:00
2013-10-25 14:02:21 +00:00
/ * *
* Changes the Texture the Sprite is using entirely . The old texture is removed and the new one is referenced or fetched from the Cache .
* This causes a WebGL texture update , so use sparingly or in low - intensity portions of your game .
*
* @ method Phaser . Sprite # loadTexture
* @ memberof Phaser . Sprite
* @ param { string | Phaser . RenderTexture | PIXI . Texture } key - This is the image or texture used by the Sprite during rendering . It can be a string which is a reference to the Cache entry , or an instance of a RenderTexture or PIXI . Texture .
* @ param { string | number } frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index .
* /
2013-10-10 08:03:38 +00:00
Phaser . Sprite . prototype . loadTexture = function ( key , frame ) {
this . key = key ;
if ( key instanceof Phaser . RenderTexture )
{
this . currentFrame = this . game . cache . getTextureFrame ( key . name ) ;
}
2013-10-24 20:21:00 +00:00
else if ( key instanceof PIXI . Texture )
{
this . currentFrame = frame ;
}
2013-10-10 08:03:38 +00:00
else
{
2013-10-24 20:21:00 +00:00
if ( typeof key === 'undefined' || this . game . cache . checkImageKey ( key ) === false )
2013-10-10 08:03:38 +00:00
{
key = '__default' ;
2013-10-24 20:21:00 +00:00
this . key = key ;
2013-10-10 08:03:38 +00:00
}
if ( this . game . cache . isSpriteSheet ( key ) )
{
this . animations . loadFrameData ( this . game . cache . getFrameData ( key ) ) ;
2013-10-24 20:21:00 +00:00
if ( typeof frame !== 'undefined' )
2013-10-10 08:03:38 +00:00
{
if ( typeof frame === 'string' )
{
this . frameName = frame ;
}
else
{
this . frame = frame ;
}
}
}
else
{
this . currentFrame = this . game . cache . getFrame ( key ) ;
2013-10-24 20:21:00 +00:00
this . setTexture ( PIXI . TextureCache [ key ] ) ;
2013-10-10 08:03:38 +00:00
}
}
2013-11-01 02:07:21 +00:00
} ;
2013-09-23 21:23:17 +00:00
2013-09-13 03:37:06 +00:00
/ * *
2013-10-01 12:54:29 +00:00
* Moves the sprite so its center is located on the given x and y coordinates .
2013-10-25 14:02:21 +00:00
* Doesn ' t change the anchor point of the sprite .
2013-10-01 12:54:29 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # centerOn
* @ memberof Phaser . Sprite
* @ param { number } x - The x coordinate ( in world space ) to position the Sprite at .
* @ param { number } y - The y coordinate ( in world space ) to position the Sprite at .
* @ return ( Phaser . Sprite ) This instance .
2013-10-01 12:54:29 +00:00
* /
2013-09-13 03:37:06 +00:00
Phaser . Sprite . prototype . centerOn = function ( x , y ) {
this . x = x + ( this . x - this . center . x ) ;
this . y = y + ( this . y - this . center . y ) ;
2013-10-25 14:02:21 +00:00
return this ;
2013-09-13 03:37:06 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-09-13 03:37:06 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Brings a 'dead' Sprite back to life , optionally giving it the health value specified .
* A resurrected Sprite has its alive , exists and visible properties all set to true .
* It will dispatch the onRevived event , you can listen to Sprite . events . onRevived for the signal .
2013-10-01 12:54:29 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # revive
* @ memberof Phaser . Sprite
* @ param { number } [ health = 1 ] - The health to give the Sprite .
* @ return ( Phaser . Sprite ) This instance .
2013-10-01 12:54:29 +00:00
* /
2013-10-10 08:03:38 +00:00
Phaser . Sprite . prototype . revive = function ( health ) {
if ( typeof health === 'undefined' ) { health = 1 ; }
2013-09-10 00:26:50 +00:00
this . alive = true ;
this . exists = true ;
this . visible = true ;
2013-10-10 08:03:38 +00:00
this . health = health ;
2013-10-25 14:02:21 +00:00
if ( this . events )
{
this . events . onRevived . dispatch ( this ) ;
}
return this ;
2013-09-10 00:26:50 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-09-10 00:26:50 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Kills a Sprite . A killed Sprite has its alive , exists and visible properties all set to false .
* It will dispatch the onKilled event , you can listen to Sprite . events . onKilled for the signal .
* Note that killing a Sprite is a way for you to quickly recycle it in a Sprite pool , it doesn ' t free it up from memory .
* If you don ' t need this Sprite any more you should call Sprite . destroy instead .
2013-10-01 12:54:29 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # kill
* @ memberof Phaser . Sprite
* @ return ( Phaser . Sprite ) This instance .
2013-10-01 12:54:29 +00:00
* /
2013-09-10 00:26:50 +00:00
Phaser . Sprite . prototype . kill = function ( ) {
this . alive = false ;
this . exists = false ;
this . visible = false ;
2013-10-23 12:30:22 +00:00
if ( this . events )
{
this . events . onKilled . dispatch ( this ) ;
}
2013-09-10 00:26:50 +00:00
2013-10-25 14:02:21 +00:00
return this ;
2013-11-01 02:07:21 +00:00
} ;
2013-09-10 00:26:50 +00:00
2013-10-13 00:29:57 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Destroys the Sprite . This removes it from its parent group , destroys the input , event and animation handlers if present
* and nulls its reference to game , freeing it up for garbage collection .
2013-10-13 00:29:57 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # destroy
* @ memberof Phaser . Sprite
2013-10-13 00:29:57 +00:00
* /
Phaser . Sprite . prototype . destroy = function ( ) {
if ( this . group )
{
this . group . remove ( this ) ;
}
2013-10-24 03:27:28 +00:00
if ( this . input )
{
this . input . destroy ( ) ;
}
if ( this . events )
{
this . events . destroy ( ) ;
}
if ( this . animations )
{
this . animations . destroy ( ) ;
}
2013-10-13 00:29:57 +00:00
this . alive = false ;
this . exists = false ;
this . visible = false ;
this . game = null ;
2013-11-01 02:07:21 +00:00
} ;
2013-10-13 00:29:57 +00:00
2013-10-10 08:03:38 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Damages the Sprite , this removes the given amount from the Sprites health property .
* If health is then taken below zero Sprite . kill is called .
2013-10-10 08:03:38 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # damage
* @ memberof Phaser . Sprite
* @ param { number } amount - The amount to subtract from the Sprite . health value .
* @ return ( Phaser . Sprite ) This instance .
2013-10-10 08:03:38 +00:00
* /
Phaser . Sprite . prototype . damage = function ( amount ) {
if ( this . alive )
{
this . health -= amount ;
if ( this . health < 0 )
{
this . kill ( ) ;
}
}
2013-10-25 14:02:21 +00:00
return this ;
2013-11-01 02:07:21 +00:00
} ;
2013-10-10 08:03:38 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Resets the Sprite . This places the Sprite at the given x / y world coordinates and then
* sets alive , exists , visible and renderable all to true . Also resets the outOfBounds state and health values .
* If the Sprite has a physics body that too is reset .
2013-10-01 12:54:29 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # reset
* @ memberof Phaser . Sprite
* @ param { number } x - The x coordinate ( in world space ) to position the Sprite at .
* @ param { number } y - The y coordinate ( in world space ) to position the Sprite at .
* @ param { number } [ health = 1 ] - The health to give the Sprite .
* @ return ( Phaser . Sprite ) This instance .
2013-10-01 12:54:29 +00:00
* /
2013-10-10 08:03:38 +00:00
Phaser . Sprite . prototype . reset = function ( x , y , health ) {
if ( typeof health === 'undefined' ) { health = 1 ; }
2013-09-03 16:28:12 +00:00
2013-09-09 12:29:33 +00:00
this . x = x ;
this . y = y ;
2013-10-04 13:41:15 +00:00
this . position . x = this . x ;
this . position . y = this . y ;
2013-09-09 12:29:33 +00:00
this . alive = true ;
this . exists = true ;
this . visible = true ;
2013-10-08 20:09:46 +00:00
this . renderable = true ;
2013-09-09 12:29:33 +00:00
this . _outOfBoundsFired = false ;
2013-10-10 08:03:38 +00:00
this . health = health ;
if ( this . body )
{
this . body . reset ( ) ;
}
2013-10-25 14:02:21 +00:00
return this ;
2013-09-09 16:01:59 +00:00
2013-11-01 02:07:21 +00:00
} ;
2013-09-03 16:28:12 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* Brings the Sprite to the top of the display list it is a child of . Sprites that are members of a Phaser . Group are only
* bought to the top of that Group , not the entire display list .
2013-10-01 12:54:29 +00:00
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # bringToTop
* @ memberof Phaser . Sprite
* @ return ( Phaser . Sprite ) This instance .
2013-10-01 12:54:29 +00:00
* /
2013-09-08 21:58:15 +00:00
Phaser . Sprite . prototype . bringToTop = function ( ) {
if ( this . group )
{
this . group . bringToTop ( this ) ;
}
else
{
this . game . world . bringToTop ( this ) ;
}
2013-10-25 14:02:21 +00:00
return this ;
2013-11-01 02:07:21 +00:00
} ;
2013-09-08 21:58:15 +00:00
2013-09-27 12:47:22 +00:00
/ * *
* Play an animation based on the given key . The animation should previously have been added via sprite . animations . add ( )
* If the requested animation is already playing this request will be ignored . If you need to reset an already running animation do so directly on the Animation object itself .
*
2013-10-25 14:02:21 +00:00
* @ method Phaser . Sprite # play
* @ memberof Phaser . Sprite
* @ param { string } name - The name of the animation to be played , e . g . "fire" , "walk" , "jump" .
* @ param { number } [ frameRate = null ] - The framerate to play the animation at . The speed is given in frames per second . If not provided the previously set frameRate of the Animation is used .
* @ param { boolean } [ loop = false ] - Should the animation be looped after playback . If not provided the previously set loop value of the Animation is used .
2013-10-09 12:36:57 +00:00
* @ param { boolean } [ killOnComplete = false ] - If set to true when the animation completes ( only happens if loop = false ) the parent Sprite will be killed .
2013-09-27 12:47:22 +00:00
* @ return { Phaser . Animation } A reference to playing Animation instance .
* /
2013-10-09 12:36:57 +00:00
Phaser . Sprite . prototype . play = function ( name , frameRate , loop , killOnComplete ) {
2013-09-27 12:47:22 +00:00
if ( this . animations )
{
2013-10-25 14:02:21 +00:00
return this . animations . play ( name , frameRate , loop , killOnComplete ) ;
2013-09-27 12:47:22 +00:00
}
2013-11-01 02:07:21 +00:00
} ;
2013-09-27 12:47:22 +00:00
2013-10-07 23:58:20 +00:00
/ * *
* Indicates the rotation of the Sprite , in degrees , from its original orientation . Values from 0 to 180 represent clockwise rotation ; values from 0 to - 180 represent counterclockwise rotation .
* Values outside this range are added to or subtracted from 360 to obtain a value within the range . For example , the statement player . angle = 450 is the same as player . angle = 90.
* If you wish to work in radians instead of degrees use the property Sprite . rotation instead .
* @ name Phaser . Sprite # angle
* @ property { number } angle - Gets or sets the Sprites angle of rotation in degrees .
* /
2013-08-30 19:05:29 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , 'angle' , {
get : function ( ) {
2013-10-07 23:58:20 +00:00
return Phaser . Math . wrapAngle ( Phaser . Math . radToDeg ( this . rotation ) ) ;
2013-08-30 19:05:29 +00:00
} ,
set : function ( value ) {
2013-10-07 23:58:20 +00:00
this . rotation = Phaser . Math . degToRad ( Phaser . Math . wrapAngle ( value ) ) ;
2013-08-30 19:05:29 +00:00
}
} ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ name Phaser . Sprite # frame
* @ property { number } frame - Gets or sets the current frame index and updates the Texture Cache for display .
2013-10-01 12:54:29 +00:00
* /
2013-08-30 03:20:14 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , "frame" , {
get : function ( ) {
return this . animations . frame ;
} ,
set : function ( value ) {
this . animations . frame = value ;
}
} ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ name Phaser . Sprite # frameName
* @ property { string } frameName - Gets or sets the current frame name and updates the Texture Cache for display .
2013-10-01 12:54:29 +00:00
* /
2013-08-30 03:20:14 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , "frameName" , {
get : function ( ) {
return this . animations . frameName ;
} ,
set : function ( value ) {
this . animations . frameName = value ;
}
} ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* @ name Phaser . Sprite # inCamera
* @ property { boolean } inCamera - Is this sprite visible to the camera or not ?
* @ readonly
2013-10-01 12:54:29 +00:00
* /
2013-09-02 22:22:24 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , "inCamera" , {
get : function ( ) {
return this . _cache . cameraVisible ;
}
} ) ;
2013-09-08 12:23:21 +00:00
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* The width of the sprite in pixels , setting this will actually modify the scale to acheive the value desired .
* If you wish to crop the Sprite instead see the Sprite . crop value .
*
* @ name Phaser . Sprite # width
* @ property { number } width - The width of the Sprite in pixels .
* /
2013-10-24 03:27:28 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , 'width' , {
2013-09-11 02:55:53 +00:00
2013-10-24 03:27:28 +00:00
get : function ( ) {
return this . scale . x * this . currentFrame . width ;
2013-09-11 02:55:53 +00:00
} ,
2013-10-24 03:27:28 +00:00
set : function ( value ) {
2013-10-24 20:21:00 +00:00
this . scale . x = value / this . currentFrame . width ;
this . _cache . scaleX = value / this . currentFrame . width ;
2013-10-24 03:27:28 +00:00
this . _width = value ;
2013-10-24 20:21:00 +00:00
2013-10-24 03:27:28 +00:00
}
2013-09-11 02:55:53 +00:00
2013-10-24 03:27:28 +00:00
} ) ;
2013-09-11 02:55:53 +00:00
2013-10-24 03:27:28 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* The height of the sprite in pixels , setting this will actually modify the scale to acheive the value desired .
* If you wish to crop the Sprite instead see the Sprite . crop value .
*
* @ name Phaser . Sprite # height
* @ property { number } height - The height of the Sprite in pixels .
* /
2013-10-24 03:27:28 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , 'height' , {
2013-09-11 02:55:53 +00:00
2013-10-24 03:27:28 +00:00
get : function ( ) {
2013-10-24 20:21:00 +00:00
return this . scale . y * this . currentFrame . height ;
2013-10-24 03:27:28 +00:00
} ,
set : function ( value ) {
2013-10-24 20:21:00 +00:00
this . scale . y = value / this . currentFrame . height ;
this . _cache . scaleY = value / this . currentFrame . height ;
2013-10-24 03:27:28 +00:00
this . _height = value ;
2013-10-24 20:21:00 +00:00
2013-09-11 02:55:53 +00:00
}
} ) ;
2013-10-01 12:54:29 +00:00
/ * *
2013-10-25 14:02:21 +00:00
* By default a Sprite won ' t process any input events at all . By setting inputEnabled to true the Phaser . InputHandler is
* activated for this Sprite instance and it will then start to process click / touch events and more .
*
* @ name Phaser . Sprite # inputEnabled
* @ property { boolean } inputEnabled - Set to true to allow this Sprite to receive input events , otherwise false .
2013-10-01 12:54:29 +00:00
* /
2013-09-08 12:23:21 +00:00
Object . defineProperty ( Phaser . Sprite . prototype , "inputEnabled" , {
get : function ( ) {
return ( this . input . enabled ) ;
} ,
set : function ( value ) {
if ( value )
{
if ( this . input . enabled == false )
{
this . input . start ( ) ;
}
}
else
{
if ( this . input . enabled )
{
this . input . stop ( ) ;
}
}
}
} ) ;