微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过多个函数传递值

如何解决如何通过多个函数传递值

这是我的示例项目列表和更新项目方法(后来导入“更新为更新项目”)

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();
        }
    }
}

这是我的主要更新方法,我在其中调用更新项并出现问题:

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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?