【发布时间】:2022-12-24 11:16:52
【问题描述】:
我为一款名为 Pirate Invasion 的游戏创建了一个暂停按钮。我试图在他们的位置暂停/冻结身体并从他们所在的位置恢复他们。
我使用setStatic和isStatic使用function mousePressed()。但是,它们都显示错误Uncaught TypeError: Matter.Bodies.setStatic is not a function。
我是不是错过了什么,或者这与保存职位或类似的东西有关?
船是必须“暂停/冻结”的物体。
function mousePressed(playButton) { // this is my play button which works perfectly
gameState = "play"
World.remove(world, playButton)
World.remove(world, playImage)
}
function mousePressed(pauseButton) { // pause button which gives an error
Matter.Bodies.setStatic(boats, true)
}
【问题讨论】:
-
物理引擎通常在主循环中有一个步进函数调用,暂停它就像不调用该函数一样简单。请出示相关代码。
-
谢谢,刚刚上传了代码...函数 mousePressed() 在 draw() 函数中。
-
Chris 的建议很好。感谢您提供代码,但这不完全是 minimal reproducible example(显示问题所需的最少代码)。当您遇到错误时,请在您的问题中提供该错误,以便我们都在同一页面上,而不必猜测您的问题。
-
感谢更新。你必须遍历每个身体并在其上设置静态:
boats.forEach(e => Matter.Body.setStatic(e, true));。但是可能有一个 better way 来暂停渲染循环。 This answer 展示了一种暂停不同场景的方法——您可以使用runner.enabled在单个场景上执行此操作。看起来您在这里使用的是 p5.js 和 MJS?
标签: javascript p5.js matter.js