Phaser.Sprite = function (game, x, y, key, frame){ x = x || 0; y = y || 0; key = key || null ; frame = frame || null ; this.game = game; this.exists = true ; this.alive = true ; this.group = null ; this.name = ''; this.type = Phaser.SPRITE; this.renderOrderID = -1; this.lifespan = 0; this.events = new Phaser.Events(this); this.animations = new Phaser.AnimationManager(this); this.input = new Phaser.InputHandler(this); this.key = key; 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 PIXI.Texture) { PIXI.Sprite.call(this, key); this.currentFrame = frame; } else { if (key == null || this.game.cache.checkImageKey(key) == false ) { key = '__default'; 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); } } this.textureRegion = new Phaser.Rectangle(this.texture.frame.x, this.texture.frame.y, this.texture.frame.width, this.texture.frame.height); this.anchor = new Phaser.Point(); this.x = x; this.y = y; this.position.x = x; this.position.y = y; this.world = new Phaser.Point(x, y); this.autoCull = false ; this.scale = new Phaser.Point(1, 1); this._cache = { dirty: false , a00: -1, a01: -1, a02: -1, a10: -1, a11: -1, a12: -1, id: -1, i01: -1, i10: -1, idi: -1, left: null , right: null , top: null , bottom: null , prevX: x, prevY: y, x: -1, y: -1, scaleX: 1, scaleY: 1, width: this.currentFrame.sourceSizeW, height: this.currentFrame.sourceSizeH, halfWidth: Math.floor(this.currentFrame.sourceSizeW / 2), halfHeight: Math.floor(this.currentFrame.sourceSizeH / 2), calcWidth: -1, calcHeight: -1, frameID: -1, frameWidth: this.currentFrame.width, frameHeight: this.currentFrame.height, cameraVisible: true , cropX: 0, cropY: 0, cropWidth: this.currentFrame.sourceSizeW, cropHeight: this.currentFrame.sourceSizeH} ; this.offset = new Phaser.Point(); this.center = new Phaser.Point(x + Math.floor(this._cache.width / 2), y + Math.floor(this._cache.height / 2)); this.topLeft = new Phaser.Point(x, y); this.topRight = new Phaser.Point(x + this._cache.width, y); this.bottomRight = new Phaser.Point(x + this._cache.width, y + this._cache.height); this.bottomLeft = new Phaser.Point(x, y + this._cache.height); this.bounds = new Phaser.Rectangle(x, y, this._cache.width, this._cache.height); this.body = new Phaser.Physics.Arcade.Body(this); this.health = 1; this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds); this.inWorldThreshold = 0; this.outOfBoundsKill = false ; this._outOfBoundsFired = false ; this.fixedToCamera = false ; this.cameraOffset = new Phaser.Point(); this.crop = new Phaser.Rectangle(0, 0, this._cache.width, this._cache.height); this.cropEnabled = false ; this.updateCache(); this.updateBounds(); } ; Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype); Phaser.Sprite.prototype.constructor = Phaser.Sprite; Phaser.Sprite.prototype.preUpdate = function (){ if (!this.exists) { this.renderOrderID = -1; return ; } if (this.lifespan > 0) { this.lifespan -= this.game.time.elapsed; if (this.lifespan <= 0) { this.kill(); return ; } } this._cache.dirty = false ; if (this.visible) { this.renderOrderID = this.game.world.currentRenderOrderID++ ; } this.updateCache(); this.updateAnimation(); this.updateCrop(); if (this._cache.dirty || this.world.x !== this._cache.prevX || this.world.y !== this._cache.prevY) { this.updateBounds(); } if (this.body) { this.body.preUpdate(); } } ; 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]; this._cache.a01 = this.worldTransform[1]; this._cache.a10 = this.worldTransform[3]; this._cache.a11 = this.worldTransform[4]; this._cache.i01 = this.worldTransform[1]; this._cache.i10 = this.worldTransform[3]; this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); 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]; this._cache.a12 = this.worldTransform[5]; } ; 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 ; } } ; Phaser.Sprite.prototype.updateCrop = function (){ 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); } } ; 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 ) { this.inWorld = Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, this.inWorldThreshold); if (this.inWorld) { this._outOfBoundsFired = false ; } } else { 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) { this.renderable = this._cache.cameraVisible; } if (this.body) { this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY); } } ; 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; } ; 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; } ; 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 ; } ; Phaser.Sprite.prototype.postUpdate = function (){ if (this.exists) { 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; } } ; 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 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]); } } } ; Phaser.Sprite.prototype.centerOn = function (x, y){ this.x = x + (this.x - this.center.x); this.y = y + (this.y - this.center.y); return this; } ; 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; } ; Phaser.Sprite.prototype.kill = function (){ this.alive = false ; this.exists = false ; this.visible = false ; if (this.events) { this.events.onKilled.dispatch(this); } return this; } ; Phaser.Sprite.prototype.destroy = function (){ 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 ; } ; Phaser.Sprite.prototype.damage = function (amount){ if (this.alive) { this.health -= amount; if (this.health < 0) { this.kill(); } } return this; } ; 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; } ; Phaser.Sprite.prototype.bringToTop = function (){ if (this.group) { this.group.bringToTop(this); } else { this.game.world.bringToTop(this); } return this; } ; Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete){ if (this.animations) { return this.animations.play(name, frameRate, loop, killOnComplete); } } ; Object.defineProperty(Phaser.Sprite.prototype, 'angle', { get: function (){ return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.rotation)); } , set: function (value){ this.rotation = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value)); } } ); Object.defineProperty(Phaser.Sprite.prototype, "frame", { get: function (){ return this.animations.frame; } , set: function (value){ this.animations.frame = value; } } ); Object.defineProperty(Phaser.Sprite.prototype, "frameName", { get: function (){ return this.animations.frameName; } , set: function (value){ this.animations.frameName = value; } } ); Object.defineProperty(Phaser.Sprite.prototype, "inCamera", { get: function (){ return this._cache.cameraVisible; } } ); Object.defineProperty(Phaser.Sprite.prototype, 'width', { get: function (){ return this.scale.x * this.currentFrame.width; } , set: function (value){ this.scale.x = value / this.currentFrame.width; this._cache.scaleX = value / this.currentFrame.width; this._width = value; } } ); Object.defineProperty(Phaser.Sprite.prototype, 'height', { get: function (){ return this.scale.y * this.currentFrame.height; } , set: function (value){ this.scale.y = value / this.currentFrame.height; this._cache.scaleY = value / this.currentFrame.height; this._height = value; } } ); 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(); } } } } );