Phaser.Image = 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.name = ''; this.type = Phaser.IMAGE; 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.alive = true ; this.autoCull = false ; this.input = null ; this.debug = false ; this.cameraOffset = new Phaser.Point(); this.cropRect = null ; this._cache = [0, 0, 0, 0, 1, 0, 1, 0, 0] ; this._crop = null ; this._frame = null ; this._bounds = new Phaser.Rectangle(); this.loadTexture(key, frame); } ; Phaser.Image.prototype = Object.create(PIXI.Sprite.prototype); Phaser.Image.prototype.constructor = Phaser.Image; Phaser.Image.prototype.preUpdate = function (){ 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.autoCull) { this._bounds.copyFrom(this.getBounds()); this.renderable = this.game.world.camera.screenView.intersects(this._bounds); } 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++ ; } for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ this.children[i].preUpdate(); } return true ; } ; Phaser.Image.prototype.update = function (){ } ; Phaser.Image.prototype.postUpdate = function (){ if (this.key instanceof Phaser.BitmapData) { this.key.render(); } 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.Image.prototype.loadTexture = function (key, frame){ frame = frame || 0; 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); } 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.Image.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.Image.prototype.resetFrame = function (){ if (this._frame) { this.setFrame(this._frame); } } ; Phaser.Image.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.Image.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.Image.prototype.revive = function (){ this.alive = true ; this.exists = true ; this.visible = true ; if (this.events) { this.events.onRevived.dispatch(this); } return this; } ; Phaser.Image.prototype.kill = function (){ this.alive = false ; this.exists = false ; this.visible = false ; if (this.events) { this.events.onKilled.dispatch(this); } return this; } ; Phaser.Image.prototype.destroy = function (destroyChildren){ if (this.game === null || this.destroyPhase) { 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.events) { this.events.destroy(); } if (this.input) { this.input.destroy(); } if (this.animations) { this.animations.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]); } } this.alive = false ; this.exists = false ; this.visible = false ; this.filters = null ; this.mask = null ; this.game = null ; this._cache[8] = 0; } ; Phaser.Image.prototype.reset = function (x, y){ 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 ; return this; } ; Phaser.Image.prototype.bringToTop = function (){ if (this.parent) { this.parent.bringToTop(this); } return this; } ; Object.defineProperty(Phaser.Image.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.Image.prototype, "deltaX", { get: function (){ return this.world.x - this._cache[0]; } } ); Object.defineProperty(Phaser.Image.prototype, "deltaY", { get: function (){ return this.world.y - this._cache[1]; } } ); Object.defineProperty(Phaser.Image.prototype, "deltaZ", { get: function (){ return this.rotation - this._cache[2]; } } ); Object.defineProperty(Phaser.Image.prototype, "inWorld", { get: function (){ return this.game.world.bounds.intersects(this.getBounds()); } } ); Object.defineProperty(Phaser.Image.prototype, "inCamera", { get: function (){ return this.game.world.camera.screenView.intersects(this.getBounds()); } } ); Object.defineProperty(Phaser.Image.prototype, "frame", { get: function (){ return this._frame; } , set: function (value){ if (value !== this.frame) { var frameData = this.game.cache.getFrameData(this.key); if (frameData && value < frameData.total && frameData.getFrame(value)) { this.setTexture(PIXI.TextureCache[frameData.getFrame(value).uuid]); this._frame = value; } } } } ); Object.defineProperty(Phaser.Image.prototype, "frameName", { get: function (){ return this._frameName; } , set: function (value){ if (value !== this.frameName) { var frameData = this.game.cache.getFrameData(this.key); if (frameData && frameData.getFrameByName(value)) { this.setTexture(PIXI.TextureCache[frameData.getFrameByName(value).uuid]); this._frameName = value; } } } } ); Object.defineProperty(Phaser.Image.prototype, "renderOrderID", { get: function (){ return this._cache[3]; } } ); Object.defineProperty(Phaser.Image.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.Image.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.Image.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.Image.prototype, "destroyPhase", { get: function (){ return !!this._cache[8]; } } );