forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.js
More file actions
1167 lines (938 loc) · 36.9 KB
/
Sprite.js
File metadata and controls
1167 lines (938 loc) · 36.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <[email protected]>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* @class Phaser.Sprite
*
* @classdesc Create a new `Sprite` object. Sprites are the lifeblood of your game, used for nearly everything visual.
*
* 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.
*
* @constructor
* @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|Phaser.BitmapData|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.
*/
Phaser.Sprite = function (game, x, y, key, frame) {
x = x || 0;
y = y || 0;
key = key || null;
frame = frame || null;
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
/**
* @property {boolean} exists - If exists = false then the Sprite isn't updated by the core game loop or physics subsystem at all.
* @default
*/
this.exists = true;
/**
* @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.
* @default
*/
this.alive = true;
/**
* @property {Phaser.Group} group - The parent Group of this Sprite. This is usually set after Sprite instantiation by the parent.
*/
this.group = null;
/**
* @property {string} name - The user defined name given to this Sprite.
* @default
*/
this.name = '';
/**
* @property {number} type - The const type of this object.
* @readonly
*/
this.type = Phaser.SPRITE;
/**
* @property {number} renderOrderID - Used by the Renderer and Input Manager to control picking order.
* @default
*/
this.renderOrderID = -1;
/**
* 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.
* @property {number} lifespan - The lifespan of the Sprite (in ms) before it will be killed.
* @default
*/
this.lifespan = 0;
/**
* @property {Phaser.Events} events - The Events you can subscribe to that are dispatched when certain things happen on this Sprite or its components.
*/
this.events = new Phaser.Events(this);
/**
* @property {Phaser.AnimationManager} animations - This manages animations of the sprite. You can modify animations through it (see Phaser.AnimationManager)
*/
this.animations = new Phaser.AnimationManager(this);
/**
* @property {Phaser.InputHandler} input - The Input Handler Component.
*/
this.input = new Phaser.InputHandler(this);
/**
* @property {string|Phaser.RenderTexture|Phaser.BitmapData|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, BitmapData or PIXI.Texture.
*/
this.key = key;
/**
* @property {Phaser.Frame} currentFrame - A reference to the currently displayed frame.
*/
this.currentFrame = null;
if (key instanceof Phaser.RenderTexture)
{
PIXI.Sprite.call(this, key);
this.currentFrame = this.game.cache.getTextureFrame(key.name);
}
else if (key instanceof Phaser.BitmapData)
{
PIXI.Sprite.call(this, key.texture, key.textureFrame);
this.currentFrame = key.textureFrame;
}
else if (key instanceof PIXI.Texture)
{
PIXI.Sprite.call(this, key);
this.currentFrame = frame;
}
else
{
if (key === null || typeof key === 'undefined')
{
key = '__default';
this.key = key;
}
else if (typeof key === 'string' && this.game.cache.checkImageKey(key) === false)
{
key = '__missing';
this.key = key;
}
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
if (this.game.cache.isSpriteSheet(key))
{
this.animations.loadFrameData(this.game.cache.getFrameData(key));
if (frame !== null)
{
if (typeof frame === 'string')
{
this.frameName = frame;
}
else
{
this.frame = frame;
}
}
}
else
{
this.currentFrame = this.game.cache.getFrame(key);
}
}
/**
* 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);
/**
* 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
*
* @property {Phaser.Point} anchor - The anchor around which rotation and scaling takes place.
*/
this.anchor = new Phaser.Point();
/**
* @property {number} x - The x coordinate in world space of this Sprite.
*/
this.x = x;
/**
* @property {number} y - The y coordinate in world space of this Sprite.
*/
this.y = y;
this.position.x = x;
this.position.y = y;
/**
* @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);
/**
* Should this Sprite be automatically culled if out of range of the camera?
* A culled sprite has its renderable property set to 'false'.
*
* @property {boolean} autoCull - A flag indicating if the Sprite should be automatically camera culled or not.
* @default
*/
this.autoCull = false;
/**
* @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.
*/
this.scale = new Phaser.Point(1, 1);
/**
* @property {object} _cache - A mini cache for storing all of the calculated values.
* @private
*/
this._cache = {
dirty: false,
// Transform cache
a00: -1,
a01: -1,
a02: -1,
a10: -1,
a11: -1,
a12: -1,
id: -1,
// Input specific transform cache
i01: -1,
i10: -1,
idi: -1,
// Bounds check
left: null,
right: null,
top: null,
bottom: null,
// delta cache
prevX: x,
prevY: y,
// The previous calculated position
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
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
halfWidth: Math.floor(this.currentFrame.sourceSizeW / 2),
halfHeight: Math.floor(this.currentFrame.sourceSizeH / 2),
// The width/height of the image, based on the un-modified frame size multiplied by the final calculated scale size
calcWidth: -1,
calcHeight: -1,
// The current frame details
// frameID: this.currentFrame.uuid, frameWidth: this.currentFrame.width, frameHeight: this.currentFrame.height,
frameID: -1,
frameWidth: this.currentFrame.width,
frameHeight: this.currentFrame.height,
// If this sprite visible to the camera (regardless of being set to visible or not)
cameraVisible: true,
// Crop cache
cropX: 0,
cropY: 0,
cropWidth: this.currentFrame.sourceSizeW,
cropHeight: this.currentFrame.sourceSizeH
};
/**
* @property {Phaser.Point} offset - Corner point defaults. Should not typically be modified.
*/
this.offset = new Phaser.Point();
/**
* @property {Phaser.Point} center - A Point containing the center coordinate of the Sprite. Takes rotation and scale into account.
*/
this.center = new Phaser.Point(x + Math.floor(this._cache.width / 2), y + Math.floor(this._cache.height / 2));
/**
* @property {Phaser.Point} topLeft - A Point containing the top left coordinate of the Sprite. Takes rotation and scale into account.
*/
this.topLeft = new Phaser.Point(x, y);
/**
* @property {Phaser.Point} topRight - A Point containing the top right coordinate of the Sprite. Takes rotation and scale into account.
*/
this.topRight = new Phaser.Point(x + this._cache.width, y);
/**
* @property {Phaser.Point} bottomRight - A Point containing the bottom right coordinate of the Sprite. Takes rotation and scale into account.
*/
this.bottomRight = new Phaser.Point(x + this._cache.width, y + this._cache.height);
/**
* @property {Phaser.Point} bottomLeft - A Point containing the bottom left coordinate of the Sprite. Takes rotation and scale into account.
*/
this.bottomLeft = new Phaser.Point(x, y + this._cache.height);
/**
* 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
*/
this.bounds = new Phaser.Rectangle(x, y, this._cache.width, this._cache.height);
/**
* @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.
*/
this.body = new Phaser.Physics.Arcade.Body(this);
/**
* @property {number} health - Health value. Used in combination with damage() to allow for quick killing of Sprites.
*/
this.health = 1;
/**
* @property {boolean} inWorld - This value is set to true if the Sprite is positioned within the World, otherwise false.
*/
this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds);
/**
* @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 100px away for example then set it to 100.
* @default
*/
this.inWorldThreshold = 0;
/**
* @property {boolean} outOfBoundsKill - If true the Sprite is killed as soon as Sprite.inWorld is false.
* @default
*/
this.outOfBoundsKill = false;
/**
* @property {boolean} _outOfBoundsFired - Internal flag.
* @private
* @default
*/
this._outOfBoundsFired = false;
/**
* 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;
/**
* @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.
*/
this.cameraOffset = new Phaser.Point(x, y);
/**
* You can crop the Sprites texture by modifying the crop properties. For example crop.width = 50 would set the Sprite to only render 50px wide.
* The crop is only applied if you have set Sprite.cropEnabled to true.
* @property {Phaser.Rectangle} crop - The crop Rectangle applied to the Sprite texture before rendering.
* @default
*/
this.crop = new Phaser.Rectangle(0, 0, this._cache.width, this._cache.height);
/**
* @property {boolean} cropEnabled - If true the Sprite.crop property is used to crop the texture before render. Set to false to disable.
* @default
*/
this.cropEnabled = false;
this.updateCache();
this.updateBounds();
/**
* @property {PIXI.Point} pivot - The pivot point of the displayObject that it rotates around.
*/
};
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
Phaser.Sprite.prototype.constructor = Phaser.Sprite;
/**
* Automatically called by World.preUpdate. Handles cache updates, lifespan checks, animation updates and physics updates.
*
* @method Phaser.Sprite#preUpdate
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.preUpdate = function() {
if (!this.exists || (this.group && !this.group.exists))
{
this.renderOrderID = -1;
// Skip children if not exists
return false;
}
if (this.lifespan > 0)
{
this.lifespan -= this.game.time.elapsed;
if (this.lifespan <= 0)
{
this.kill();
return false;
}
}
this._cache.dirty = false;
if (this.visible)
{
this.renderOrderID = this.game.world.currentRenderOrderID++;
}
this.updateCache();
this.updateAnimation();
this.updateCrop();
// Re-run the camera visibility check
if (this._cache.dirty || this.world.x !== this._cache.prevX || this.world.y !== this._cache.prevY)
{
this.updateBounds();
}
if (this.body)
{
this.body.preUpdate();
}
return true;
};
/**
* Internal function called by preUpdate.
*
* @method Phaser.Sprite#updateCache
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.updateCache = function() {
this._cache.prevX = this.world.x;
this._cache.prevY = this.world.y;
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]);
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)
{
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|
this._cache.a11 = this.worldTransform[4]; // scaleY d
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)
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
this._cache.a01 *= -1;
this._cache.a10 *= -1;
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);
this._cache.dirty = true;
}
this._cache.a02 = this.worldTransform[2]; // translateX tx
this._cache.a12 = this.worldTransform[5]; // translateY ty
};
/**
* Internal function called by preUpdate.
*
* @method Phaser.Sprite#updateAnimation
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.updateAnimation = function() {
if (this.animations.update() || (this.currentFrame && this.currentFrame.uuid != this._cache.frameID))
{
this._cache.frameID = this.currentFrame.uuid;
this._cache.frameWidth = this.texture.frame.width;
this._cache.frameHeight = this.texture.frame.height;
this._cache.width = this.currentFrame.width;
this._cache.height = this.currentFrame.height;
this._cache.halfWidth = Math.floor(this._cache.width / 2);
this._cache.halfHeight = Math.floor(this._cache.height / 2);
this._cache.dirty = true;
}
};
/**
* Internal function called by preUpdate.
*
* @method Phaser.Sprite#updateCrop
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.updateCrop = function() {
// 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))
{
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);
}
};
/**
* Internal function called by preUpdate.
*
* @method Phaser.Sprite#updateBounds
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.updateBounds = function() {
this.offset.setTo(this._cache.a02 - (this.anchor.x * this.width), this._cache.a12 - (this.anchor.y * this.height));
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);
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();
}
}
}
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.updateScale(this._cache.scaleX, this._cache.scaleY);
}
};
/**
* Gets the local position of a coordinate relative to the Sprite, factoring in rotation and scale.
* Mostly only used internally.
*
* @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.
*/
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
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;
return p;
};
/**
* 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.
*
* @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.
*/
Phaser.Sprite.prototype.getLocalUnmodifiedPosition = function(p, gx, gy) {
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);
return p;
};
/**
* Resets the Sprite.crop value back to the frame dimensions.
*
* @method Phaser.Sprite#resetCrop
* @memberof Phaser.Sprite
*/
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;
};
/**
* Internal function called by the World postUpdate cycle.
*
* @method Phaser.Sprite#postUpdate
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.postUpdate = function() {
if (this.key instanceof Phaser.BitmapData && this.key._dirty)
{
this.key.render();
}
if (this.exists)
{
// The sprite is positioned in this call, after taking into consideration motion updates and collision
if (this.body)
{
this.body.postUpdate();
}
if (this.fixedToCamera)
{
this._cache.x = this.game.camera.view.x + this.cameraOffset.x;
this._cache.y = this.game.camera.view.y + this.cameraOffset.y;
}
else
{
this._cache.x = this.x;
this._cache.y = this.y;
}
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;
}
};
/**
* 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|Phaser.BitmapData|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, BitmapData 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.
*/
Phaser.Sprite.prototype.loadTexture = function (key, frame) {
this.key = key;
if (key instanceof Phaser.RenderTexture)
{
this.currentFrame = this.game.cache.getTextureFrame(key.name);
}
else if (key instanceof Phaser.BitmapData)
{
this.setTexture(key.texture);
this.currentFrame = key.textureFrame;
}
else if (key instanceof PIXI.Texture)
{
this.currentFrame = frame;
}
else
{
if (typeof key === 'undefined' || this.game.cache.checkImageKey(key) === false)
{
key = '__default';
this.key = key;
}
if (this.game.cache.isSpriteSheet(key))
{
this.animations.loadFrameData(this.game.cache.getFrameData(key));
if (typeof frame !== 'undefined')
{
if (typeof frame === 'string')
{
this.frameName = frame;
}
else
{
this.frame = frame;
}
}
}
else
{
this.currentFrame = this.game.cache.getFrame(key);
this.setTexture(PIXI.TextureCache[key]);
}
}
};
/**
* Moves the sprite so its center is located on the given x and y coordinates.
* Doesn't change the anchor point of the sprite.
*
* @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.
*/
Phaser.Sprite.prototype.centerOn = function(x, y) {
if (this.fixedToCamera)
{
this.cameraOffset.x = x + (this.cameraOffset.x - this.center.x);
this.cameraOffset.y = y + (this.cameraOffset.y - this.center.y);
}
else
{
this.x = x + (this.x - this.center.x);
this.y = y + (this.y - this.center.y);
}
return this;
};
/**
* 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.
*
* @method Phaser.Sprite#revive
* @memberof Phaser.Sprite
* @param {number} [health=1] - The health to give the Sprite.
* @return (Phaser.Sprite) This instance.
*/
Phaser.Sprite.prototype.revive = function(health) {
if (typeof health === 'undefined') { health = 1; }
this.alive = true;
this.exists = true;
this.visible = true;
this.health = health;
if (this.events)
{
this.events.onRevived.dispatch(this);
}
return this;
};
/**
* 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.
*
* @method Phaser.Sprite#kill
* @memberof Phaser.Sprite
* @return (Phaser.Sprite) This instance.
*/
Phaser.Sprite.prototype.kill = function() {
this.alive = false;
this.exists = false;
this.visible = false;
if (this.events)
{
this.events.onKilled.dispatch(this);
}
return this;
};
/**
* 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.
*
* @method Phaser.Sprite#destroy
* @memberof Phaser.Sprite
*/
Phaser.Sprite.prototype.destroy = function() {
if (this.filters)
{
this.filters = null;
}
if (this.group)
{
this.group.remove(this);
}
if (this.input)
{
this.input.destroy();
}
if (this.events)
{
this.events.destroy();
}
if (this.animations)
{
this.animations.destroy();
}
this.alive = false;
this.exists = false;
this.visible = false;
this.game = null;
};
/**
* Damages the Sprite, this removes the given amount from the Sprites health property.
* If health is then taken below zero Sprite.kill is called.
*
* @method Phaser.Sprite#damage
* @memberof Phaser.Sprite
* @param {number} amount - The amount to subtract from the Sprite.health value.
* @return (Phaser.Sprite) This instance.
*/
Phaser.Sprite.prototype.damage = function(amount) {
if (this.alive)
{
this.health -= amount;
if (this.health < 0)
{
this.kill();
}
}
return this;
};
/**
* 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.
*
* @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.
*/
Phaser.Sprite.prototype.reset = function(x, y, health) {
if (typeof health === 'undefined') { health = 1; }
this.x = x;
this.y = y;
this.position.x = this.x;
this.position.y = this.y;
this.alive = true;
this.exists = true;
this.visible = true;
this.renderable = true;
this._outOfBoundsFired = false;
this.health = health;
if (this.body)
{
this.body.reset();
}
return this;
};
/**
* 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.
*
* @method Phaser.Sprite#bringToTop
* @memberof Phaser.Sprite
* @return (Phaser.Sprite) This instance.
*/
Phaser.Sprite.prototype.bringToTop = function() {
if (this.group)
{
this.group.bringToTop(this);
}
else
{
this.game.world.bringToTop(this);
}
return this;
};
/**
* 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.
*
* @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.
* @param {boolean} [killOnComplete=false] - If set to true when the animation completes (only happens if loop=false) the parent Sprite will be killed.
* @return {Phaser.Animation} A reference to playing Animation instance.
*/
Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete) {
if (this.animations)
{
return this.animations.play(name, frameRate, loop, killOnComplete);
}
};
/**
* Returns the delta x value. The difference between Sprite.x now and in the previous step.
* @name Phaser.Sprite#deltaX
* @property {number} deltaX - The delta value. Positive if the motion was to the right, negative if to the left.
* @readonly
*/