Home > Enterprise >  How to group existing multiple canvas textures?
How to group existing multiple canvas textures?

Time:05-09

I have created 3 canvas texture in file a.ts:

    this.textures1 = this.textures.createCanvas('canvastextures1', 450, 170)
    this.textures2 = this.textures.createCanvas('canvastextures2', 410, 180)
    this.textures3 = this.textures.createCanvas('canvastextures3', 400, 210)

and in file b.ts I want to create a group multiple canvas texture to use function overlap(groupCanvas, object).

I can call 3 canvas texture in file b.ts by using: this.textures.get('canvastextures1')

Does anyone have any ideas? Thank you.

CodePudding user response:

Well this.textures.createCanvas just creates a texture, so you can simply create gameobjects and add them to a group (or physics group), than you cancheck for overlap, between a gameobject and the texture group.

Updated full example:

// Minor formating for stackoverflow
document.body.style = "display: flex;flex-direction: column;";    


class FirstScene extends Phaser.Scene {
  constructor() {
    super('firstScene')
  }
  
  create(){
    console.log('Create Call in First Scene!')
    this.add.rectangle(0,0, 600,20, 0xff0000).setOrigin(0)
    this.scene.start('secondScene')
  }
  
  createTextures(){
      let helperGraphics = this.make.graphics({x:0, y: 0, add: false});
      helperGraphics.fillStyle(0xff0000);
      helperGraphics.fillRect(0, 0, 20, 20 );
      helperGraphics.fillStyle(0x00ff00);
      helperGraphics.fillRect(20, 0, 20, 20 );
      helperGraphics.fillStyle(0xffffff);
      helperGraphics.fillRect(40, 0, 20, 20 );
      helperGraphics.generateTexture('helper', 60, 20);
      let img = this.textures.get('helper').getSourceImage()
    
      this.textures1 = this.textures.createCanvas('canvastextures1', 20, 20)
      this.textures1.draw(-40, 0, img)
      
      this.textures2 = this.textures.createCanvas('canvastextures2', 20, 20)
      this.textures2.draw(-20, 0, img)
      
      this.textures3 = this.textures.createCanvas('canvastextures3',20, 20)
      this.textures3.draw(0, 0, img)
      
      this.textures4 = this.textures.createCanvas('canvastextures4',40, 40)
      this.textures4.draw(20, 10, this.textures.get('canvastextures2').getSourceImage())
      this.textures4.draw(10, 0, this.textures.get('canvastextures1').getSourceImage())
      
      this.textures4.draw(0, 10, this.textures.get('canvastextures3').getSourceImage())   
  }
}

class SecondScene extends Phaser.Scene {
  constructor() {
    super('secondScene')
  }
  
  create(){
    this.textureGroup = this.physics.add.group({
        immovable: true,
        allowGravity: false
    });           
    
    let loadScene = this.scene.get('firstScene')
    
    loadScene.createTextures();
    
    this.textureGroup = this.physics.add.group({
        immovable: true,
        allowGravity: false
    });  
    
    this.textureGroup.add(
        this.add.image(100, 25, 'canvastextures1').setOrigin(0)
    )
    
    this.textureGroup.add(
        this.add.image(100, 75, 'canvastextures2').setOrigin(0)
    )
    
    this.textureGroup.add(
        this.add.image(100, 125, 'canvastextures3').setOrigin(0)
    )
    
    this.ball = this.add.rectangle(100, 0, 10, 10, 0xffffff)  
    this.physics.add.existing(this.ball)
    
    this.add.text(200, 30, 'Alternative:\nmerged all textures,\nin a new texture', { fontSize:10, color: '#ffffff', align: 'center' }).setOrigin(.5, 0)
    this.add.image(200, 75, 'canvastextures4').setOrigin(.5, 0)
            
    this.physics.add.overlap(this.ball, this.textureGroup, overlap )
  }
}

function overlap(player, groupItem){
    groupItem.setTint(0xcdcdcd)
    setTimeout(_ => groupItem.clearTint() , 400)
}

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    physics:{
        default: 'arcade',
        arcade:{ gravity:{y: 100}}
    },
    scene: [FirstScene, SecondScene],
    banner: false
}; 

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js">
</script>

Extra: If you want to create a new texture from three seperate textures, checkout textures4.

  • Related