...

View Full Version : Global variable not working with a method



Tails
04-07-2003, 08: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++)
{
window['p'+i.toString(16)=new Image()
window['p'+i.toString(16).src=i+".gif"
alert('p'+i.toString(16).src)
}

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.

liorean
04-07-2003, 08:46 PM
Well, this works:

var
i=0;
while(0x10>i)
alert('p'+(i++).toString(0x10));

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.


<edit>
Found 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.
</edit>

beetle
04-07-2003, 08:47 PM
This what you're needing?
var hex;
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.

eggman
04-07-2003, 08:55 PM
I'm sorry to say your code is confusing to me. Try the following instead.



var myImages=[]
for (var i=0;i<16;i++)
{
myImages[i]=new Image()
myImages[i].src=i.toString(16)+".gif"
alert(myImages[i].src)
}


Change 'myImages' to any variable name you want.

Tails
04-09-2003, 08: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.

liorean
04-09-2003, 08:36 PM
0x10==16==020

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.

Tails
04-12-2003, 08: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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum