我创建了两个对象(一个实心圆和一个笔划圆).但它们根本不会像预期的那样相撞.

以下是我的代码:

const config = {
    type: Phaser.AUTO,
    width: 400,
    height: 400,
    backgroundColor: "#f5bd22",
    scene: {
        preload: preload,
        create: create,
        update: update
    },
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 90 },
            debug: false
        }
    }
}

const game = new Phaser.Game(config);

let circle;
let hollowCircle;

function preload() {

}

function create() {
    circle = this.add.circle(200, 200, 50, '#f5bd22');

    hollowCircle = this.add.graphics();
    hollowCircle.lineStyle(2, 0x000000);
    hollowCircle.strokeCircle(200, 200, 100);

    this.physics.world.enable([circle, hollowCircle]);

    hollowCircle.body.gravity.y = -90;
    this.physics.add.collider(circle, hollowCircle);
}

function update() {

}

它正在创建一个笔画圆圈,里面有一个实心圆圈.重力设置为实体为90,笔划为0. 当代码运行时,实心圆落下,笔划保持不变.但这两个圆圈碰在一起,什么也不会发生.

推荐答案

这就是你想要做的,不能这样做.街机物理学只是在物理体的外部相撞.此外,如果您查看调试边界框,您可以看到"hollowCircle"的物理框位于左上角(102 100 and 101 Gameobject, don't react as other GameObjects in phaser).

Screenshot Bounding box

也就是说,如果你想要这种效果,一个简单的解决方法就是创建几个小的物理对象,覆盖轮廓的"空心"- Shape,然后再设置与这些对象的碰撞.

Here a Small Demo:
(Here I'm creating a circle with 32 small rectangles so keep the shape, and I made the physics-box of the inner Circle round so that it looks a bit better)

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

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    backgroundColor: "#f5bd22",
    physics: {
        default: 'arcade',
        arcade: {            
            gravity:  { y: 90 },
            // turn on to show the created Objects
            // debug: true
        }
    },
    scene: {
        create
    },
}; 

function createCircle(scene, x, y, radius){

  // CREATE VISUALS
  let displayCircle = scene.add.circle(x, y, radius).setStrokeStyle(2,0x000000);
  
  // CREATE PHYSICS PART
  const circle = new Phaser.Geom.Circle(x, y, radius);
  const points = circle.getPoints(32);
  let boxes = [];

  for (let i = 0; i < points.length; i++){
      const p = points[i];

      let box = scene.add.rectangle(p.x - 2, p.y - 2, 4, 4);
      // with the second parameter I'm making the object(s) static, so no need for the -90 gravity
      let boxPhysics = scene.physics.add.existing(box, true);
      boxes.push(box);
  }
    
  return boxes;
}


function create() {
    let circle = this.add.circle(200, 80, 30, '#f5bd22');
    let circlePhysics = this.physics.add.existing(circle);
    circlePhysics.body.setCircle(30);

    let helperBoxes = createCircle(this, 200, 100, 70)
    
    this.physics.add.collider(circle, helperBoxes);
}

const game = new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

要查看正在发生的情况,请再次打开config中的调试标志.

Javascript相关问答推荐

使用redux-toolet DelivercThunks刷新访问令牌

如何使用侧边滚动按钮具体滚动每4个格?

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

如何找出摆线表面上y与x相交的地方?

为什么promise对js中的错误有一个奇怪的优先级?

用JavaScript复制C#CRC 32生成器

cypress中e2e测试上的Click()事件在Switch Element Plus组件上使用时不起作用

使用GraphQL查询Uniswap的ETH价格

优化Google Sheet脚本以将下拉菜单和公式添加到多行

S文本内容和值不必要的不同

将核心模块导入另一个组件模块时存在多个主题

如何修复我的数据表,以使stateSave正常工作?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

为什么云存储中的文件不能公开使用?

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

将嵌套数组的元素乘以其深度,输出一个和

我不知道如何纠正这一点.

使用Perl Selify::Remote::Driver执行Java脚本时出错

Chrome上的印度时区名S有问题吗?

如何动态呈现适合未知屏幕大小的最大数量的表行?苗条的