admin管理员组

文章数量:1022982

In my Phaser game, I want to use a cell-like background texture which moves when you move.

My idea was to add each cell in a for loop like this:

  let width=100, height=100;
  for(let y=0;y<width;y++){
    ;
    for(let x=0; x<height;x++){
    
      this.physics.add.image(width/2+64*x, height/2+64*y, "snow_field").setScale(4,4);
      
    }
  }

The problem: The game gets very (very) slow

So is there a better / remended way to add repeating images/backgrounds?

In my Phaser game, I want to use a cell-like background texture which moves when you move.

My idea was to add each cell in a for loop like this:

  let width=100, height=100;
  for(let y=0;y<width;y++){
    ;
    for(let x=0; x<height;x++){
    
      this.physics.add.image(width/2+64*x, height/2+64*y, "snow_field").setScale(4,4);
      
    }
  }

The problem: The game gets very (very) slow

So is there a better / remended way to add repeating images/backgrounds?

Share Improve this question asked Dec 14, 2021 at 19:55 MoPaMoMoPaMo 6848 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You are looking for the Phaser.GameObjects.TileSprite (link to the documentation)

...
A TileSprite is a Sprite that has a repeating texture.
...

this.add.tileSprite(posX, posY, width, height, "snow_field");

here is a small official example, where you can see it in action.

In my Phaser game, I want to use a cell-like background texture which moves when you move.

My idea was to add each cell in a for loop like this:

  let width=100, height=100;
  for(let y=0;y<width;y++){
    ;
    for(let x=0; x<height;x++){
    
      this.physics.add.image(width/2+64*x, height/2+64*y, "snow_field").setScale(4,4);
      
    }
  }

The problem: The game gets very (very) slow

So is there a better / remended way to add repeating images/backgrounds?

In my Phaser game, I want to use a cell-like background texture which moves when you move.

My idea was to add each cell in a for loop like this:

  let width=100, height=100;
  for(let y=0;y<width;y++){
    ;
    for(let x=0; x<height;x++){
    
      this.physics.add.image(width/2+64*x, height/2+64*y, "snow_field").setScale(4,4);
      
    }
  }

The problem: The game gets very (very) slow

So is there a better / remended way to add repeating images/backgrounds?

Share Improve this question asked Dec 14, 2021 at 19:55 MoPaMoMoPaMo 6848 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You are looking for the Phaser.GameObjects.TileSprite (link to the documentation)

...
A TileSprite is a Sprite that has a repeating texture.
...

this.add.tileSprite(posX, posY, width, height, "snow_field");

here is a small official example, where you can see it in action.

本文标签: javascriptHow do you add repeating images in PhaserStack Overflow