简体   繁体   English

Javascript:If 语句不执行,尽管条件为真,并且要执行的代码在 If 之外工作

[英]Javascript: If statement not executing despite condition true and code to be executed works outside of If

I am trying to complete an assignment that simulates the micromouse challenge, where I need to map walls and then find the best path to the goal.我正在尝试完成一项模拟微型鼠标挑战的作业,我需要在其中绘制墙壁图,然后找到通往目标的最佳路径。 I have been building out my blocks to move and to detect collision with a wall and was having success until my screen froze.我一直在构建我的块来移动并检测与墙壁的碰撞并且一直成功,直到我的屏幕冻结。 I had my code saved and so restarted my laptop and pasted my code back in but now one of my If statements doesn't execute and I can't see what is wrong.我保存了我的代码,因此重新启动了我的笔记本电脑并重新粘贴了我的代码,但现在我的一个 If 语句没有执行,我看不出哪里出了问题。 Each function (the in-built isColllisionDetected() and my function offWall()) works separately for the same move input.每个函数(内置的 isColllisionDetected() 和我的函数 offWall())分别为相同的移动输入工作。 But together I now get nothing.但现在在一起我什么也得不到。
This is my first post here so not sure if you need more information thn the below, please let me know.这是我在这里的第一篇文章,所以不确定您是否需要更多信息,请告诉我。 Any guidance on what I am missing would be hugely appreciated.任何关于我所缺少的指导将不胜感激。 Key points:关键点:

  1. isCollisionDetected() is an inbuilt function of the environment. isCollisionDetected() 是环境的内置函数。 When the robot hits a wall this returns true当机器人撞墙时返回真
  2. offWall() is my code, when called the robot moves in reverse for a few pixels and then stops. offWall() 是我的代码,当被调用时,机器人反向移动几个像素然后停止。 I have included this code just in case, although it refers to other functions I have not shown here.我已包含此代码以防万一,尽管它指的是我未在此处显示的其他功能。 I need to use this when I hit a wall so I can move away from the wall and then reorient the robot.当我撞到墙上时我需要使用它,这样我就可以远离墙壁,然后重新调整机器人的方向。
  3. When I call the offWall() function on it's own, the code executes correctly.当我自己调用 offWall() 函数时,代码正确执行。
  4. When I deliberately run the robot into a wall and println(isCollisionDetected()), the output is true.当我故意将机器人撞到墙上并 println(isCollisionDetected()) 时,输出为真。
  5. My If statement as below does not execute, despite the individual components seemingly working in isolation尽管各个组件似乎独立工作,但我的以下 If 语句未执行

edited to show full code编辑以显示完整代码

full code完整代码

// function for directional movement
function direction() {
    if ((getHeading() >= 1.47) && (getHeading() <= 1.66)) {
        return "South";
    }
    if ((getHeading() >= 4.62) && (getHeading() <= 4.80)) {
        return "North"; 
    } 
    if ((getHeading() >= 6.19) && (getHeading() <= 0.87)) {
    return "East"; 
    }
    if ((getHeading() >= 3.05) && (getHeading() <= 3.23)) {
    return "West"; 
    }
}

// function for tile coordinates
function tileX() {
    return (Math.floor(getX() / 64));
}
function tileY() {
    return (Math.floor(getY() / 64));
}

// truncate decimal places to 4
const formatter = new Intl.NumberFormat('en-US', {
   minimumFractionDigits: 4,      
   maximumFractionDigits: 4,
});

// calculate diagonal distance
function magnitude(x, y) {
    return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}

// velocity calculation
velocity = magnitude(getVelocityX(), getVelocityY());

// create turn function with degrees conversion
function turn(angleDeg) {
    angleDeg = angleDeg * (formatter.format(Math.PI / 180));
    let delta = 0;
    do {
        delta = angleDeg - getHeading();
        let v = Math.min(1 , Math.abs(delta));
        if (Math.abs(delta) > 0.002) {
             if (delta > 0) {
                setLeftPower(v);
                setRightPower(-v);
            } else {
                setLeftPower(-v);
                setRightPower(v);
            }
        } else {
            setLeftPower(0);
            setRightPower(0);
        }
    } while (Math.abs(delta) > 0.002);
}

// move foreward function with distance in tiles
function forward(distance) {
    clearCollision();
    distance = distance * 64;
    const startX = getX();
    const startY = getY();
    const leeway = 5;
    let travelled = 0;
    let delta = 0;
    let velocity = magnitude(getVelocityX(), getVelocityY());
    do {
        travelled = magnitude(
           getX() - startX, getY() - startY
        );
        delta = distance - travelled;
        if (Math.abs(delta > leeway)) {
            let v = Math.min(1 , Math.abs(delta) / 64);
            if (delta > 0) {
                setLeftPower(v);
                setRightPower(v);
            } else {
                setLeftPower(-v);
                setRightPower(-v);
            }
        } else {
            setLeftPower(0);
            setRightPower(0);           
        }
    } while (
        !isCollisionDetected() && (
            Math.abs(delta) > 3 ||
            Math.abs(velocity) > 3
       )
    );
    setLeftPower(0);
    setRightPower(0);
}

function offWall() {
    const d = 5;
    const startX = getX();
    const startY = getY();
    
    let travelled = 0;
    do {
        travelled = magnitude(
           getX() - startX, getY() - startY
        );
        if (travelled < d) {
        setLeftPower(-0.1);
        setRightPower(-0.1);
        }   
    } while (travelled < d);
    
    setLeftPower(0);
    setRightPower(0);
}


// wall locator functions for Y and X
function wallLocY() {
    if (direction("South")) {
    return (tileY() + 1);
    }
    if (direction("North")) {
    return (tileY() - 1);
    }
}
function wallLocX() {
    if (direction("East")) {
    return (tileX() + 1);
    }
    if (direction("West")) {
    return (tileX() - 1);
    }
}

This is all followd by my If statement:这一切都在我的 If 语句之后:

if (isCollisionDetected()) {
    println("wall found");    
    offWall();
    setLeftPower(0);
    setRightPower(0);
    clearCollision();
}

Which is then followed by me calling a movement function, eg forward(9);然后我调用一个移动函数,例如 forward(9);

Just answering this formally, thanks to @Jaromanda X.感谢@Jaromanda X,只是正式回答这个问题。

I needed to move the If statement to after calling my movement function so it can run at the right time in my code block.我需要在调用我的移动函数后移动 If 语句,以便它可以在我的代码块中的正确时间运行。 thanks for helping me, I was feeling crazy :)谢谢你帮助我,我感觉疯了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM