Jump to content

Getting public property or method from an extended class


ManBoy
 Share

Recommended Posts

I have a Bullet that extends from Sprite

Bullet = function (game,x,y,colorID){	Phaser.Sprite.call(this,game,x,y,'Atlas','Bullet_'+colorID);	this.colorID = colorID;	Bullet.prototype.createExplosion = function()	{		this.animations.add('bullet_explosion',Phaser.Animation.generateFrameNames('Bullet_Explosion_', 1, 18, '_'+colorID+'.png', 4),18,true);                this.animation.play('bullet_explosion');	}};Bullet.prototype = Object.create(Phaser.Sprite.prototype);Bullet.prototype.constructor = Bullet;
function bulletHit(bullet,other){	console.log(bullet); //print out Sprite, not Bullet}

As the bullet collides, I need to at least get either the colorID or call the createExplosion method inside the Bullet class to get the correct color/animation of the explosion. How can I achieve that?

 

I'm sorry for the newbie question.

Link to comment
Share on other sites

I wrote the following code in the update function:

game.physics.collide(bullets, enemies, bulletHitEnemies, null, this);

 

This is the callback:

function bulletHitEnemies(bullet,enemy){	//bullet.animations.add('bullet_explosion',Phaser.Animation.generateFrameNames('Bullet_Explosion_', 1, 18, '_'+bullet.colorID+'.png', 4),18,true);        //bullet.animation.play('bullet_explosion');}

I still don't know how can I get the colorID from the bullet, it says the colorID is 'undefined' because bullet is treated as Sprite, not Bullet.

Sorry for being noob.

Link to comment
Share on other sites

You don't need to add the bullet animation in that function, just in the initial creation of the bullet. You only need to play the animation and kill the bullet and do the damage in that function. As for the colorID I have no clue. Ordinarily you'd use a spritesheet and animated the frames from the spritesheet frames.

Link to comment
Share on other sites

This is something I grappled with myself. Since I don't extend Phaser.Sprite and instead prefer to wrap the sprite in a container object to handle the data model, it makes using the builtin collision handlers a little tricky when you need access to the properties of the wrapper class. But since I really do like the pattern I'm presently using, I came up with a way to deal with this — via a custom event emitter.

Here's an example entity class I would use to wrap around the sprite...

function Player ( spriteData ) {  this.sprite = {};  this.someProp = 'foo';  this.createSprite(spriteData);​}Player.prototype.create = function ( spriteData ) {  this.sprite = game.add.sprite( spriteData.name ...};

 

I opt to use my own event emitter to handle the delegation of events across entities. So in my primary game loop, I have the top-level create() and update() functions which call my event emitter...
 

...function create() {  this.vent = new Shout();  this.vent.emit('create');}function update() {  this.vent.emit('update');}...

So now in my Player class I can listen to these events (the `vent` reference gets passed around via a gameState object shared via a top-level namespace) and subsequently check collisions while you have a direct reference to the object wrapping the sprite.

Player.prototype.bindEvents = function(){  this.vent.on('update', this.checkCollisions.bind(this));}Player.prototype.checkCollisions = function(){  // Assuming you have a reference to `game` here.  // I usually pass that reference in the constructor, or another  //   similar shared gameState object.  game.physics.collide(this, bullets, this.onCollide, null, this);}Player.prototype.onCollide = function( player, bullet ){  // Now you have a reference to the top-level wrapper class via `this`,  // as well as to the `player` and `bullet` sprites themselves   if ( this.someProp == 'foo' ) {    player.kill();  }}

In your case, you would follow this pattern for your bullet class (since bullet is the object from which you want to grab a property).

 

Does this make sense? 

 

As an aside, the event emitter I use in my own projects is available on my github @ https://github.com/spmurrayzzz/Shout.js

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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