javascript - 如何通过多个函数传递值
问题描述
这是我的示例项目 List 和 updateItems 方法(后来导入“更新为 updateItems”)
export const itemsForest = [
{
x: 3, y: 3,
mainTexture: "house.png",
interaction: function(){
console.log("enterHouse");
}
},
{
x: 4, y: 4,
mainTexture: "letter.png",
interaction: function(){
console.log("test");
}
},
{
x: 3, y: 8,
mainTexture: "exitForest.png",
interaction: function(){
return {changeMap: true, map: mapIsland, items: itemsIsland, playerPos: {x: 3, y: 8}};
}
}
]
export function update(currentItems){
for(const item of currentItems){
if(underCharacter(item)){
item.interaction();
}
}
}
这是我的主要更新方法,我在其中调用 update Items 以及出现问题的地方:
function update(){
updateCharacter(currentMap);
if(updateItems(currentItems).changeMap === true){
currentMap = updateItems(currentItems).map;
currentItems = updateItems(currentItems).items;
}else{
updateItems(currentItems);
}
}
真的不知道如何将交互返回的内容传递给更新我也尝试返回 item.interaction() 但它没有帮助也许有更好的方法,我需要更改其他文件中的变量:
let currentMap = mapIsland;
let currentItems = itemsIsland;
const entitiesBoard = document.querySelector("#entitiesBoard");
function main(currentTime){
if(gameOver){
return alert("You Lose");
}
// window.requestAnimationFrame(main);
const secondsSinceLastRender = (currentTime - lastRenderTime)/1000;
if(secondsSinceLastRender < 1 / gameSpeed) return;
lastRenderTime = currentTime;
update();
draw();
}
事情是 IM 得到错误:
Uncaught TypeError: Cannot read property 'changeMap' of undefined
at update (game.js:51)
at main (game.js:36)
at game.js:41
如果有人可以提供帮助,我会很高兴向我解释为什么这种方式而不是另一种方式
总之:我想要这样的工作:
function a{
return {1}
}
export function b{
function a()
}
import function b;
function c{
//Get Somehow function a value
}
解决方案
推荐阅读
- mongodb - 我在哪里可以托管使用 mongodb 作为后端服务器的 web 应用程序
- bash - 有没有办法阻止 cp 覆盖两个全局文件中的第二个
- java - Spring WebClient - 带有字符串的 toEntityList
- c - 如何比较С中的*(void *)?
- twitter-bootstrap - 依赖一个框架,比如 Bootstrap,是不是“危险”?
- html - 浏览器窗口大小,然后在模板中
- java - 列出文件夹内容,包括所有子目录和文件
- java - How to use one type in method?
- jquery - 只想在我的页面上显示日历,它可以使用角度选择日期范围,bootstrap
- json - 在 Express API 中全局引用 UUID