Jump to content

replacing animations for recycled sprites


zazazayou
 Share

Recommended Posts

so i have a game where there are different enemies who look different and so have different spritesheets.

and i have a pool of objects mechanism to recycle the dead enemy objects into new ones, kinda like this:

var enemy = this.state.enemies.getFirstDead();

  if (!enemy){
    enemy = new MyGame.Enemy(this.state, cords.x, cords.y, data);
    this.state.enemies.add(enemy);
  } else {
    enemy.reset(cords.x, cords.y, data);  
  }

 

so the problem is like this: sprites are not properly having their animations changed from their old selves to their new selves. their sprites change properly for the most part but are keeping bits of info from the old animations, ie they are not being fully replaced when i call the this.animations.add again.

 

the data parameter is an object with a buncha info about each enemy, including animation info. kinda like this:

{
      key: 'troll',
      scale: 0.8,
      health: 20,
      movementSpeed: 60,
      attack: {amount: 2, delay: 0.5, type: 'physical', frames: [0, 1, 2, 3, 4, 5, 6, 7], fps: 16},
      death: {frames: Phaser.ArrayUtils.numberArray(0, 31), fps: 32}
    }

 

so in my modified reset method, which runs actually every time an enemy is created, these animations are made, pulling the frames and stuff from data and making an animation, like this (in the reset method of Enemy):

this.animations.add('attack', this.data.attack.frames, this.data.attack.fps || 12, false);

and when i run that line with different data on a sprite that was recycled, the data is not fully over-written, resulting in these mutating enemies that change from trolls to scorpions as their various animations play.

 

the first thing i tried was calling this.animations.destroy() on my modified .kill() method that is called on all dying sprites, i thought that shoulda fixed it...

but then i immediately get this error, 'Cannot read property 'getFrameIndexes' of null(…)', as if the animations were not being properly cleared and then added freshly again.

 

i hope this made sense, thanks for the help guys.

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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