Q: Why is movement restricted in these directions depending on the canvas' width/height + player position, and how do I fix this?

正如标题中提到的,我一直在构建一个2D画布游戏(一个像精灵宝可梦一样的自上而下的生物Collection 家),without implementing any collision有一个神秘的错误.

这个错误阻止了我的角色在两个方向上移动,这取决于角色的起始位置和画布的宽度/高度,尽管我可以在被停止之前在这些方向上移动到一些瓷砖:

  • 上/右

  • 向左/向下

100

我想要的宽度和高度是360x240,以显示 map 的24x24像素的平铺,15跨宽,10跨高.当try 使用720x480(所需尺寸的2倍)和300x200机芯was not restricted时.

Here is a link to the repo f或 the bug if you would like to test it out: https://github.com/LateSupper/CreatureCollect或Bug

我在"Engineering.js"文件中包含了两个不同大小的 map 来测试这个问题.只需切换一下你想要测试哪一个.

And here is an image of where the invisible diagonal barrier seems to be in the example I have in the repo: Invisible Barrier Visual (Player starts on the yellow square

我试过的东西包括:

  • 控制台记录移动逻辑,以查看它是否仍会触发(会,但没有移动)
  • 将画布的宽度/高度设置为不同的尺寸,结果不同,并且大多数画布的纵横比尺寸为3:2,不允许在这些方向上移动
  • 创建了不同的 map ,通过我的代码的逻辑改变了玩家的起始位置,并改变了两个方向被阻止
  • Googling.当然,我今天用谷歌搜索了一下,因为为什么会发生这种事?这是我见过的最奇怪的关于一个HTML画布游戏的错误.

添加我认为可能会给我带来麻烦的代码的建议为edit:%:

// "main.js" -> line 1
const game = new GameEngine(
  { width: 24 * 16, height: 24 * 10 }, // Dimensions
  24, // Tile Size
  18 // Camera Offset
)

In the above code when the width and/或 height is multiplied by an odd number movement is restricted, but if both numbers are 24 * an even number it w或ks perfectly fine.

这可能会成为我如何在画布上绘制背景/玩家精灵的问题吗?:

// "engine.js" -> GameEngine.determineSpriteProperties()
determineSpriteProperties = (image) => {
  const properties = {
    type: 0,
    position: { x: 0, y: 0 },
    image: image
  }
  if (image.currentSrc.indexOf("map_") > -1) {
    properties.position.x = ((this.canvas.width / 2) - (image.width / 2)) + (this.tileSize / 2),
    properties.position.y = ((this.canvas.height / 2) - (image.height / 2))
  } else {
    properties.type = 1
    properties.position.x = (this.canvas.width / 2 - (image.width / 3)) + ((image.width / 3) / 2)
    properties.position.y = this.canvas.height / 2 - (image.height / 4) + this.cameraOffset
  }
  return properties
}

推荐答案

The problem was in the condition to stop the movement...
You had an if statement:

this.mapSprite.position.x !== this.targetPosition.position && 
this.mapSprite.position.y !== this.targetPosition.position

...但是targetPosition还有一个您没有包含在该条件中的轴属性

(targetPosition.axis == "x" && mapSprite.position.x !== targetPosition.position) ||
(targetPosition.axis == "y" && mapSprite.position.y !== targetPosition.position)

( I removed some code to make this sample small )


See my PR for details:
https://github.com/LateSupper/CreatureCollectorBug/pull/1

Javascript相关问答推荐

如果被1个Phaser JS抵消,我的倾斜碰撞

我的YouTube视频没有以html形式显示,以获取免费加密信号

当promise 在拒绝处理程序被锁定之前被拒绝时,为什么我们会得到未捕获的错误?

如何在不创建新键的情况下动态更改 map 中的项目?

处理时间和字符串时MySQL表中显示的日期无效

如何调用名称在字符串中的实例方法?

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

如何在ASP.NET中使用Google Charts API JavaScript将条形图标签显示为绝对值而不是负值

如何使用TypeScrip设置唯一属性?

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

如何在HTMX提示符中设置默认值?

expo 联系人:如果联系人的状态被拒绝,则请求访问联系人的权限

如何为仅有数据可用的点显示X轴标签?

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

P5.js中的分形树

在Java脚本中录制视频后看不到曲目

与在编剧中具有动态价值的定位器交互

Promise.race()返回已解析的promise ,而不是第一个被拒绝的promise

TabNavigator和StackNavigator之间的Reaction Native中的导航问题

如何将字符串拆分成单词并跟踪每个单词的索引(在原始字符串中)?