Jump to content

Pass a property name as an argument to create group?


KonceptZer0
 Share

Recommended Posts

So I'm going about reducing the number of lines of code in a game I'm creating and I have several functions that are basically doing the same thing: adding a group to my game and proceeding to do a bunch of the same stuff to that group. So I figured I could just unify those instructions into one function and call it a bunch of times for the different groups, passing the name of the group as an argument like so:

doGroupStuff: function(groupName){

this.groupName = this.add.group();

//do other stuff to this group

},

doGroupStuff(firstGroup);
doGroupStuff(secondGroup);

(etc.)

Except that isn't quite working and I'm probably just being ignorant to something very basic I should know.
Can anyone help me?

Link to comment
Share on other sites

So, I've only had the chance to test this out today, (working on a ton of different projects in parallel for college sucks), and it works, but now I'm running into a different error: 

My function is taking a bunch of arguments to augment the group, creating multiple sprites, enabling physics, etc.

createSpriteGroup: function(groupName, physics, number, key, reward, dropRate, animated, delay) {
    this[groupName] = this.add.group();
    this[groupName].createMultiple(number, key);
    if(physics){
      this[groupName].enableBody=true;
      this[groupName].physicsBodyType=Phaser.Physics.ARCADE;
      this[groupName].setAll('outOfBoundsKill', true);
      this[groupName].setAll('checkWorldBounds', true);
    }
    this[groupName].setAll('anchor.x', 0.5);
    this[groupName].setAll('anchor.y', 0.5);
    this[groupName].setAll('reward', reward, false, false, 0, true);
    this[groupName].setAll('dropRate', dropRate, false, false, 0, true);
    if(animated){
      this[groupName].forEach(function(unit){
        unit.animations.add('fly', [0,1,2,0], 20, true);
        unit.animations.add('hit', [3,1,3,2,3], 20, false);
        unit.events.onAnimationComplete.add(function(u){
          u.play('fly');
        }, this);
      });
    }
    if(delay>0){
      this[groupName].resTime = delay;
    }
  },

I'm calling it like this: 

this.createSpriteGroup('enemyPool', true, 200, 'greenEnemy', BasicGame.ENEMY_REWARD, BasicGame.ENEMY_DROP_RATE, true, BasicGame.SPAWN_ENEMY_DELAY);

this.nextEnemyAt = 0; //also calling this, 

But when the game tries to access the sprites through:

spawnEnemies: function() {
    if (this.nextEnemyAt < this.time.now && this.enemyPool.countDead() > 0) {
      this.nextEnemyAt = this.time.now + this.enemyPool.resTime;
      var enemy = this.enemyPool.getFirstExists(false);
      enemy.reset(this.rnd.integerInRange(20, this.game.width - 20), 0, BasicGame.ENEMY_HEALTH);           //this line here
      enemy.body.velocity.y = this.rnd.integerInRange(BasicGame.ENEMY_MIN_Y_VELOCITY, BasicGame.ENEMY_MAX_Y_VELOCITY);
      enemy.play('fly');
    }

It's returning "Uncaught TypeError: Cannot read property 'velocity' of null" in the commented line.

Sorry for the ton of pasted code, 
Any insights?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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