Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    Global variable not working with a method

    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.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, this works:
    Code:
    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>
    Last edited by liorean; 04-07-2003 at 09:07 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This what you're needing?
    Code:
    var hex;
    for (i=0; i<16; i++)
    {
        hex = i.toString(16);
        window['p' + hex] = new Image();
        window['p' + hex].src = hex + ".gif"
    }
    Edit: Nice liorean, I forgot about those hex literals, or whatever they are called. Just another type of Number literal I guess.
    Last edited by beetle; 04-07-2003 at 08:49 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    New Coder
    Join Date
    Mar 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm sorry to say your code is confusing to me. Try the following instead.

    Code:
    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.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •