mirror of
https://github.com/wassname/phaser.git
synced 2026-08-03 13:10:25 +08:00
Fixed a fantastic FrameData bug. Also added support to the Emitter to handle multiple image keys and/or frames.
This commit is contained in:
@@ -9,6 +9,10 @@
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
*/
|
||||
Phaser.Animation.FrameData = function () {
|
||||
|
||||
this._frames = [];
|
||||
this._frameNames = [];
|
||||
|
||||
};
|
||||
|
||||
Phaser.Animation.FrameData.prototype = {
|
||||
|
||||
+20
-11
@@ -23,7 +23,8 @@ Phaser.Animation.Parser = {
|
||||
// How big is our image?
|
||||
var img = game.cache.getImage(key);
|
||||
|
||||
if (img == null) {
|
||||
if (img == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -33,13 +34,15 @@ Phaser.Animation.Parser = {
|
||||
var column = Math.round(height / frameHeight);
|
||||
var total = row * column;
|
||||
|
||||
if (frameMax !== -1) {
|
||||
if (frameMax !== -1)
|
||||
{
|
||||
total = frameMax;
|
||||
}
|
||||
|
||||
// Zero or smaller than frame sizes?
|
||||
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0) {
|
||||
throw new Error("AnimationLoader.parseSpriteSheet: width/height zero or width/height < given frameWidth/frameHeight");
|
||||
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0)
|
||||
{
|
||||
console.warn("Phaser.Animation.Parser.spriteSheet: width/height zero or width/height < given frameWidth/frameHeight");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -82,9 +85,11 @@ Phaser.Animation.Parser = {
|
||||
JSONData: function (game, json, cacheKey) {
|
||||
|
||||
// Malformed?
|
||||
if (!json['frames']) {
|
||||
if (!json['frames'])
|
||||
{
|
||||
console.warn("Phaser.Animation.Parser.JSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
|
||||
console.log(json);
|
||||
throw new Error("Phaser.AnimationLoader.parseJSONData: Invalid Texture Atlas JSON given, missing 'frames' array");
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's create some frames then
|
||||
@@ -143,14 +148,16 @@ Phaser.Animation.Parser = {
|
||||
JSONDataHash: function (game, json, cacheKey) {
|
||||
|
||||
// Malformed?
|
||||
if (!json['frames']) {
|
||||
if (!json['frames'])
|
||||
{
|
||||
console.warn("Phaser.Animation.Parser.JSONDataHash: Invalid Texture Atlas JSON given, missing 'frames' object");
|
||||
console.log(json);
|
||||
throw new Error("Phaser.AnimationLoader.parseJSONDataHash: Invalid Texture Atlas JSON given, missing 'frames' object");
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's create some frames then
|
||||
var data = new Phaser.Animation.FrameData();
|
||||
|
||||
|
||||
// By this stage frames is a fully parsed array
|
||||
var frames = json['frames'];
|
||||
var newFrame;
|
||||
@@ -204,8 +211,10 @@ Phaser.Animation.Parser = {
|
||||
XMLData: function (game, xml, cacheKey) {
|
||||
|
||||
// Malformed?
|
||||
if (!xml.getElementsByTagName('TextureAtlas')) {
|
||||
throw new Error("Phaser.AnimationLoader.parseXMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
|
||||
if (!xml.getElementsByTagName('TextureAtlas'))
|
||||
{
|
||||
console.warn("Phaser.Animation.Parser.XMLData: Invalid Texture Atlas XML given, missing <TextureAtlas> tag");
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's create some frames then
|
||||
|
||||
@@ -305,6 +305,7 @@ Phaser.Game.prototype = {
|
||||
this.input = new Phaser.Input(this);
|
||||
this.sound = new Phaser.SoundManager(this);
|
||||
this.physics = new Phaser.Physics.Arcade(this);
|
||||
this.particles = new Phaser.Particles(this);
|
||||
this.plugins = new Phaser.PluginManager(this, this);
|
||||
this.net = new Phaser.Net(this);
|
||||
this.debug = new Phaser.Utils.Debug(this);
|
||||
@@ -391,6 +392,7 @@ Phaser.Game.prototype = {
|
||||
this.tweens.update();
|
||||
this.sound.update();
|
||||
this.world.update();
|
||||
this.particles.update();
|
||||
this.state.update();
|
||||
this.plugins.update();
|
||||
|
||||
|
||||
@@ -91,4 +91,10 @@ Phaser.GameObjectFactory.prototype = {
|
||||
|
||||
},
|
||||
|
||||
emitter: function (x, y, maxParticles) {
|
||||
|
||||
return this.game.particles.add(new Phaser.Particles.Arcade.Emitter(this.game, x, y, maxParticles));
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
@@ -23,16 +23,13 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
// The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.
|
||||
this.lifespan = 0;
|
||||
|
||||
if (key)
|
||||
if (key == null || this.game.cache.checkImageKey(key) == false)
|
||||
{
|
||||
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No texture yet
|
||||
PIXI.Sprite.call(this);
|
||||
key = '__default';
|
||||
}
|
||||
|
||||
PIXI.Sprite.call(this, PIXI.TextureCache[key]);
|
||||
|
||||
this.events = new Phaser.Events(this);
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,15 @@
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
*/
|
||||
Phaser.Cache = function (game) {
|
||||
|
||||
this.game = game;
|
||||
this._canvases = {};
|
||||
this._images = {};
|
||||
this._sounds = {};
|
||||
this._text = {};
|
||||
|
||||
this.addDefaultImage();
|
||||
|
||||
};
|
||||
|
||||
Phaser.Cache.prototype = {
|
||||
@@ -106,6 +114,25 @@ Phaser.Cache.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a default image to be used when a key is wrong / missing.
|
||||
* Is mapped to the key __default
|
||||
*/
|
||||
addDefaultImage: function () {
|
||||
|
||||
this._images['__default'] = { url: null, data: null, spriteSheet: false };
|
||||
this._images['__default'].frame = new Phaser.Animation.Frame(0, 0, 32, 32, '', '');
|
||||
|
||||
var base = new PIXI.BaseTexture();
|
||||
base.width = 32;
|
||||
base.height = 32;
|
||||
base.hasLoaded = true; // avoids a hanging event listener
|
||||
|
||||
PIXI.BaseTextureCache['__default'] = base;
|
||||
PIXI.TextureCache['__default'] = new PIXI.Texture(base);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new image.
|
||||
* @param key {string} Asset key for the image.
|
||||
@@ -219,6 +246,22 @@ Phaser.Cache.prototype = {
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks if an image key exists.
|
||||
* @param key Asset key of the image you want.
|
||||
* @return {boolean} True if the key exists, otherwise false.
|
||||
*/
|
||||
checkImageKey: function (key) {
|
||||
|
||||
if (this._images[key])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get image data by key.
|
||||
* @param key Asset key of the image you want.
|
||||
|
||||
@@ -1 +1,39 @@
|
||||
Phaser.Particles = {}
|
||||
Phaser.Particles = function (game) {
|
||||
|
||||
this.emitters = {};
|
||||
|
||||
this.ID = 0;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Particles.prototype = {
|
||||
|
||||
emitters: null,
|
||||
|
||||
add: function (emitter) {
|
||||
|
||||
this.emitters[emitter.name] = emitter;
|
||||
|
||||
return emitter;
|
||||
|
||||
},
|
||||
|
||||
remove: function (emitter) {
|
||||
|
||||
delete this.emitters[emitter.name];
|
||||
|
||||
},
|
||||
|
||||
update: function () {
|
||||
|
||||
for (var key in this.emitters)
|
||||
{
|
||||
if (this.emitters[key].exists)
|
||||
{
|
||||
this.emitters[key].update();
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
@@ -12,6 +12,8 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
|
||||
|
||||
Phaser.Group.call(this, game);
|
||||
|
||||
this.name = 'emitter' + this.game.particles.ID++;
|
||||
|
||||
/**
|
||||
* The X position of the top left corner of the emitter in world space.
|
||||
*/
|
||||
@@ -119,12 +121,16 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
|
||||
*/
|
||||
this.on = false;
|
||||
|
||||
/**
|
||||
* Determines whether the emitter is being updated by the core game loop.
|
||||
*/
|
||||
this.exists = true;
|
||||
this.active = true;
|
||||
// Needs to be a setter/getter and toggle the _container property
|
||||
this.visible = true;
|
||||
|
||||
// The point the particles are emitted from (Emitter.x/y control the container location, i.e. all particles, this controls position within it)
|
||||
/**
|
||||
* The point the particles are emitted from.
|
||||
* Emitter.x and Emitter.y control the containers location, which updates all current particles
|
||||
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
|
||||
*/
|
||||
this.emitX = 0;
|
||||
this.emitY = 0;
|
||||
|
||||
@@ -186,42 +192,42 @@ Phaser.Particles.Arcade.Emitter.prototype.update = function () {
|
||||
*
|
||||
* @return This Emitter instance (nice for chaining stuff together, if you're into that).
|
||||
*/
|
||||
Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (key, quantity, multiple, collide) {
|
||||
Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames, quantity, collide) {
|
||||
|
||||
if (typeof frames == 'undefined')
|
||||
{
|
||||
frames = 0;
|
||||
}
|
||||
|
||||
quantity = quantity || this.maxParticles;
|
||||
multiple = multiple || false;
|
||||
collide = collide || 0;
|
||||
|
||||
var totalFrames = 1;
|
||||
var randomFrame;
|
||||
var particle;
|
||||
var i = 0;
|
||||
var rndKey = keys;
|
||||
var rndFrame = 0;
|
||||
|
||||
while (i < quantity)
|
||||
{
|
||||
if (this.particleClass == null)
|
||||
{
|
||||
particle = new Phaser.Sprite(this.game, 0, 0, key);
|
||||
if (typeof keys == 'object')
|
||||
{
|
||||
rndKey = this.game.rnd.pick(keys);
|
||||
}
|
||||
|
||||
if (typeof frames == 'object')
|
||||
{
|
||||
rndFrame = this.game.rnd.pick(frames);
|
||||
}
|
||||
|
||||
particle = new Phaser.Sprite(this.game, 0, 0, rndKey, rndFrame);
|
||||
}
|
||||
else
|
||||
{
|
||||
// particle = new this.particleClass(this.game);
|
||||
}
|
||||
|
||||
if (multiple)
|
||||
{
|
||||
/*
|
||||
randomFrame = this.game.math.random()*totalFrames;
|
||||
*/
|
||||
}
|
||||
else
|
||||
{
|
||||
// if (graphics)
|
||||
// {
|
||||
// particle.texture.loadImage(graphics);
|
||||
// }
|
||||
}
|
||||
|
||||
if (collide > 0)
|
||||
{
|
||||
particle.body.allowCollision.any = true;
|
||||
@@ -433,11 +439,47 @@ Phaser.Particles.Arcade.Emitter.prototype.setRotation = function (min, max) {
|
||||
*/
|
||||
Phaser.Particles.Arcade.Emitter.prototype.at = function (object) {
|
||||
|
||||
//this.x = object.body.bounds.halfWidth - (this.width >> 1);
|
||||
//this.y = object.body.bounds.halfHeight - (this.height >> 1);
|
||||
this.emitX = object.center.x;
|
||||
this.emitY = object.center.y;
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "alpha", {
|
||||
|
||||
/**
|
||||
* Get the emitter alpha.
|
||||
*/
|
||||
get: function () {
|
||||
return this._container.alpha;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the emiter alpha value.
|
||||
*/
|
||||
set: function (value) {
|
||||
this._container.alpha = value;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "visible", {
|
||||
|
||||
/**
|
||||
* Get the emitter visible state.
|
||||
*/
|
||||
get: function () {
|
||||
return this._container.visible;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the emitter visible state.
|
||||
*/
|
||||
set: function (value) {
|
||||
this._container.visible = value;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "left", {
|
||||
|
||||
get: function () {
|
||||
|
||||
Reference in New Issue
Block a user