Jump to content

No TileSprite.kill() ? What instead?


Recommended Posts

That seems to me enough to make the sprite inactive yeah. Here's what Phaser does when it runs .kill() on a sprite:

Phaser.Sprite.prototype.kill = function() {    this.alive = false;    this.exists = false;    this.visible = false;    if (this.events)    {        this.events.onKilled.dispatch(this);    }    return this;};

You could also easily add your own kill method to all TileSprites by just adding this somewhere before your game code:

Phaser.TileSprite.prototype.kill = function() {    this.stopScroll();    this.alive = false;    this.exists = false;    this.visible = false;    if (this.events)    {        this.events.onKilled.dispatch(this);    }    return this;};
Link to comment
Share on other sites

Thanks, I got more from this answer than I hoped.


You seem to be very knowledgeable, and I have some problems adapting to JavaScript's way of OO programming. Could you recommend any good materials to help me understand how prototypes work? I "know" how to use them in a sort of "class method definition" way, but I'm pretty sure I don't actually understand what's happening there.

Link to comment
Share on other sites

I'd highly recommend JavaScript: The Good Parts by Doug Crockford: http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742


All that's happening in the example above is that we're adding a new method to an existing object, and because we're adding it to the object's prototype, it gets added to any instances of that object too.


If you think of Phaser.TileSprite as the class (it isn't a class, but let's pretend it is) adding a method to Phaser.TileSprite directly would create a 'static' method - a method only accessible from the original 'class' object. If however you add a method (or indeed any property) to its prototype, all instances will then have access to that as if they always had them, and the method/property in each case would belong to the instance (so 'this' within a method would refer to the instance).


There're lots of other materials out there to read on JavaScript - it's definitely different from the OO you're used to, because in JavaScript everything is based on actual objects, not on classes which act as blueprints for new objects. It's flexible enough however to apply many of the standard practices you're used to, and even go so far as to start calling things 'classes' and 'static methods' and so on.

Link to comment
Share on other sites

Thanks, my library will probably have this book, I'll check it out.


Some things you said about prototypes and 'static' properties caught my attention, so let's take a look at, for example, ScaleManager.js (AFAIK it doesn't extend anything, which should make it a bit easier).


It starts with:

Phaser.ScaleManager = function (game, width, height) {    this.game = game;//a lot more code    var _this = this;//some more code};Phaser.ScaleManager.EXACT_FIT = 0;

My questions about this snippet (especially no. 1 is quite confusing to me):

1) Why does this.game work, and is non-static? It's not added to the prototype, unless the this. makes some kind of a shortcut.

2) I suspect that Phaser.ScaleManager.EXACT_FIT = 0; is 'static', is that correct?

3) I think that var _this is local and available only from the function, is that true?

4) Would I be able to do Phaser.ScaleManager.prototype.myVar = 3; (outside of function definition) and if yes, what'd be the difference from having this.myVar = 3; (in the function definition)?

5) Is Phaser.ScaleManager effectively a constructor?

Link to comment
Share on other sites

1) this.game used here is an instance reference - the function here as in your 5th question is a constructor that takes three arguments. When you instantiate a new object with new Phaser.ScaleManager(gameReference, 320, 240) the gameReference you pass becomes accessible within the instances, like so:

var scale = new Phaser.ScaleManager(gameReference, 320, 240);console.log(scale.game === gameReference);> true

2) Yes, it is, and because it's in all caps, it's also inferred to be a constant. JavaScript doesn't properly support constants so the coding convention in Phaser and many other projects is to use all uppercase with underscores to denote consts


3) Yes, and any functions inside this function - this is a special way to keep a reference to the outside scope when doing things with anonymous functions, as in this example:

// incorrectvar Foo = function() {  this.bar = "hello";  setTimeout(function() {    console.log(this.bar); // error, as 'this' now refers to the function inside setTimeout, not Foo  }, 1000);}// correctvar Baz = function() {  var self = this; // store a reference to the Baz instance which can be accessed from any functions inside this function - people also use self, that, _this and so on  this.bar = "hello";  setTimeout(function() {    console.log(self.bar); // works as intended   }, 1000);}

4) Yes - the difference is that your in-function definition will supersede anything on the prototype. JavaScript will look first to see if the instance itself has the property, and if not go up the prototype chain until it finds the property. This means you can add a property to all existing instances but if any already have this property, it will not be overwritten.


5) Yes - the function itself is run when instantiated because new creates the instance and then any function reference that have parentheses after them will cause that function to be run with the parameters (if any) provided.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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