我在做一个将物体切成两半的小游戏.

我需要创建的新形状,以红色填充,但它不工作.

第94-99行是创建新形状的位置,希望用红色填充...

第99行是我目前工作的地方!

我曾try 使用.render.fulStyle将形状变为红色,但这不起作用.如有任何帮助,将不胜感激!

enter image description here

let level1;
window.onload = function() {
    let gameConfig = {
        type: Phaser.AUTO,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 600,
            height: 700
        },
        scene: scene1,
        physics: {
            default: "matter",
            matter: {
                gravity: {
                    y: 0
                },
                debug: false,
            }
        }
    }
    level1 = new Phaser.Game(gameConfig);
    window.focus();

}
//document.body.style = 'margin:0;';

class scene1 extends Phaser.Scene{
    constructor(){
        super("PlayGame");
    }
create(){

    
    let path = [{x:0, y:0}, {x:level1.config.width, y:0}, {x:level1.config.width, y:50},{x: 0, y:50} ];
        this.polygon = this.matter.add.fromVertices(level1.config.width/2, 70, path, { isStatic: true });
        this.polygon.gameObject = this.add.polygon(level1.config.width/2, 70 , path, 0xff0000);
        this.lineGraphics = this.add.graphics();
        this.input.on("pointerdown", this.startDrawing, this);
        this.input.on("pointerup", this.stopDrawing, this);
        this.input.on("pointermove", this.keepDrawing, this);
        this.isDrawing = false;
        this.add.text(13, 11, 'Level 1',{fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif'});

   
    }
    
    startDrawing(pointer){
        this.isDrawing = true;
    }

    keepDrawing(pointer){
        if(this.isDrawing){
            this.lineGraphics.clear();
            this.lineGraphics.lineStyle(1, 0xff0000);
            this.lineGraphics.moveTo(pointer.downX, pointer.downY);
            this.lineGraphics.lineTo(pointer.x, pointer.y);
            this.lineGraphics.strokePath();
    }}
    
    stopDrawing(pointer){
        this.lineGraphics.clear();
        this.isDrawing = false;
        let bodies = this.matter.world.localWorld.bodies;
        let toBeSliced = [];
        let toBeCreated = [];
        for(let i = 0; i < bodies.length; i++){
            let vertices = bodies[i].parts[0].vertices;
            let pointsArray = [];
            vertices.forEach(function(vertex){
                pointsArray.push(vertex.x, vertex.y)
            });
            let slicedPolygons = PolyK.Slice(pointsArray, pointer.downX, pointer.downY, pointer.upX, pointer.upY);
            if(slicedPolygons.length > 1){
                toBeSliced.push(bodies[i]);
                slicedPolygons.forEach(function(points){
                    toBeCreated.push(points)
                });
            }
        }
                
        toBeSliced.forEach(function(body){ 
              body.gameObject.destroy();
              this.matter.world.remove(body)
        }.bind(this))
        
        toBeCreated.forEach(function(points){
            let polyObject = [];
            for(let i = 0; i < points.length / 2; i ++){
                polyObject.push({
                    x: points[i * 2] ,
                    y: points[i * 2 + 1] 
                })
            }
            let sliceCentre = Phaser.Physics.Matter.Matter.Vertices.centre(polyObject)
            let slicedBody = this.matter.add.fromVertices(sliceCentre.x, sliceCentre.y, polyObject, { isStatic: true });
            let topleft = this.matter.bodyBounds.getTopLeft(slicedBody); 
            slicedBody.gameObject = this.add.polygon(topleft.x, topleft.y, polyObject, 0xff0000);



            }.bind(this));
        }
    };
    




<!DOCTYPE html>
<html>
    <head>
        <script src = "phaser.min.js"></script>
        <script src = "polyk.js"></script>
        <script src = "level1.js"></script>
        <script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>
        <script src="http://polyk.ivank.net/polyk.js"></script>

        <style type = "text/css">
            body{
                background-image:url("gamebackground.jpg");
                height: 100%;
                background-position: center;
                background-repeat: no-repeat;
                background-size: cover;
                padding: 0px;
                margin: 0px;
            }

    </head>
    <body>
        <div id = "thegame"></div>
        <script>
    </body>
</html>
import Phaser from 'phaser'

import { scene1 } from 'Desktop/diss/level1.js'
import { scene2 } from 'Desktop/diss/level2.js'
import { scene3 } from 'Desktop/diss/level3.js'
import { scene4 } from 'Desktop/diss/level4.js'
import { scene5 } from 'Desktop/diss/level5.js'
import { scene5 } from 'Desktop/diss/level6.js'

const gameConfig = {
  type: Phaser.AUTO,
  width: 400,
  height: 400,
  backgroundColor: '#000088',
  parent: 'phaser-example',
  scene: [scene1, scene2, scene3, scene4, scene5, scene6]
};

const game = new Phaser.Game(gameConfig)

game()

推荐答案

你可以简单地手动添加游戏对象,要获得顶角,你可以使用this.matter.bodyBounds.getTopLeft(...)函数(link to the documentation)

btw.:如果这个问题是正确的,则功能.render.fillStyle不适用于Phaserjs.

Demo, add new gameObjects:

After slicing the polygon-object it will be destroyed and the parts will be created with a small timeout.

document.body.style = 'margin:0;';

class GameScene extends Phaser.Scene {
    create(){
        let path = [{x:0, y:0}, {x: config.width, y:0}, {x:config.width, y:50},{x: 0, y:50} ]
        this.polygon = this.matter.add.fromVertices(config.width/2, 70, path, { isStatic: true });
        this.polygon.gameObject = this.add.polygon(config.width/2, 70 , path, 0x00ff00);
            
        // ... Remove to keep demo short Demo
         
        this.input.on("pointerdown", this.startDrawing, this);
        this.input.on("pointerup", this.stopDrawing, this);
        this.input.on("pointermove", this.keepDrawing, this);   
    }
    
    startDrawing(pointer){
        // ... Remove to keep demo short Demo
    }
    
    stopDrawing(pointer){
        let bodies = this.matter.world.localWorld.bodies;
        let toBeSliced = [];
        let toBeCreated = [];
        for(let i = 0; i < bodies.length; i++){
            let vertices = bodies[i].parts[0].vertices;
            let pointsArray = [];
            vertices.forEach(function(vertex){
                pointsArray.push(vertex.x, vertex.y)
            });
            let slicedPolygons = PolyK.Slice(pointsArray, pointer.downX, pointer.downY, pointer.upX, pointer.upY);
            if(slicedPolygons.length > 1){
                toBeSliced.push(bodies[i]);
                slicedPolygons.forEach(function(points){
                    toBeCreated.push(points)
                });
            }
        }
                
        toBeSliced.forEach(function(body){ 
              body.gameObject.destroy();
              this.matter.world.remove(body)
        }.bind(this))
        
        // JUST for the demo to see it appear
        setTimeout( _ => {
                toBeCreated.forEach(function(points){
                    let polyObject = [];
                    for(let i = 0; i < points.length / 2; i ++){
                        polyObject.push({
                            x: points[i * 2] ,
                            y: points[i * 2 + 1] 
                        })
                    }
                    let sliceCentre = Phaser.Physics.Matter.Matter.Vertices.centre(polyObject)

                    let slicedBody = this.matter.add.fromVertices(sliceCentre.x, sliceCentre.y, polyObject, { isStatic: true });
                    let topleft = this.matter.bodyBounds.getTopLeft(slicedBody); 

                    slicedBody.gameObject = this.add.polygon( topleft.x, topleft.y, polyObject, 0xff0000);

                }.bind(this));
        }, 500);
    }
    
    keepDrawing(pointer){
        // ... Remove to keep demo short Demo
    }
}

var config = {
    width: 536,
    height: 183,
    scene: GameScene,
        physics: {
        default: 'matter',
        matter: {
          debug: true,
        }
    }
}; 

new Phaser.Game(config);
console.clear();
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>
<script type="text/javascript" src="http://polyk.ivank.net/polyk.js"></script>

Javascript相关问答推荐

JavaScript:循环访问不断变化的数组中的元素

如何提取Cypress中文本

如何修复循环HTML元素附加函数中的问题?

如何解决chrome—extension代码中的错误,它会实时覆盖google—meet的面部图像?'

微软Edge编辑和重新发送未显示""

如何在Javascript中使用Go和检索本地托管Apache Arrow Flight服务器?

如何将Map字符串,布尔值转换为{key:string;value:bo布尔值;}[]?<>

在react JS中映射数组对象的嵌套数据

如何从HTML对话框中检索单选项组的值?

如何在文本字段中输入变量?

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

为什么我的按钮没有从&q;1更改为&q;X&q;?

Next.js中的服务器端组件列表筛选

如何使用Astro优化大图像?

Cherrio JS返回父div的所有图像SRC

Reaction useState和useLoaderData的组合使用引发无限循环错误

如何使用puppeteer操作所有选项

如何使用Reaction路由导航测试挂钩?

在将元素追加到DOM之前,createElement()是否会触发回流?混淆abt DocumentFragment行为

在高位图中显示每个y轴系列的多个值