Jump to content

Adding properties to sprite class


moe091
 Share

Recommended Posts

I'm still pretty new to javascript so there are a lot of thigns i don't understand. 

 

One thing that would be useful for me in my phaser games is if I could add a reference to my gameobject inside the gameObjects sprite object. I use gameobjects to represent things like enemies and the enemy object has a reference to its sprite, maybe I need to stop trying to force java style OOP into javascript so much, but I already have a large game coded this way so I'm not about to go back right now.

 

Anyway I tried just adding this.sprite.owner = this into my gameobject constructor but it's giving me undefined when I try to access sprite.owner in other parts of the code. Is it possible to get the behavior I'm going for? Am I doing it wrong? Are there any other solutions to my problem?

 

the reason I want to do this is because there are a lot of callbacks(collision for instance) that pass the sprite object to a function, but I need to operate on the sprites GameObject in the collision reaction function, so isntead of looping through all the gameobjects and checking if their sprite matches the gameobjects sprite to figure out which gameobject it belongs too, I could just use sprite.owner.

 

 

 

Another side question. In my GameObjects prototype object I have a die() function that makes an explosion animation and adds points to the players score and everything. I used to call it when the objects health went <= 0 but I recently learned that phaser has health and alive variables along with convenient accompanying functions like revive(x, y, health) that I could use to reuse my sprites more easily, so I'm switching over to using the built in phaser methods/properties. The problem is setting the onKilled event callback of the sprite to call the die() method. 

 

In the 'constructor' function I have this.sprite.events.onKilled.add(//stuff goes here); and I the //stuff goes here part to call the die() method, which exists inside the GameObjects prototype(same gameobject who's constructor that line of code is in). I can't figure how to do it at all, I tried a bunch of different things but I just don't have the understanding of javascript(mostly scope it seems) to figure out the right way to do it. I can't just put this.die(); or die() or Enemy.die() or Enemy.prototype.die() or this.owner.die() in there because they all give me various errors, it seems like I'm way off.

 

Anyway I hope someone can help me with either one of these problems, thanks a lot everyone

 

 

EDIT: It would help me a lot if soemone could just explain what the 'this' refers to in code that looks like this:   

function Enemy(sprite) { this.sprite = sprite; this.sprite.events.onKilled.add(function() {  this.//WHAT IS THIS REFERRING TO HERE? });}Enemy.prototype = {//...
Link to comment
Share on other sites

That should all work fine.

 

For the event, your 'this' problem could just be that you're leaving out the 2nd argument to the event's add method:

this.sprite.events.onKilled.add(function() { //stuff }, this);

Unless you just left it out from your example code above. The 2nd arg, this, is a reference to the current context (in your case, an Enemy instance) so that the event manager can call the callback with the correct 'this' binding (otherwise the 'this' keyword inside your callback would likely end up referring to the event manager, the window, who knows...).

 

Not sure why you're getting undefined for sprite.owner. If you set it in the constructor like you've show above, like:

function Enemy(sprite) { this.sprite = sprite; this.sprite.owner = this; this.sprite.events.onKilled.add(function() {  this.//WHAT IS THIS REFERRING TO HERE? }, this);}

then I think it should work.

Link to comment
Share on other sites

yah the sprite.owner thing works fine now, I don't know what was happening before.

 

And thanks a ton guys I understand what was going on with the onKilled thing now, and I learned a bit more about javascript :).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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