Phaser.Sprite = function (game, x, y, key, frame){ x = x || 0; y = y || 0; key = key || null ; frame = frame || null ; this.game = game; this.name = ''; this.type = Phaser.SPRITE; this.z = 0; this.events = new Phaser.Events(this); this.animations = new Phaser.AnimationManager(this); this.key = key; PIXI.Sprite.call(this, PIXI.TextureCache.__default); this.position.set(x, y); this.world = new Phaser.Point(x, y); this.autoCull = false ; this.input = null ; this.body = null ; this.alive = true ; this.health = 1; this.lifespan = 0; this.checkWorldBounds = false ; this.outOfBoundsKill = false ; this.debug = false ; this.cameraOffset = new Phaser.Point(); this.cropRect = null ; this._cache = [0, 0, 0, 0, 1, 0, 1, 0] ; this._crop = null ; this._frame = null ; this._bounds = new Phaser.Rectangle(); this.loadTexture(key, frame); } ; Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype); Phaser.Sprite.prototype.constructor = Phaser.Sprite; Phaser.Sprite.prototype.preUpdate = function (){ if (this._cache[4] === 1 && this.exists) { this.world.setTo(this.parent.position.x + this.position.x, this.parent.position.y + this.position.y); this.worldTransform.tx = this.world.x; this.worldTransform.ty = this.world.y; this._cache[0] = this.world.x; this._cache[1] = this.world.y; this._cache[2] = this.rotation; if (this.body) { this.body.preUpdate(); } this._cache[4] = 0; return false ; } this._cache[0] = this.world.x; this._cache[1] = this.world.y; this._cache[2] = this.rotation; if (!this.exists || !this.parent.exists) { this._cache[3] = -1; return false ; } if (this.lifespan > 0) { this.lifespan -= this.game.time.elapsed; if (this.lifespan <= 0) { this.kill(); return false ; } } if (this.autoCull || this.checkWorldBounds) { this._bounds.copyFrom(this.getBounds()); } if (this.autoCull) { this.renderable = this.game.world.camera.screenView.intersects(this._bounds); } if (this.checkWorldBounds) { if (this._cache[5] === 1 && this.game.world.bounds.intersects(this._bounds)) { this._cache[5] = 0; this.events.onEnterBounds.dispatch(this); } else if (this._cache[5] === 0 && !this.game.world.bounds.intersects(this._bounds)) { this._cache[5] = 1; this.events.onOutOfBounds.dispatch(this); if (this.outOfBoundsKill) { this.kill(); return false ; } } } this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty); if (this.visible) { this._cache[3] = this.game.stage.currentRenderOrderID++ ; } this.animations.update(); if (this.body) { this.body.preUpdate(); } for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ this.children[i].preUpdate(); } return true ; } ; Phaser.Sprite.prototype.update = function (){ } ; Phaser.Sprite.prototype.postUpdate = function (){ if (this.key instanceof Phaser.BitmapData) { this.key.render(); } if (this.exists && this.body) { this.body.postUpdate(); } if (this._cache[7] === 1) { this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x; this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y; } for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ this.children[i].postUpdate(); } } ; Phaser.Sprite.prototype.loadTexture = function (key, frame, stopAnimation){ frame = frame || 0; if (stopAnimation || typeof stopAnimation === 'undefined') { this.animations.stop(); } this.key = key; var setFrame = true ; var smoothed = this.smoothed; if (key instanceof Phaser.RenderTexture) { this.key = key.key; this.setTexture(key); } else if (key instanceof Phaser.BitmapData) { this.setTexture(key.texture); if (this.game.cache.getFrameData(key.key, Phaser.Cache.BITMAPDATA)) { setFrame = !this.animations.loadFrameData(this.game.cache.getFrameData(key.key, Phaser.Cache.BITMAPDATA), frame); } } else if (key instanceof PIXI.Texture) { this.setTexture(key); } else { if (key === null || typeof key === 'undefined') { this.key = '__default'; this.setTexture(PIXI.TextureCache[this.key]); } else if (typeof key === 'string' && !this.game.cache.checkImageKey(key)) { console.warn("Texture with key '" + key + "' not found."); this.key = '__missing'; this.setTexture(PIXI.TextureCache[this.key]); } else { this.setTexture(new PIXI.Texture(PIXI.BaseTextureCache[key])); setFrame = !this.animations.loadFrameData(this.game.cache.getFrameData(key), frame); } } if (setFrame) { this._frame = Phaser.Rectangle.clone(this.texture.frame); } if (!smoothed) { this.smoothed = false ; } } ; Phaser.Sprite.prototype.setFrame = function (frame){ this._frame = frame; this.texture.frame.x = frame.x; this.texture.frame.y = frame.y; this.texture.frame.width = frame.width; this.texture.frame.height = frame.height; this.texture.crop.x = frame.x; this.texture.crop.y = frame.y; this.texture.crop.width = frame.width; this.texture.crop.height = frame.height; if (frame.trimmed) { if (this.texture.trim) { this.texture.trim.x = frame.spriteSourceSizeX; this.texture.trim.y = frame.spriteSourceSizeY; this.texture.trim.width = frame.sourceSizeW; this.texture.trim.height = frame.sourceSizeH; } else { this.texture.trim = { x: frame.spriteSourceSizeX, y: frame.spriteSourceSizeY, width: frame.sourceSizeW, height: frame.sourceSizeH} ; } this.texture.width = frame.sourceSizeW; this.texture.height = frame.sourceSizeH; this.texture.frame.width = frame.sourceSizeW; this.texture.frame.height = frame.sourceSizeH; } else if (!frame.trimmed && this.texture.trim) { this.texture.trim = null ; } if (this.cropRect) { this.updateCrop(); } else { if (this.game.renderType === Phaser.WEBGL) { PIXI.WebGLRenderer.updateTextureFrame(this.texture); } } } ; Phaser.Sprite.prototype.resetFrame = function (){ if (this._frame) { this.setFrame(this._frame); } } ; Phaser.Sprite.prototype.crop = function (rect, copy){ if (typeof copy === 'undefined') { copy = false ; } if (rect) { if (copy && this.cropRect !== null ) { this.cropRect.setTo(rect.x, rect.y, rect.width, rect.height); } else if (copy && this.cropRect === null ) { this.cropRect = new Phaser.Rectangle(rect.x, rect.y, rect.width, rect.height); } else { this.cropRect = rect; } this.updateCrop(); } else { this._crop = null ; this.cropRect = null ; this.resetFrame(); } } ; Phaser.Sprite.prototype.updateCrop = function (){ if (!this.cropRect) { return ; } this._crop = Phaser.Rectangle.clone(this.cropRect, this._crop); this._crop.x += this._frame.x; this._crop.y += this._frame.y; var cx = Math.max(this._frame.x, this._crop.x); var cy = Math.max(this._frame.y, this._crop.y); var cw = Math.min(this._frame.right, this._crop.right) - cx; var ch = Math.min(this._frame.bottom, this._crop.bottom) - cy; this.texture.crop.x = cx; this.texture.crop.y = cy; this.texture.crop.width = cw; this.texture.crop.height = ch; this.texture.frame.width = Math.min(cw, this.cropRect.width); this.texture.frame.height = Math.min(ch, this.cropRect.height); this.texture.width = this.texture.frame.width; this.texture.height = this.texture.frame.height; if (this.game.renderType === Phaser.WEBGL) { PIXI.WebGLRenderer.updateTextureFrame(this.texture); } } ; 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 (destroyChildren){ if (this.game === null || this._cache[8] === 1) { return ; } if (typeof destroyChildren === 'undefined') { destroyChildren = true ; } this._cache[8] = 1; if (this.events) { this.events.onDestroy.dispatch(this); } if (this.parent) { if (this.parent instanceof Phaser.Group) { this.parent.remove(this); } else { this.parent.removeChild(this); } } if (this.input) { this.input.destroy(); } if (this.animations) { this.animations.destroy(); } if (this.body) { this.body.destroy(); } if (this.events) { this.events.destroy(); } var i = _AN_Read_length('length', this.children); if (destroyChildren) { while (i-- ){ this.children[i].destroy(destroyChildren); } } else { while (i-- ){ this.removeChild(this.children[i]); } } if (this._crop) { this._crop = null ; } if (this._frame) { this._frame = null ; } this.alive = false ; this.exists = false ; this.visible = false ; this.filters = null ; this.mask = null ; this.game = null ; this._cache[8] = 0; } ; 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.world.setTo(x, y); this.position.x = x; this.position.y = 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(x, y, false , false ); } this._cache[4] = 1; return this; } ; Phaser.Sprite.prototype.bringToTop = function (){ if (this.parent) { this.parent.bringToTop(this); } return this; } ; Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete){ if (this.animations) { return this.animations.play(name, frameRate, loop, killOnComplete); } } ; Phaser.Sprite.prototype.overlap = function (displayObject){ return Phaser.Rectangle.intersects(this.getBounds(), displayObject.getBounds()); } ; 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, "deltaX", { get: function (){ return this.world.x - this._cache[0]; } } ); Object.defineProperty(Phaser.Sprite.prototype, "deltaY", { get: function (){ return this.world.y - this._cache[1]; } } ); Object.defineProperty(Phaser.Sprite.prototype, "deltaZ", { get: function (){ return this.rotation - this._cache[2]; } } ); Object.defineProperty(Phaser.Sprite.prototype, "inWorld", { get: function (){ return this.game.world.bounds.intersects(this.getBounds()); } } ); Object.defineProperty(Phaser.Sprite.prototype, "inCamera", { get: function (){ return this.game.world.camera.screenView.intersects(this.getBounds()); } } ); 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, "renderOrderID", { get: function (){ return this._cache[3]; } } ); Object.defineProperty(Phaser.Sprite.prototype, "inputEnabled", { get: function (){ return (this.input && this.input.enabled); } , set: function (value){ if (value) { if (this.input === null ) { this.input = new Phaser.InputHandler(this); this.input.start(); } else if (this.input && !this.input.enabled) { this.input.start(); } } else { if (this.input && this.input.enabled) { this.input.stop(); } } } } ); Object.defineProperty(Phaser.Sprite.prototype, "exists", { get: function (){ return !!this._cache[6]; } , set: function (value){ if (value) { this._cache[6] = 1; if (this.body && this.body.type === Phaser.Physics.P2JS) { this.body.addToWorld(); } this.visible = true ; } else { this._cache[6] = 0; if (this.body && this.body.type === Phaser.Physics.P2JS) { this.body.removeFromWorld(); } this.visible = false ; } } } ); Object.defineProperty(Phaser.Sprite.prototype, "fixedToCamera", { get: function (){ return !!this._cache[7]; } , set: function (value){ if (value) { this._cache[7] = 1; this.cameraOffset.set(this.x, this.y); } else { this._cache[7] = 0; } } } ); Object.defineProperty(Phaser.Sprite.prototype, "smoothed", { get: function (){ return !this.texture.baseTexture.scaleMode; } , set: function (value){ if (value) { if (this.texture) { this.texture.baseTexture.scaleMode = 0; } } else { if (this.texture) { this.texture.baseTexture.scaleMode = 1; } } } } ); Object.defineProperty(Phaser.Sprite.prototype, "x", { get: function (){ return this.position.x; } , set: function (value){ this.position.x = value; if (this.body && this.body.type === Phaser.Physics.ARCADE && this.body.phase === 2) { this.body._reset = 1; } } } ); Object.defineProperty(Phaser.Sprite.prototype, "y", { get: function (){ return this.position.y; } , set: function (value){ this.position.y = value; if (this.body && this.body.type === Phaser.Physics.ARCADE && this.body.phase === 2) { this.body._reset = 1; } } } ); Object.defineProperty(Phaser.Sprite.prototype, "destroyPhase", { get: function (){ return !!this._cache[8]; } } );