【问题标题】:How can I generate a new position only when it arrives there?只有当它到达那里时,我才能生成一个新位置?
【发布时间】:2020-05-14 11:22:44
【问题描述】:

我创建了这些函数:


function generatePosition(){

    var locX = Math.floor(Math.random() * 12) + 5;
    var locY = Math.floor(Math.random() * 10) + 3; 
    console.log('MOVE 0 ' + locX + ' ' + locY );     // MOVE <pacId> <x> <y>

 }

 function sonoArrivato(){

    var locW = [locX, locY];  //position to go
    var locP = [x, y];    //position my pacman
    var test = 0;

    if(locP != locW){
        test = 0;
    } else{
        test = 1;
    }

 }

 function maintainPosition(){

    var saveX = locX;
    var saveY = locY;

    console.log('MOVE ' + saveX + ' ' + saveY );
 }

我希望我的主题转到创建的位置,然后从随机函数创建一个新位置

        generatePosition();
        if (sonoArrivato.test != 0){
            generatePosition();
        }else{
            maintainPosition();
            console.error(visiblePelletCount);
        }

但它每次都会产生一个新的位置

【问题讨论】:

    标签: javascript jquery math random pacman


    【解决方案1】:

    sonoArrivato 是一个函数
    sonoArrivato.test 未定义
    未定义!=0 为真
    这就是为什么它总是产生一个新的位置

    通过添加返回测试更改函数 sonoArrivato;最后并将 if 语句更改为 if(sonoArrivato()!==0){generatePosition();}else{...}

    【讨论】:

    • 我做了,现在它工作了 2 次,然后它停止 if is in a while(true) 所以我不明白
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多