View Full Version : Global variable not working with a method
04-07-2003, 09:28 PM
I have several images named in the fashion 20.gif, 21, 29, all the way up to 2f. For my preloading script, I made this:
for (i=0; i<16; i++)
But the toString(16) is not working. It eventually goes up to 210.gif and not as hex. What has gone wrong?
If I mistyped all of this, I'll have the real thing copy-and-paste, but it isn't on hand. Still, the problem should be reproducable or a solution within grasp.
04-07-2003, 09:46 PM
Well, this works:
You forgot to close your square brackets there, but I hardly things that's your in-context situation or you would have found it easily. window['p'+i.toString(16)]
I can't think of the reason why it wouldn't work as you use it.
You're using i+".gif" when you should use i.toString(16).
Doing it the way beetle wrote it should be more efficient, though.
04-07-2003, 09:47 PM
This what you're needing?
for (i=0; i<16; i++)
hex = i.toString(16);
window['p' + hex] = new Image();
window['p' + hex].src = hex + ".gif"
Nice liorean, I forgot about those hex literals, or whatever they are called. Just another type of Number literal I guess.
04-07-2003, 09:55 PM
I'm sorry to say your code is confusing to me. Try the following instead.
for (var i=0;i<16;i++)
Change 'myImages' to any variable name you want.
04-09-2003, 09:00 PM
Hmm, I think I tried that (remember I tried putting this script up from memory with no way to test it for mistakes), but I never thought of making a for loop with hex numbers. Will having var hex at the top of the script make all the difference? Isn't it just being defined twice? For whatever reason, I didn't try it before, and I don't know if it makes any difference, but does it? I'll have the real (not)working script posted in a little while for you to see.
04-09-2003, 09:36 PM
No, it doesn't make any difference. I just thought that if you use hex literals, you can see where the loop will end in hex, so that makes it a little easier to read when you have a look at it.
The only problem you have with your approach is that you use 'p'+i+'.gif' instead of 'p'+i.toString(16)+'.gif'. Beetle's code is doing what you try to do in a much less wasteful way, though, by storing the hex value in a vriable that you use multiple times.
04-12-2003, 09:18 PM
The i being there instead of the hex variable was the problem. It is fixed now. But the preloading doesn't work. Can someone tell me if it is working for them? I'm probably on a system that doesn't allow cache and cookies. http://XFox_Prower.tripod.com/games/js/slide/miles
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.