Jump to content

Question about documentation


Nebulocity
 Share

Recommended Posts

I'm looking at the Sprite class, specifically the Lifespan property.  I thought to add it to an enemy with 1000 set as the lifespan (which I understand to be in ms), but it didn't die after the time was up.  Could someone please explain how one generally goes about unraveling the mysteries locked in the codex of the documentation?  ;-) 

 

The URL to the property is: http://docs.phaser.io/Phaser.Sprite.html#toc33
The URL to the definition in the Sprite.js file is: http://docs.phaser.io/Sprite.js.html#sunlight-1-line-76

I don't understand how to implement this, and it's a problem that I find myself having every time I go looking through a class: I find a property or method that I want to play around with (to learn), and it ends up not working.  But looking further into it (into the actual definition itself), it either says nothing about it, or is too archaic (to me) to understand. 

 

For example, the source for Lifespan (as noted in the docs) starts on line 76.  Here is what that section (and subsequent lines) states.  Nothing about it gives any indication of how to use it.  The only thing that I can understand is that it's dealing with displaying a frame...which it also says in the comments ;-)

 

    /**    *  @property {Phaser.Frame} currentFrame - A reference to the currently displayed frame.    */    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 Phaser.BitmapData)    {        PIXI.Sprite.call(this, key.texture, key.textureFrame);        this.currentFrame = key.textureFrame;    }    else if (key instanceof PIXI.Texture)    {        PIXI.Sprite.call(this, key);        this.currentFrame = frame;    }    else    {        if (key === null || typeof key === 'undefined')        {            key = '__default';            this.key = key;        }        else if (typeof key === 'string' && this.game.cache.checkImageKey(key) === false)        {            key = '__missing';            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);        }    }
Link to comment
Share on other sites

What are you trying to do, and how is it not working?
I have successfully used lifespan on sprites before simply by setting it explicitly:

var spr = game.add.sprite(0,0,"image")spr.lifespan = 2000;

As for documentation, numbers lie. In regards to lifespan, it is declared on line 76, yes, but in the online source code, the line numbers do not line up to the lines, thus it *looks* like lifespan is actually on line 57 and a half.

What you pasted *looks* like it is line 76, but it isn't.

Easiest way to find things is Ctrl+F it.

 

If you cloned the repository, then you should have all the source files on your machine, and can look through there instead, and have more reliable line numbers...

 

NOTE: This line number inconsistency happens in Chrome and Firefox for me, so I assume it'll happen for everyone... If not, lucky you..!

Link to comment
Share on other sites

Yup, this line number inconsistency happens for me, too, in Chrome.  Here's the actual lifespan declaration in the source, if anyone's interested:

   /**    * If you would like the Sprite to have a lifespan once 'born' you can set this to a positive value. Handy for particles, bullets, etc.    * The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.    * @property {number} lifespan - The lifespan of the Sprite (in ms) before it will be killed.    * @default    */    this.lifespan = 0;

And here's where it's used to kill the sprite:

/*** Automatically called by World.preUpdate. Handles cache updates, lifespan checks, animation updates and physics updates.** @method Phaser.Sprite#preUpdate* @memberof Phaser.Sprite*/Phaser.Sprite.prototype.preUpdate = function() {    if (!this.exists || (this.group && !this.group.exists))    {        this.renderOrderID = -1;                // Skip children if not exists        return false;    }    if (this.lifespan > 0)    {        this.lifespan -= this.game.time.elapsed;        if (this.lifespan <= 0)        {            this.kill();            return false;        }    }...};
Link to comment
Share on other sites

Oh, thank you both for that bit of good news.  I thought I was going insane last night when I couldn't get it to work.  I used the very same implementation code and it did nothing for me until I ran it in Chrome and not Firefox, so perhaps my Firefox was out of date?  Odd.  It's working now, as long as I use chrome (which also complains about some CSS that I'm not actually using, but oh well).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...