Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

It just never goes back into the function. I've tried using a simple if statement, and a while wouldn't work because that would create an infinite loop which locks the script up. It goes through the function once, stores nothing, returns nothing, and compares nothing. I can see how the logic is wrong, yet I don't know how to make it so it constantly searches for player1's input.

edit: And this just makes it go straight from player1's turn to player2's turn.

code:
//omitted
For reasons I don't understand. How is counter being incremented unless a player clicks on a button?

There seems to be a fundamental misunderstanding here: Do not run the .on() function more than once per element. The .on() function adds an event handler for the named event. Every time you run .on('click', function ...), you are saying "when the user clicks this element, also run this function in addition to other functions that already run."

I have rewritten your code above so that it works exactly as it did before, but hopefully the logic is clearer:

JavaScript code:
function player1Turn() {
    alert("Es turno del jugador uno!");
    var counter = 0;
	
	var c1iClicked = function() {
		redButton();
		player1.sequence[player1.sequence.length] = 1;
		counter++;
	};
	var c2iClicked = function() {
		yellowButton();
		player1.sequence[player1.sequence.length] = 2;
		counter++;
	};
	var c3iClicked = function() {
		blueButton();
		player1.sequence[player1.sequence.length] = 3;
		counter++;
	};
	var c4iClicked = function() {
		greenButton();
		player1.sequence[player1.sequence.length] = 4;
		counter++;
	};
	
    if (counter < storedSequence.length) {
        $("#c1i").on("click", c1iClicked);
        $("#c2i").on("click", c2iClicked);
        $("#c3i").on("click", c3iClicked);
        $("#c4i").on("click", c4iClicked);
    } else {
        player2Turn();
    }
}
Below code is almost the same, but not exactly, because counter is outside the player1Turn function. It will work the same in this specific case, though:
JavaScript code:
var counter = 0;

var c1iClicked = function() {
	redButton();
	player1.sequence[player1.sequence.length] = 1;
	counter++;
};
var c2iClicked = function() {
	yellowButton();
	player1.sequence[player1.sequence.length] = 2;
	counter++;
};
var c3iClicked = function() {
	blueButton();
	player1.sequence[player1.sequence.length] = 3;
	counter++;
};
var c4iClicked = function() {
	greenButton();
	player1.sequence[player1.sequence.length] = 4;
	counter++;
};

function player1Turn() {
    alert("Es turno del jugador uno!");
	
    if (counter < storedSequence.length) {
        $("#c1i").on("click", c1iClicked);
        $("#c2i").on("click", c2iClicked);
        $("#c3i").on("click", c3iClicked);
        $("#c4i").on("click", c4iClicked);
    } else {
        player2Turn();
    }
}
I suspect that you think that the if (counter < storedSequence.length) conditional is run every time the player clicks on an element. That is not true. It is run once every time you call player1Turn(). If the condition is true, the code inside the if block is run. That sets up the click handlers for the elements. Those click handlers then run unconditionally every time the user clicks on an element.

Adbot
ADBOT LOVES YOU

Tres Burritos
Sep 3, 2009

Plorkyeran posted:

Did you actually read that article, or did you just laugh at the introduction then close the page? It doesn't actually try to claim that AppCache is unusable; merely that it has a bunch of gotchas you have to keep in mind.

God bless anyone who could make it through that whole thing, that was terribly written.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Tres Burritos posted:

God bless anyone who could make it through that whole thing, that was terribly written.

Thank god someone else thinks that. I hated that article and I find it obnoxious not only in its writing style, but also how its turned people off AppCache just due to how negative the article reads, disregarding the facts presented. Its not technical, but its not a manifesto either, It just sort of.. is.

pepito sanchez
Apr 3, 2004
I'm not mexican
Thanks, Wheany. I also learned today that writing var storedSequence = gameMode.sequence only created a reference to the gameMode.sequence array. It didn't actually copy its contents into a new variable. I have to clone it, or slice it. I also have to rewrite lots of code so everything is stored in a single associative array instead of two separate player arrays. It should simplify things in the long run.

edit: Yet I'm having serious issues with that. It's a lot to check for and I don't know how, even after searching through articles about JS associative arrays. For example, how would I check if a player already exists in the array? And how would I capture that position in the array to, say, update existing "point" value. It's an issue because the array is constantly growing, it's never going to be player1 and player2 again and again. I was thinking perhaps add two seperate arrays to an associative array at the end of the game?

I did post on stackoverflow for help: http://stackoverflow.com/questions/24124769/jquery-and-javascript-to-keep-track-of-two-players-in-one-associative-array

<e/n>Honestly this whole project is kind of lame. He's teaching us as we're supposed to be working on it. I've rewritten so much code it's as if I never make progress. </e/n>

pepito sanchez fucked around with this message at 18:12 on Jun 9, 2014

Strong Sauce
Jul 2, 2003

You know I am not really your father.





Plorkyeran posted:

Did you actually read that article, or did you just laugh at the introduction then close the page? It doesn't actually try to claim that AppCache is unusable; merely that it has a bunch of gotchas you have to keep in mind.

Yeah man, anything with douchebag in the title is great on that alone.

Albeit this article was written ~2 years ago, and I'm guessing poo poo has changed, there's just way too many conditions you have to work around to make it work. The only reason to use it is you literally have no other option if what you want to do is get your website working offline at the time. I skimmed the article, but also read that he suggests its best for SPAs, but not for normal websites which was my viewpoint around that time.

Anyways maybe my original statement was too harsh, but it's definitely not the solution to storing files without a server. Have not tried the iframe trick or whatever so could be wrong.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

edit: Yet I'm having serious issues with that. It's a lot to check for and I don't know how, even after searching through articles about JS associative arrays. For example, how would I check if a player already exists in the array? And how would I capture that position in the array to, say, update existing "point" value. It's an issue because the array is constantly growing, it's never going to be player1 and player2 again and again. I was thinking perhaps add two seperate arrays to an associative array at the end of the game?

What do you mean by associative array? That is a PHP term. Do you mean a "plain" object, or an array (which is possible, but can be confusing, since arrays are also objects, just a special case). Also the syntax can look like array access: someObject['someProperty'] = "a value";

If you have an array, you can use the indexOf function of the array to search for your value.
If you have a non-array object, you have to iterate through the properties and compare the values. You can use jQuery's $.each() for that. You can actually user $.each() for both cases, but you don't need to use it with an array.

But I don't really see why you would need to store your player objects in an array, if there are always just two players.

pepito sanchez
Apr 3, 2004
I'm not mexican
Err just translating from spanish there. "Array asociativo," yeah it's just storing arrays inside an array. It's never just between two players. An existing player can use his name again, for example, and at the end of the game his existing score would be updated. That's why I need every player to be part of one array, to search all names through one array, etc. Otherwise, player1 could simply use the same name in player2's submit field, and the game would have no way of knowing that player's name already exists in player1's array.

edit: The end of both game and project is making an updated list of players based on their points, so it's just ideal to have everything as part of one array. This list has an indefinite amount of players.

Stackoverflow suggests the .grep JQuery function, which I had no idea existed. Trying to wrap my head around it now.

pepito sanchez fucked around with this message at 02:30 on Jun 10, 2014

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
So I'm making a javascript based game and I want to use key combinations ctrl-1, ctrl-2, etc. This appears to work fine in chrome on a mac, but the windows chrome apparently uses these to select between tabs. Is there anyway to prevent that?

down with slavery
Dec 23, 2013
STOP QUOTING MY POSTS SO PEOPLE THAT AREN'T IDIOTS DON'T HAVE TO READ MY FUCKING TERRIBLE OPINIONS THANKS

HappyHippo posted:

So I'm making a javascript based game and I want to use key combinations ctrl-1, ctrl-2, etc. This appears to work fine in chrome on a mac, but the windows chrome apparently uses these to select between tabs. Is there anyway to prevent that?

Tried using http://api.jquery.com/event.preventdefault/

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

HappyHippo posted:

So I'm making a javascript based game and I want to use key combinations ctrl-1, ctrl-2, etc. This appears to work fine in chrome on a mac, but the windows chrome apparently uses these to select between tabs. Is there anyway to prevent that?

How are you capturing it? You should get first crack at it in keydown, with evt.ctrlKey set.

You also need to cancel the default processing of the event; returning false from your keydown handler works.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Subjunctive posted:

How are you capturing it? You should get first crack at it in keydown, with evt.ctrlKey set.

You also need to cancel the default processing of the event; returning false from your keydown handler works.

This did it, thanks

Roger Troutman
Jan 11, 2007


What I do is sometimes get a tin of soup, heat it up, poach an egg in it, serve that with a pork pie sausage roll.
Major noob here. I'm sure that I'm making many mistakes but this one has me pretty confused.

I've got most of the behavior of this page functioning as I think it should but two of the monster "types", those being electric and fire are not showing the nextLevel value. I don't really understand why not?

http://jsfiddle.net/orenthal/3HsSs/

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Citizen Jerk posted:

Major noob here. I'm sure that I'm making many mistakes but this one has me pretty confused.

I've got most of the behavior of this page functioning as I think it should but two of the monster "types", those being electric and fire are not showing the nextLevel value. I don't really understand why not?

http://jsfiddle.net/orenthal/3HsSs/

Those two monster types progress bars are too narrow, because their baseXP is lower. The "100" gets pushed to the next row (and thus hidden).

Roger Troutman
Jan 11, 2007


What I do is sometimes get a tin of soup, heat it up, poach an egg in it, serve that with a pork pie sausage roll.
I didn't even consider the width of the active area of the progress bar as I was ignorantly looking at the bar as a whole.

Thanks a million.

pepito sanchez
Apr 3, 2004
I'm not mexican
So everything was going great. Simon was working, my sequences were showing, and players were playing. Would you believe I never actually played Simon? I was delivering the full sequence to both players before it's their turn, and that's just not how the game is played. I've had to recode a bunch of stuff so that the sequences are shown a little bit at a time. With each "round," the players each go into their turn. That's not so much of a problem. My problem is simply showing the sequences a bit at a time. I was using a for loop, until I remembered for doesn't really work. Now I'm stuck... again.

I'm trying to replace it using $.each, and I've read up on the JQuery API for it, but I'm still not sure how to implement it in this function.

code:
function playButtonSequence(sequence) {
    /*gameMode.sequence (sequence) contains the full randomly generated sequence. 
     *It's slowly cut off with .shift(). When it reaches zero, it ends the game.
     */
    if (sequence.length < 1) {
        endGame();
    }
    /*The idea is if gameMode.sequence still has elements, it continues to cycle
     *through this function (which is called at the end of player 2's turn).
     */
    if (sequence.length >= 1)
        for (i = 0; i < sequenceCounter; i++) {
    /*sequenceCounter is a global var starting with a value of 1. It's meant to keep
     *track of which round we're on.
     *storedSequence is a .slice() of gameMode.sequence, since it's being .shift()ed.
     */
            $("#c" + storedSequence[i] + "i").attr("src", "imgs/" + storedSequence[i] + ".png");
            $("#snd" + storedSequence[i])[0].play();

            setTimeout(function() {
                $("#c" + storedSequence[i] + "i").attr("src", "imgs/d" + storedSequence[i] + ".png");
            }, gameMode.speed);
        }
    //Move onto player 1's turn after variables have been modified
    sequence = sequence.shift();
    sequenceCounter++;
    player1Turn();
}
Before I had to rewrite this, it was showing full sequences just fine. Right now it enters the function, but doesn't go into the for loop. How would I implement $.each in this instance?

mobby_6kl
Aug 9, 2009

by Fluffdaddy
I'm dicking around with WebGL and thanks to some OpenGL experience with C++ I figured most stuff out, but ran into an issue with textures that seems pretty simple. Basically, I can't get multiple textures (one for each of a handful of objects, not bumpmaps or anything) to work - one is ok, but the moment I try to stick it into an array, everything stops working:

JavaScript code:
for (i = 0; i<5; i++) {
	textures[i] = {};
	textures[i]= gl.createTexture();
	textures[i].image = new Image();
	textures[i].image.onload = function () {setup_texture(textures[i])};
	textures[i].image.src = images[i];
	
	if( !gl.isTexture(textures[i]) ) { // fixed
		alert(images[i] + " wasn't loaded!?");
	}
}
This magically works if I use a simple variable instead of the textures array. Is this some JS voodoo or am I missing sometihng else entirely?

mobby_6kl fucked around with this message at 17:40 on Jun 13, 2014

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
You're assigning to textures[i] rather than textures[i].texture. Also, use {}, not new Object().

mobby_6kl
Aug 9, 2009

by Fluffdaddy
Oops, that was a copy-paste error as I was trying out different things. Updated to what I actually have now (also including your {} suggestion). Same result, the alert gets fired anyway.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

mobby_6kl posted:

Oops, that was a copy-paste error as I was trying out different things. Updated to what I actually have now (also including your {} suggestion). Same result, the alert gets fired anyway.

You don't give enough context. Show the "simple variable" version, what does setup_texture do?

Also, you immediately overwrite the empty object in textures[i] with gl.createTexture();

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

code:
function playButtonSequence(sequence) {
    /*gameMode.sequence (sequence) contains the full randomly generated sequence. 
     *It's slowly cut off with .shift(). When it reaches zero, it ends the game.
     */
    if (sequence.length < 1) {
        endGame();
    }
    /*The idea is if gameMode.sequence still has elements, it continues to cycle
     *through this function (which is called at the end of player 2's turn).
     */
    if (sequence.length >= 1)
        for (i = 0; i < sequenceCounter; i++) {
    /*sequenceCounter is a global var starting with a value of 1. It's meant to keep
     *track of which round we're on.
     *storedSequence is a .slice() of gameMode.sequence, since it's being .shift()ed.
     */
            $("#c" + storedSequence[i] + "i").attr("src", "imgs/" + storedSequence[i] + ".png");
            $("#snd" + storedSequence[i])[0].play();

            setTimeout(function() {
                $("#c" + storedSequence[i] + "i").attr("src", "imgs/d" + storedSequence[i] + ".png");
            }, gameMode.speed);
        }
    //Move onto player 1's turn after variables have been modified
    sequence = sequence.shift();
    sequenceCounter++;
    player1Turn();
}
Before I had to rewrite this, it was showing full sequences just fine. Right now it enters the function, but doesn't go into the for loop. How would I implement $.each in this instance?

Well, I don't know how Simon is played either, but I'm going to help you with the for loop:
JavaScript code:
var entryDisplayer = function (indexInArray, value) {
	$("#c" + value + "i").attr("src", "imgs/" + value + ".png");
	$("#snd" + value)[0].play();

	setTimeout(function() {
		$("#c" + value + "i").attr("src", "imgs/d" + value + ".png");
	}, gameMode.speed);
}

$.each(storedSequence, entryDisplayer);

mobby_6kl
Aug 9, 2009

by Fluffdaddy

Wheany posted:

You don't give enough context. Show the "simple variable" version, what does setup_texture do?

Also, you immediately overwrite the empty object in textures[i] with gl.createTexture();

Sure, this thing here works when placed instead of the previous code :
JavaScript code:
mytexture = gl.createTexture();
mytexture.image = new Image();
mytexture.image.onload = function () { setup_texture(mytexture) }
mytexture.image.src = images[0];
Setup_texture is mostly recycled from some sample code to deal with WebGL bureaucracy:
JavaScript code:
function setup_texture (texture) {
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
		
    /* For non-POT sizes */
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
		
    gl.bindTexture(gl.TEXTURE_2D, null);
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Okay, the problem is probably using setup_texture(textures[i]). The i variable is 5 at the end of that loop and textures[5] presumably is undefined.

edit: Also gl.isTexture(textures[i]) is always false because the texture is setup asynchronously and it has not finished when you get to that point in the loop. (I assume)

Wheany fucked around with this message at 19:46 on Jun 13, 2014

pepito sanchez
Apr 3, 2004
I'm not mexican
edit:

Wheany, your function shows every button at once. At least it's showing something again! The delay timer gets completely ignored, just like in a for loop, and it's not showing only the first button before moving on, which would be what I was trying to change in the first place.

pepito sanchez fucked around with this message at 04:45 on Jun 14, 2014

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

edit:

Wheany, your function shows every button at once. At least it's showing something again! The delay timer gets completely ignored, just like in a for loop, and it's not showing only the first button before moving on, which would be what I was trying to change in the first place.

Well if entryDisplayer works right with a single entry, maybe you can still use that to display a single entry.

The delay timer is not ignored (I assume you mean setTimeout), that function just returns immediately, because it is asynchronous. Asynchronicity is the nature of javascript. You call a function that returns immediately, but you give that function a callback as a parameter. Once the function finishes doing what it is doing (For example: Once it is done waiting for 1 second), it calls the callback.

Here is that piece of code from my last post edited again to assign one more function into variables:

JavaScript code:
var entryDisplayer = function (indexInArray, value) {
	$("#c" + value + "i").attr("src", "imgs/" + value + ".png");
	$("#snd" + value)[0].play();
	
	var buttonReverter = function() {
		$("#c" + value + "i").attr("src", "imgs/d" + value + ".png");
	};
	
	setTimeout(buttonReverter, gameMode.speed);
}

$.each(storedSequence, entryDisplayer);
Both entryDisplayer and buttonReverter are callbacks. $.each calls the callback immediately for each entry in a collection, while setTimeout returns immediately and then calls the callback after gameMode.speed has elapsed.

One common example is ajax calls, where you setup the connection and then have it connect. After the server returns a result (which can take a long time), your callback gets called with the result and you can process it.

pepito sanchez
Apr 3, 2004
I'm not mexican
The problem is it reacts just like a for loop, so everything in the sequence shows at once. I got it to do what I wanted using a while inside an if.

code:
function playButtonSequence(sequence) {
    if (sequence.length < 1) {
        endGame();
    }
    
    if (sequence.length >= 1) {
        var i = 0; 
        while( i < sequenceCounter ) {
            $("#c" + storedSequence[i] + "i").attr("src", "imgs/" + storedSequence[i] + ".png");
            $("#snd" + storedSequence[i])[0].play();

            setTimeout(function() {
                $("#c" + storedSequence[i] + "i").attr("src", "imgs/d" + storedSequence[i] + ".png");
            }, gameMode.speed);
            i++;
        }
    }

    sequence = sequence.shift();
    sequenceCounter++;
    player1Turn();
}
Now the problem is when it gets into player1Turn() I make it push() numbers into player1's array, and mean it to go into player2Turn() once that array reaches sequenceCounter (so we know his turn is complete), yet it never compares it. It just stays stuck in player1Turn.

code:
function player1Turn() {
    alert("Es turno del jugador uno!");
    player1.active = true;
    if (!player1.active && player1.sequence.length === sequenceCounter - 1) {
        player2.active = true;
        player2Turn();
    }
    if (player1.active) {
        $("#c1i, #c2i, #c3i, #c4i").unbind();
        $("#c1i").on("click", function() {
            redButton();
            player1.sequence.push(1);
            player1.active = false;
        });
        $("#c2i").on("click", function() {
            yellowButton();
            player1.sequence.push(2);
            player1.active = false;
        });
        $("#c3i").on("click", function() {
            blueButton();
            player1.sequence.push(3);
            player1.active = false;
        });
        $("#c4i").on("click", function() {
            greenButton();
            player1.sequence.push(4);
            player1.active = false;
        });
    }
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

The problem is it reacts just like a for loop, so everything in the sequence shows at once. I got it to do what I wanted using a while inside an if.

I would change that playButtonSequence so that it simply shift()s one entry from the sequence, then feeds that entry to entryDisplayer.

You probably have some timeout somewhere that calls playButtonSequence.

But if it works, it works.

pepito sanchez posted:

Now the problem is when it gets into player1Turn() I make it push() numbers into player1's array, and mean it to go into player2Turn() once that array reaches sequenceCounter (so we know his turn is complete), yet it never compares it. It just stays stuck in player1Turn.

JavaScript code:
    player1.active = true;
    if (!player1.active 

Do you see it?

pepito sanchez
Apr 3, 2004
I'm not mexican
No, I don't. Each click action is supposed to set it to false, but the && is what's important, if the player1.sequence meets sequenceCounter. Then it should ALL be true, and move onto player2Turn()

Am I missing something obvious?

Fish Ladder Theory
Jun 7, 2005

pepito sanchez posted:

No, I don't. Each click action is supposed to set it to false, but the && is what's important, if the player1.sequence meets sequenceCounter. Then it should ALL be true, and move onto player2Turn()

Am I missing something obvious?

code:
 player1.active = true;
    if (!player1.active && player1.sequence.length === sequenceCounter - 1) {
As soon as the interpreter sees that player1.active is true, the rest of that if statement is ignored.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Fish Ladder Theory posted:

code:
 player1.active = true;
    if (!player1.active && player1.sequence.length === sequenceCounter - 1) {
As soon as the interpreter sees that player1.active is true, the rest of that if statement is ignored.

And even it it wasn't, the logical and would still evaluate to false as a whole and the code inside the if block would be skipped.

pepito sanchez
Apr 3, 2004
I'm not mexican
On my way...

Why does the NetBeans IDE turn "sequence" into a dark color instead of its normal green when I do this? It's scaring me.

code:
function checkTurn1() {
    if (player1.sequence.length === gameMode.sequence.length) {
        count = 0;
        sequenceCounter++;
        player1.sequence.length = 0; // it doesn't like this statement, why?
        playButtonSequence();
    }
    if (player1.sequence.length === sequenceCounter) {
        sequenceCounter++;
        player1.active = false;
        playButtonSequence();
    }
}
I need the function to reset the array, and found on Stackoverflow this is the easiest way. It only turns "sequence" in player1.sequence.length to dark, without showing me any indications as to why, when I do this for either player1.sequence.length or player2.sequence.length.

It it actually turning it into an integer?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

pepito sanchez posted:

Why does the NetBeans IDE turn "sequence" into a dark color instead of its normal green when I do this? It's scaring me.

That is entirely dependent on your IDE's syntax highlight settings, and not really related to JavaScript.

It could be that it doesn't know what player1.sequence is, it could be warning you that player1.sequence might not have been initialized, or it could be something completely different.

Pollyanna
Mar 5, 2005

Milk's on them.


Is there a way to make Javascript pause and wait for input, in a fashion that doesn't use alert() or confirm()?

I have code that runs through a set of instructions, but one of those instructions involves "waiting for user input and accepting a key press". My initial implementation was to start an infinite (or not infinite) while loop that would run as long as a certain variable was set to false. I was figuring that on a keydown event, that variable would be set to true, at which point that while loop would break and the code could continue. I tried implementing this, but it just hangs and never responds to anything I do :( Is there something particular about Javascript making something like this difficult?

Gul Banana
Nov 28, 2003

yes; javascript is single-threaded and can't block. you need to instead use a callback when the user input event occurs.

you can call setTimeout() to invoke some piece of code after a delay, and then repeat until there's input, but this would be pretty inconvenient compared to an onTextChanged event or whatever

spiritual bypass
Feb 19, 2008

Grimey Drawer
Maybe this library would make things simpler?

http://craig.is/killing/mice

Fish Ladder Theory
Jun 7, 2005

Pollyanna posted:

Is there a way to make Javascript pause and wait for input, in a fashion that doesn't use alert() or confirm()?

I have code that runs through a set of instructions, but one of those instructions involves "waiting for user input and accepting a key press". My initial implementation was to start an infinite (or not infinite) while loop that would run as long as a certain variable was set to false. I was figuring that on a keydown event, that variable would be set to true, at which point that while loop would break and the code could continue. I tried implementing this, but it just hangs and never responds to anything I do :( Is there something particular about Javascript making something like this difficult?

User input is async, right? So anything that depends on user input has to happen in a callback invoked after that input has been received.

Here's a basic callback example.
http://jsfiddle.net/LKs23/2

You could also look at promises using jQuery or Q if the behavior needs to be complex.

Fish Ladder Theory fucked around with this message at 19:41 on Jun 20, 2014

mobby_6kl
Aug 9, 2009

by Fluffdaddy

Wheany posted:

Okay, the problem is probably using setup_texture(textures[i]). The i variable is 5 at the end of that loop and textures[5] presumably is undefined.

edit: Also gl.isTexture(textures[i]) is always false because the texture is setup asynchronously and it has not finished when you get to that point in the loop. (I assume)

Thanks! I'm probably mildly retarded because I never figured out where exactly the issue was, but I had to refactor everything anyway so I got rid of this thing altogether. Also totally right about isTexture, I realized that when I unrolled the loop and saw what was actually happening.

I suppose I could ask this in the web design or 3d graphics threads, but what's generally the best way to overlay some 2d graphics over a WebGL scene? I'd need to have some responsive diagnostics and a basic UI that I'd prefer to draw myself.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

mobby_6kl posted:

I suppose I could ask this in the web design or 3d graphics threads, but what's generally the best way to overlay some 2d graphics over a WebGL scene? I'd need to have some responsive diagnostics and a basic UI that I'd prefer to draw myself.

Simplest is probably to position a <div> or 2D <canvas> over top of the WebGL scene's <canvas>. You can also use a 2D <canvas> as a texture if you want more control. Here's a simple example.

DholmbladRU
May 4, 2006
I am using the InfoBox plugin to create custom interactive info-windows in google maps. From these infowindows users can scroll through a list and click on an image to invoke some script. Everything works, the issue is when enableEventPropagation is enabled I can click through the infobox to other markers which causes another infobox to pop up. When it is set to false I cannot use actions within the infobox(scroll, click)

does anyone have experience solving this problem?

code:
InfoWindow = new InfoBox({
                            content: document.getElementById("infobox-wrapper"),
                            disableAutoPan: true,
                            enableEventPropagation: true,
                            zIndex: null,
                            pixelOffset: new google.maps.Size(-300, -0),
                            closeBoxURL: '../assets/images/info-close.png',
                            boxStyle: {
                                color: 'white',
                                background: '#101010',
                                width: "600px",
                                height: "400px"
                            },
                            infoBoxClearance: new google.maps.Size(1, 1)
                        });
Removing infobox
code:
function removeInfoWindow() {
    $('.infoBox').remove()
}
Javascript invoked when image within infobox is clicked, only works when enableEventPropagation is set to true

code:
$(document.body).on('touchstart', '.infoBox img', function () {
    if ($(this).attr('val') == 'off') {
        //turn on
        $(this).attr('val', 'on');
        $(this).attr('src', '../assets/images/check.png');
    } else {
        //turn off
        $(this).attr('val', 'off');
        $(this).attr('src', '../assets/images/uncheck.png');
    }
});

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic
I wrote a simple form input that does some calculations. Since I'm not writing to a database, do I need to sanitize my inputs?

Basically the page takes in two integers, divides them and returns the value.

Adbot
ADBOT LOVES YOU

pram
Jun 10, 2001
I'm messing around with AngularJS. I like it a lot so far, but I'm having a hard time figuring out how to make some pagination links. I haven't been able to find any good examples on stackoverflow etc :shepface:

My json schema already has pagination, it includes these items:

code:
    "per_page": 5,
    "pages": 36,
    "current_page": 1,
So I can get the total number of pages and the last page from "pages" and the current page obviously. I thought about doing an ng-repeat but it doesn't seem like you can just iterate over things that aren't actually in the scope. It this a job for a filter, or something else?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply