首页 > 解决方案 > 如何通过多个函数传递值

问题描述

这是我的示例项目 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
}

标签: javascriptfunctionvariablesreturngame-engine

解决方案


推荐阅读