enter image description here

我需要实现的是对玩家(主角)内部较小的hitbox与墙壁的碰撞检测,以便它给出玩家能够在墙壁旁边行走的图像.这应该非常简单,但碰撞检测确实有效.相关零件有墙层和hitbox sprote.这是我的代码:

preload(){
  //all the assets here
}
create() {
  console.log('create method executed');


  const characterPositionInWorldMap = 3700;
  this.hitbox = this.physics.add.sprite(characterPositionInWorldMap, 250, null); // we need collision for this
  this.protagonist = this.physics.add.sprite(characterPositionInWorldMap, 250, 'protagonist');
  
   // Initialize the hitbox sprite for collision detection
  
  this.hitbox.setSize(10, 10); // Set hitbox size (adjust as needed)

  
  this.protagonist.setDepth(3);
  this.hitbox.setDepth(3);
  const map = this.make.tilemap({ key: 'map' });
  this.animatedTiles.init(map);
  this.physics.world.createDebugGraphic();

// Add the tilesets...



// Create layers from tilemap data using different tilesets
const groundLayer = map.createLayer('Groundlayer', darkTileset, 0, 0);
const waterLayer = map.createLayer('waterlayer', darkTileset, 0, 0);
const grassLayer = map.createLayer('grass_and_tree_layer', darkTileset, 0, 0);
const stoneLayer = map.createLayer('stones_n_stuff', darkTileset, 0, 0);

// Create hut layer using the hut tileset
const hutLayer = map.createLayer('hutLayer', hutTileset, 0, 0);
const transportlayer = map.createLayer('transportlayer', hutTileset, 0, 0);
const walls = map.createLayer('walls', hutTileset, 0, 0);


  // Adjust the depth of layers as needed
  groundLayer.setDepth(1);
  waterLayer.setDepth(0);
  grassLayer.setDepth(1);
  stoneLayer.setDepth(2);
  hutLayer.setDepth(3);
  walls.setDepth(4);
  transportlayer.setDepth(10);

  // Enable collision detection between player and walls layer
  const collidableTileIndexes = [
    1279,1278,1244,1225,1257,1267,1302,1302,1320,1305,1277,1298,1307,1288,1319,1320,1321,1225,1223,1244
];

  collidableTileIndexes.forEach(index => {
    map.setCollision(index, true, walls);
});


  //animations....
 
  // Enable physics for the protagonist
  this.physics.world.setBounds(0, 0, map.widthInPixels, map.heightInPixels);

  // Listen for collision between hitbox and walls
this.physics.add.collider(this.hitbox, walls, () => {
// Disable movement controls for the protagonist
//this.protagonist.setVelocity(0); // Stop the protagonist's movement
console.log("hitbox hit")
});

// Listen for when hitbox stops colliding with walls
this.physics.add.collider(this.hitbox, walls, () => {
// Re-enable movement controls for the protagonist
// You may need to adjust this depending on how your movement controls are implemented
// For example, if you're using velocity-based movement, reset the velocity to allow movement
this.protagonist.setVelocity(0); // Reset velocity
});

this.groundLayer = groundLayer;
this.waterLayer = waterLayer;
this.grassLayer = grassLayer;
this.stoneLayer = stoneLayer;
this.hutLayer = hutLayer;
this.walls = walls;
this.transportlayer = transportlayer;


 // Set up camera to follow the player
 this.cameras.main.startFollow(this.protagonist);

}

update() {
  // Reset velocity
  this.protagonist.setVelocity(0);
  
  this.physics.collide(this.hitbox, this.walls, this.handleWallCollision, null, this);
    // Update the hitbox sprite position to match the player sprite
    this.hitbox.setPosition(this.protagonist.x, this.protagonist.y);
    ...
}

推荐答案

One issue I see, just reading the code is, that you are moving the hitbox yourself.
Arcade (and probably matter) Physics don't work when you move / set the position of the game object, since you are overriding the position calculation of the engine (and also the object can be set inside the other object).

如何解决你的问题并不是那么直接,想到的一个解决方案是简单地使用玩家的hitbox/身体,并使用collide函数的processCallback参数(第四个)来确定玩家是否应该碰撞(link to the documentation).

根据您的使用情况以及您需要的精确程度,简单判断相对位置(ontop, below, left or right)和与两个碰撞物体中心的距离就可以做到这一点.

Javascript相关问答推荐

我想做一个程序,计算字符串中所有单词的数量

如何在使用fast-xml-parser构建ML时包括属性值?

node TS:JWT令牌签名以验证客户端和后台问题之间的身份验证

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

Plotly热图:在矩形上zoom 后将zoom 区域居中

拖放仅通过 Select 上传

Rehype将hashtag呈现为URL

我怎么才能得到Kotlin的密文?

在我的index.html页面上找不到我的Java脚本条形图

扩展类型的联合被解析为基类型

使用auth.js保护API路由的Next.JS,FETCH()不起作用

在画布中调整边上反弹框的大小失败

将对象推送到数组会导致复制

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

无法重定向到Next.js中的动态URL

Phaser3 preFX addGlow不支持zoom

如何在尚未创建的鼠标悬停事件上访问和着色div?

如果对象中的字段等于某个值,则从数组列表中删除对象

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