Camera.flash is a new function that makes the camera 'flash' over the top of your game. It works by filling the game with the solid fill color specified, and then fading it away to alpha 0 over the duration given. This is great for things like hit effects. You can listen for the Camera.onflashComplete Signal.

Camera.fade is a new function that makes the camera fade to the color given, over the course of the duration specified. This is great for things like transitioning from one State to another. You can listen for the Camera.onFadeComplete Signal.

Camera.resetFX resets any active FX, such as a fade or flash and immediately clears it. Useful to calling after a fade in order to remove the fade from the Stage.

Phaser.Camera.ENABLE_FX is a const that controls if the Camera FX are available or not. It's `true` by default, but if you set it to `false` before boot then it won't create the Graphics object required to process the effects.
This commit is contained in:
photonstorm 2016-04-09 04:05:07 +01:00
parent 2ea94c83e6
commit 7102a34706
4 changed files with 230 additions and 13 deletions

View file

@ -332,6 +332,10 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
* You can now pass a TilemapLayer as a Texture to a TileSprite. A limitation of this is that if you pass it to a TileSprite it will make a fill pattern from the TilemapLayer at that instant it's passed, and it won't keep track of the layer in future should it update (thanks @jdnichollsc #1989)
* Camera has a new property: `lerp`. This is a Point object, that allows you to control the amount of horizontal and vertical smoothing to be applied to the camera when it tracks a Sprite. It works both with and without deadzones, and is turned off by default. Set it to low values such as 0.1 for really smooth motion tracking (thanks to @WombatTurkey for the idea of adding this)
* Camera.shake is a new function that creates a camera shake effect. You can specify the intensity, duration and direction of the effect. You can also set if it should shake the camera out of bounds or not.
* Camera.flash is a new function that makes the camera 'flash' over the top of your game. It works by filling the game with the solid fill color specified, and then fading it away to alpha 0 over the duration given. This is great for things like hit effects. You can listen for the Camera.onflashComplete Signal.
* Camera.fade is a new function that makes the camera fade to the color given, over the course of the duration specified. This is great for things like transitioning from one State to another. You can listen for the Camera.onFadeComplete Signal.
* Camera.resetFX resets any active FX, such as a fade or flash and immediately clears it. Useful to calling after a fade in order to remove the fade from the Stage.
* Phaser.Camera.ENABLE_FX is a const that controls if the Camera FX are available or not. It's `true` by default, but if you set it to `false` before boot then it won't create the Graphics object required to process the effects.
### New Arcade Physics Features

View file

@ -82,12 +82,12 @@ Phaser.Camera = function (game, id, x, y, width, height) {
this.target = null;
/**
* @property {PIXI.DisplayObject} displayObject - The display object to which all game objects are added. Set by World.boot
* @property {PIXI.DisplayObject} displayObject - The display object to which all game objects are added. Set by World.boot.
*/
this.displayObject = null;
/**
* @property {Phaser.Point} scale - The scale of the display object to which all game objects are added. Set by World.boot
* @property {Phaser.Point} scale - The scale of the display object to which all game objects are added. Set by World.boot.
*/
this.scale = null;
@ -109,9 +109,30 @@ Phaser.Camera = function (game, id, x, y, width, height) {
this.lerp = new Phaser.Point(1, 1);
/**
* @property {Phaser.Signal} shakeOnComplete - This signal is dispatched when the camera shake effect completes.
* @property {Phaser.Signal} onShakeComplete - This signal is dispatched when the camera shake effect completes.
*/
this.shakeOnComplete = new Phaser.Signal();
this.onShakeComplete = new Phaser.Signal();
/**
* @property {Phaser.Signal} onFlashComplete - This signal is dispatched when the camera flash effect completes.
*/
this.onFlashComplete = new Phaser.Signal();
/**
* This signal is dispatched when the camera fade effect completes.
* When the fade effect completes you will be left with the screen black (or whatever
* color you faded to). In order to reset this call `Camera.resetFX`. This is called
* automatically when you change State.
* @property {Phaser.Signal} onFadeComplete
*/
this.onFadeComplete = new Phaser.Signal();
/**
* The Graphics object used to handle camera fx such as fade and flash.
* @property {Phaser.Graphics} fx
* @protected
*/
this.fx = null;
/**
* @property {Phaser.Point} _targetPosition - Internal point used to calculate target position.
@ -147,6 +168,18 @@ Phaser.Camera = function (game, id, x, y, width, height) {
y: 0
};
/**
* @property {number} _fxDuration - FX duration timer.
* @private
*/
this._fxDuration = 0;
/**
* @property {number} _fxType - The FX type running.
* @private
*/
this._fxType = 0;
};
/**
@ -191,8 +224,37 @@ Phaser.Camera.SHAKE_HORIZONTAL = 5;
*/
Phaser.Camera.SHAKE_VERTICAL = 6;
/**
* @constant
* @type {boolean}
*/
Phaser.Camera.ENABLE_FX = true;
Phaser.Camera.prototype = {
/**
* Called automatically by Phaser.World.
*
* @method Phaser.Camera#boot
* @private
*/
boot: function () {
this.displayObject = this.game.world;
this.scale = this.game.world.scale;
this.game.camera = this;
if (Phaser.Graphics && Phaser.Camera.ENABLE_FX)
{
this.fx = new Phaser.Graphics(this.game);
this.game.stage.addChild(this.fx);
}
},
/**
* Camera preUpdate. Sets the total view counter to zero.
*
@ -298,7 +360,7 @@ Phaser.Camera.prototype = {
* spacing on the x and y axis each frame. You can control the intensity and duration
* of the effect, and if it should effect both axis or just one.
*
* When the shake effect ends the signal Camera.shakeOnComplete is dispatched.
* When the shake effect ends the signal Camera.onShakeComplete is dispatched.
*
* @method Phaser.Camera#shake
* @param {float} [intensity=0.05] - The intensity of the camera shake. Given as a percentage of the camera size representing the maximum distance that the camera can move while shaking.
@ -336,6 +398,91 @@ Phaser.Camera.prototype = {
},
/**
* This creates a camera flash effect. It works by filling the game with the solid fill
* color specified, and then fading it away to alpha 0 over the duration given.
*
* You can use this for things such as hit feedback effects.
*
* When the effect ends the signal Camera.onFlashComplete is dispatched.
*
* @method Phaser.Camera#flash
* @param {numer} [color=0xffffff] - The color of the flash effect. I.e. 0xffffff for white, 0xff0000 for red, etc.
* @param {number} [duration=500] - The duration of the flash effect in milliseconds.
* @param {boolean} [force=false] - If a camera flash or fade effect is already running and force is true it will replace the previous effect, resetting the duration.
* @return {boolean} True if the effect was started, otherwise false.
*/
flash: function (color, duration, force) {
if (color === undefined) { color = 0xffffff; }
if (duration === undefined) { duration = 500; }
if (force === undefined) { force = false; }
if (!this.fx || (!force && this._fxDuration > 0))
{
return false;
}
this.fx.clear();
this.fx.beginFill(color);
this.fx.drawRect(0, 0, this.width, this.height);
this.fx.endFill();
this.fx.alpha = 1;
this._fxDuration = duration;
this._fxType = 0;
return true;
},
/**
* This creates a camera fade effect. It works by filling the game with the
* color specified, over the duration given, ending with a solid fill.
*
* You can use this for things such as transitioning to a new scene.
*
* The game will be left 'filled' at the end of this effect, likely obscuring
* everything. In order to reset it you can call `Camera.resetFX` and it will clear the
* fade. Or you can call `Camera.flash` with the same color as the fade, and it will
* reverse the process, bringing the game back into view again.
*
* When the effect ends the signal Camera.onFadeComplete is dispatched.
*
* @method Phaser.Camera#fade
* @param {numer} [color=0x000000] - The color the game will fade to. I.e. 0x000000 for black, 0xff0000 for red, etc.
* @param {number} [duration=500] - The duration of the fade in milliseconds.
* @param {boolean} [force=false] - If a camera flash or fade effect is already running and force is true it will replace the previous effect, resetting the duration.
* @return {boolean} True if the effect was started, otherwise false.
*/
fade: function (color, duration, force) {
if (color === undefined) { color = 0x000000; }
if (duration === undefined) { duration = 500; }
if (force === undefined) { force = false; }
if (!this.fx || (!force && this._fxDuration > 0))
{
return false;
}
this.fx.clear();
this.fx.beginFill(color);
this.fx.drawRect(0, 0, this.width, this.height);
this.fx.endFill();
this.fx.alpha = 0;
this._fxDuration = duration;
this._fxType = 1;
return true;
},
/**
* The camera update loop. This is called automatically by the core game loop.
*
@ -344,6 +491,11 @@ Phaser.Camera.prototype = {
*/
update: function () {
if (this._fxDuration > 0)
{
this.updateFX();
}
if (this.target)
{
this.updateTarget();
@ -371,6 +523,41 @@ Phaser.Camera.prototype = {
},
/**
* Update the camera flash and fade effects.
*
* @method Phaser.Camera#updateFX
* @private
*/
updateFX: function () {
if (this._fxType === 0)
{
// flash
this.fx.alpha -= this.game.time.elapsedMS / this._fxDuration;
if (this.fx.alpha <= 0)
{
this._fxDuration = 0;
this.fx.alpha = 0;
this.onFlashComplete.dispatch();
}
}
else
{
// fade
this.fx.alpha += this.game.time.elapsedMS / this._fxDuration;
if (this.fx.alpha >= 1)
{
this._fxDuration = 0;
this.fx.alpha = 1;
this.onFadeComplete.dispatch();
}
}
},
/**
* Update the camera shake effect.
*
@ -383,7 +570,7 @@ Phaser.Camera.prototype = {
if (this._shake.duration <= 0)
{
this.shakeOnComplete.dispatch();
this.onShakeComplete.dispatch();
this._shake.x = 0;
this._shake.y = 0;
}
@ -562,15 +749,38 @@ Phaser.Camera.prototype = {
/**
* Resets the camera back to 0,0 and un-follows any object it may have been tracking.
* Also immediately resets any camera effects that may have been running such as
* shake, flash or fade.
*
* @method Phaser.Camera#reset
*/
reset: function () {
this.target = null;
this.view.x = 0;
this.view.y = 0;
this._shake.duration = 0;
this.resetFX();
},
/**
* Resets any active FX, such as a fade or flash and immediately clears it.
* Useful to calling after a fade in order to remove the fade from the Stage.
*
* @method Phaser.Camera#resetFX
*/
resetFX: function () {
this.fx.clear();
this.fx.alpha = 0;
this._fxDuration = 0;
}
};

View file

@ -67,14 +67,10 @@ Phaser.World.prototype.boot = function () {
this.camera = new Phaser.Camera(this.game, 0, 0, 0, this.game.width, this.game.height);
this.camera.displayObject = this;
this.camera.scale = this.scale;
this.game.camera = this.camera;
this.game.stage.addChild(this);
this.camera.boot();
};
/**

View file

@ -583,19 +583,23 @@ declare module Phaser {
static SHAKE_BOTH: number;
static SHAKE_HORIZONTAL: number;
static SHAKE_VERTICAL: number;
static ENABLE_FX: number;
atLimit: { x: boolean; y: boolean; };
bounds: Phaser.Rectangle;
deadzone: Phaser.Rectangle;
displayObject: PIXI.DisplayObject;
id: number;
fx: Phaser.Graphics;
game: Phaser.Game;
height: number;
lerp: Phaser.Point;
position: Phaser.Point;
roundPx: boolean;
scale: Phaser.Point;
shakeOnComplete: Phaser.Signal;
onFadeComplete: Phaser.Signal;
onFlashComplete: Phaser.Signal;
onShakeComplete: Phaser.Signal;
target: Phaser.Sprite;
totalInView: number;
view: Phaser.Rectangle;
@ -606,10 +610,13 @@ declare module Phaser {
y: number;
checkBounds(): void;
fade(color?: number, duration?: number, force?: boolean): boolean;
flash(color?: number, duration?: number, force?: boolean): boolean;
focusOn(displayObject: PIXI.DisplayObject): void;
focusOnXY(x: number, y: number): void;
follow(target: Phaser.Sprite, style?: number, lerpX?: number, lerpY?: number): void;
reset(): void;
resetFX(): void;
setBoundsToWorld(): void;
setPosition(x: number, y: number): void;
setSize(width: number, height: number): void;