Hello,

I'm using the DX Studio engine (used to develop 3d games/applications), which uses javaScript as the main language.
Since the engine is not getting much attention, their forum is almost completely dead. So I thought maybe one of you here at daniWeb could help me out?

When previewing my application it crashes. I'm sure it's because of the while loop, but why, I don't know:

(Some of these is DX Studio events, but I just want you to look at the while loop)

// Variable
var run = false;

// Detect if "w" key is down
function onKeyChange(input)
{
    if ( input.keyId == "w" && input.keyDown )
    {
        run = true;

        // Begin printing "True" to the console
        while ( run == true )
        {
            print("True");

            // When run is false, print "False" and escape the loop with break
            if ( run == false )
            {
                print("False");
                break;
            }
        }
    }
    // Detect if "w" key is released
    else if ( input.keyId == "w" )
    {
        run = false;
    }
}

So, when W is down, it prints "True" and when it's released it should print "False" and escape the loop with break. But this causes the application to crash.

Any ideas?

Chris

I am not familiar with what you are trying to accomplish, but its clear that in your code, lines 17-21 will never execute because run will always equal to "true" prior to getting to line 17.

@JorgeM

Right, line 17-21. So, it will never execute because it's always true?

On line 25-28, run actually goes false (when "w" key is released).
I tried putting line 25-28 up to the top of the code, didn't work either.

Hmmm

Right, line 17-21. So, it will never execute because it's always true?

It is never true because you are setting "run = true;" on line 9. When it gets to line 17, the varaible "run" still equals to "true".

On line 25-28, run actually goes false (when "w" key is released).

yes, i see that, but once you set "run=false" in there is no more code to run in that block, so that is all that happens there. Once the function executes again and you get to " if ( input.keyId == "w" && input.keyDown )", on the next line you are setting "run=true" again. So you see its not possible for "run=false" in that if block.

In your sample code above, the program crashes because of the while loop. "while ( run == true )" When you get to that block of code run is always true so the loop never ends.

What is it that you are trying to make happen? Forget the code for a second... in plain english? In other words, what should the workflow look like?

Since i dont know what your program is trying to do I cant really fine tune your code, but if you simply want to set something to true when you have w and keypress down and false when its keypress up, it should look something like this...

// Variable
var run = false;

function onKeyChange(input)
{
    if ( input.keyId == "w" && input.keyDown )
    {
        run = true;
        print("True");
    }
    // Detect if "w" key is released
    if ( input.keyId == "w" )
    {
        run = false;
        print("True");
    }
}

If you are trying to detect whether the user is holding down the "w" key, you'll need someother type of event/process to detect that. I did a quick search online regaring "keyhold" and seems like other people have posted a variety of different ways to accomplish this.

@JorgeM

Thanks man, appreciate it. Yes, I am trying to accomplish the keyholding, that was why I was using the while loop, but now I see that won't work. I'm googling.

Thanks again,
Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.