[SOLVED] Get sprite's texture

Is there a way to get a sprite’s texture/name of the spritesheet it is using?

I’m asking because I’m trying to make a function where an item falls and its position gets reset to the top of the screen once it goes out of the world bounds. The problem is that I have 3 different types of items and I want to make a general function to reset an item regardless of what kind of item it is.

Is this possible? Is this a good way to go about this? I have considered making a separate function for each type of object, but it seems like a waste, since the functions would be very similar…

Here’s my code:

//falling items
  itemFall(item, accel) {
    //set acceleration
    item.body.setAcceleration(0,accel);
    //reset item when it falls beyond the world boundary (top/bottom)
    if (item.y > config.height) {
      this.itemReset(item);
    } else if (item.y < 0){
      this.itemReset(item);
    }
    //reset item when it falls beyond the world boundary (left/right)
    if (item.x > config.width) {
      this.itemReset(item);
    } else if (item.x < 0){
      this.itemReset(item);
    }
  }
  itemReset(item) {
    item.y = 0;
    var randomX = Phaser.Math.Between(0, config.width);
    item.x = randomX;
    //this is where I want to detect the texture and act accordingly
    //this particular instance only deals with one type of item - tetrominos
    item.setTexture("tetromino", Phaser.Math.Between(0, 59));
  }

I’d appreciate any advice in this situation

There certainly is. Use mySprite.texture.key for the texture and mySprite.frame.name for the frame if you are using an atlas.

2 Likes

Thanks! After a bit of trial and error, I ended up using this:

itemReset(item) {
    item.y = 0;
    var randomX = Phaser.Math.Between(0, config.width);
    item.x = randomX;
    console.log(item.texture.key);
    if (item.texture.key == "tetromino") {
      item.setTexture("tetromino", Phaser.Math.Between(0, 59));
    }
    else if (item.texture.key == "junk") {
      item.setTexture("junk", Phaser.Math.Between(0, 5));
    }
    else if (item.texture.key == "1up") {
      item.setTexture("1up", 0);
    }