移动器3:Update()帆布纹理不起作用

发布于 2025-01-26 06:37:30 字数 890 浏览 2 评论 0原文

我正在尝试将此帖子转换为3: https://phaster.io/tutorials/codorials/coding -tips-002 但是update()函数不起作用。 在文件a.ts中,我创建了一个帆布纹理:

this.textures1 = this.textures.createCanvas('canvastextures1', 450, 170)
this.land1 = this.textures.get(MAPOPTIONS.BASE1_NAME).getSourceImage()
this.textures1.draw(0, 0, this.land1)
this.textures1.context.globalCompositeOperation = 'destination-out'

在文件b.ts中,在重叠()函数中:

  this.activeTextures = this.textures.get('canvastextures1')
  this.activeTextures.context.beginPath()
  this.activeTextures.context.arc(Math.floor(overlap2.x-tile.getTopLeft().x), Math.floor(overlap2.y-tile.getTopLeft().y), 50, 0, Math.PI * 2, false)
  this.activeTextures.context.fill()
  this.activeTextures.update()

有人有任何想法吗?谢谢。

I am trying to convert this post to phaser 3: https://phaser.io/tutorials/coding-tips-002 but the update() function not working.I have also tried with refresh() function but it not working too.
In file a.ts I create a canvas texture:

this.textures1 = this.textures.createCanvas('canvastextures1', 450, 170)
this.land1 = this.textures.get(MAPOPTIONS.BASE1_NAME).getSourceImage()
this.textures1.draw(0, 0, this.land1)
this.textures1.context.globalCompositeOperation = 'destination-out'

and in file b.ts, in overlap() function:

  this.activeTextures = this.textures.get('canvastextures1')
  this.activeTextures.context.beginPath()
  this.activeTextures.context.arc(Math.floor(overlap2.x-tile.getTopLeft().x), Math.floor(overlap2.y-tile.getTopLeft().y), 50, 0, Math.PI * 2, false)
  this.activeTextures.context.fill()
  this.activeTextures.update()

Does anyone have any ideas? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2025-02-02 06:37:30

这是 demo代码,它显示了我将如何接近整个createCanvas,地形/土地破坏碰撞机械师。
除文件分配外,它应该涵盖所有要点。我希望这会有所帮助。

炸弹&火山口演示:

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

class Scene extends Phaser.Scene {

        constructor() {
            super({ key: 'testScene' });
        }

        create() {

            // Start  -- GENERATE TEXTURE just for demo
            let helperGraphics = this.make.graphics({x: -250, y: -105, add: false});
            helperGraphics.fillStyle(0xEAEAEA, 1);
            helperGraphics.fillRect(0, 20, 500, 100 );
            helperGraphics.generateTexture('land', 500, 210);
            // End  -- GENERATE TEXTURE just for demo

            this.baseLand = this.textures.get('land').getSourceImage()
            this.baseCanvas = this.textures.createCanvas('currentLand', 800, 200);
            // Create initial Land Texture
            this.baseCanvas.draw(0, 0, this.baseLand);
            this.baseCanvas.context.globalCompositeOperation = 'destination-out';
            // load image to scene
            this.land = this.add.image(0, 20, 'currentLand').setOrigin(0);
            this.physics.add.existing(this.land);
            this.land.body.setImmovable(true);
            this.land.body.setAllowGravity(false);

            // init bomb
            this.createBomb();

        }
        // creates always new bomb
        createBomb(bomb, land) {
            this.bomb = this.add.circle(Phaser.Math.Between(100, 300), -5, 5, 0x6666ff);
            this.physics.add.existing(this.bomb);

            this.physics.add.overlap(
                this.bomb,
                this.land,
                this.overlap,
                this.shouldProcess, this);
        }
        // action on overlap
        overlap(bomb, land) {
            // remove bomb on contact
            let {x, y} = bomb.body.center;
            bomb.destroy();
            // create crater on old bomb position
            this.createCrater({x, y});
            // create new bomb 
            this.createBomb();
        }
        // checks if bomb hits land
        shouldProcess(bomb, land) {
            let x = Math.floor(bomb.body.center.x - land.x);
            let y = Math.floor(bomb.body.center.y - land.y);
            return this.textures.getPixelAlpha(x, y, "currentLand") === 255;
        }
        // create crater 
        createCrater({ x, y }) {
            this.baseCanvas.context.beginPath();
            this.baseCanvas.context.arc(x- this.land.x, y - this.land.y, 23, 0, Math.PI * 2, false);
            this.baseCanvas.context.fill();
            this.baseCanvas.update();
        }
        
        update(){
        
            if(this.bomb && this.bomb.body.y > 160){
                this.bomb.destroy();
                this.createBomb();
            }
        }
    }
    
 var config = {
    type: Phaser.AUTO,
    width: 500,
    height: 153,
    physics: {
      default: "arcade",
          arcade: {
              gravity: { y: 100 }
          }
    },
    scene: [Scene],
    banner: false
}; 

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

Here is a demo-code, that shows how I would approach the whole createCanvas, terrain/land destruction collision mechanic.
It should cover all the points, except the file splitting. I hope this helps.

Bomb & Crater Demo:

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

class Scene extends Phaser.Scene {

        constructor() {
            super({ key: 'testScene' });
        }

        create() {

            // Start  -- GENERATE TEXTURE just for demo
            let helperGraphics = this.make.graphics({x: -250, y: -105, add: false});
            helperGraphics.fillStyle(0xEAEAEA, 1);
            helperGraphics.fillRect(0, 20, 500, 100 );
            helperGraphics.generateTexture('land', 500, 210);
            // End  -- GENERATE TEXTURE just for demo

            this.baseLand = this.textures.get('land').getSourceImage()
            this.baseCanvas = this.textures.createCanvas('currentLand', 800, 200);
            // Create initial Land Texture
            this.baseCanvas.draw(0, 0, this.baseLand);
            this.baseCanvas.context.globalCompositeOperation = 'destination-out';
            // load image to scene
            this.land = this.add.image(0, 20, 'currentLand').setOrigin(0);
            this.physics.add.existing(this.land);
            this.land.body.setImmovable(true);
            this.land.body.setAllowGravity(false);

            // init bomb
            this.createBomb();

        }
        // creates always new bomb
        createBomb(bomb, land) {
            this.bomb = this.add.circle(Phaser.Math.Between(100, 300), -5, 5, 0x6666ff);
            this.physics.add.existing(this.bomb);

            this.physics.add.overlap(
                this.bomb,
                this.land,
                this.overlap,
                this.shouldProcess, this);
        }
        // action on overlap
        overlap(bomb, land) {
            // remove bomb on contact
            let {x, y} = bomb.body.center;
            bomb.destroy();
            // create crater on old bomb position
            this.createCrater({x, y});
            // create new bomb 
            this.createBomb();
        }
        // checks if bomb hits land
        shouldProcess(bomb, land) {
            let x = Math.floor(bomb.body.center.x - land.x);
            let y = Math.floor(bomb.body.center.y - land.y);
            return this.textures.getPixelAlpha(x, y, "currentLand") === 255;
        }
        // create crater 
        createCrater({ x, y }) {
            this.baseCanvas.context.beginPath();
            this.baseCanvas.context.arc(x- this.land.x, y - this.land.y, 23, 0, Math.PI * 2, false);
            this.baseCanvas.context.fill();
            this.baseCanvas.update();
        }
        
        update(){
        
            if(this.bomb && this.bomb.body.y > 160){
                this.bomb.destroy();
                this.createBomb();
            }
        }
    }
    
 var config = {
    type: Phaser.AUTO,
    width: 500,
    height: 153,
    physics: {
      default: "arcade",
          arcade: {
              gravity: { y: 100 }
          }
    },
    scene: [Scene],
    banner: false
}; 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文