Reputation: 7657
I don't understand this at all. Adding a while loop causes a variable to throw an "Uncaught type error cannot read property of 'perp'". I tried many solutions, separating code, adding additional typing, trying to make :any type, I am truly at a loss.
Little background info: I am trying to make a quick little game called "Three's a Crime", essentially I have an algorithm that displays up to 3 criminals, at most 2 of them may be the actual perpetrators.
All the code is publicly view-able: https://github.com/treehau5/ThreesACrime
function main(num) {
var is_over = false;
var game = new GameObject.Game(shuffled_criminals, num);
$('body').append(game.display());
$("#status").text("Turn in progress");
$('body').append('<div class="container"><p>Enter your guess<p><div class="input-append" id="turn_form"><form>\
<input class="span2" id="Text1" type="text" />\
<button class="btn" id="Button1" type="button">Go!</button>\
</form>\
</div></div>');
console.log(game.getThree());
while (true) //removing this while loop causes above line to work fine.
{ }
}
here is the getThree method within the game class
getThree() {
var result: Criminal[] = [];
var temp: Criminal[] = _.shuffle(this.criminals);
var num_perp = 0;
while (result.length != 3) {
while ((num_perp != 3) && (result.length != 3)) {
var x: Criminal = temp.pop();
result.push(x);
if (x.perp == true)
num_perp++;
temp = _.shuffle(temp);
}
do {
temp = _.shuffle(temp);
var y: Criminal = temp.pop();
if (y.perp == false)
result.push(y);
}
while (result.length != 3)
}
return result;
}//end displayThree
I am using underscorejs for the shuffling, and the DefinitelyTyped underscore typings.
Feel free to view or clone the repo to try. This error is occuring in Google Chrome 26. All the code is within game.ts, gameobject.ts and main.ts, theres lots of references to outside libraries like underscore and jquery ui which I plan to spruce up the GUI a little once I get the main game logic complete.
Thank you for any who have even bothered to read all this, your help is much appreciated.
edit: so I'm trying to use a loop like this to put a "turn" in the game
while (is_over)
{
break;
}
Upvotes: 0
Views: 9602
Reputation: 276235
Try
while(false){
// Unreachable code
do_something;
break;
}
If what is inside the condition ie. anything
in your case never becomes false then it is effectively while(true)
which as mentioned by steve fenton would be stopped by browsers or crash.
A slightly better way to loop would be:
function loop(){
var result = do_something();
// Repeat
if (result) {setTimeout(loop,100);}
// Break
else return;
}
setTimeout(loop,100);
Although I would recommend using a game engine instead of setTimeout. e.g http://www.createjs.com/#!/EaselJS
Upvotes: 1
Reputation: 251172
In your question you are using the following example:
while (true) //removing this while loop causes above line to work fine.
{ }
This tight loop will prevent pretty much anything from happening (and usually the browser will ask if you want to terminate the script.
If this isn't the code you are using, please show the code you are using - ideally a shortened version that demonstrates the issue rather than an entire program. I have tested several variations of loops in TypeScript and can find no problem with a while loop.
Upvotes: 2