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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    105
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Need help with this script for animating pic new to forum!!!!!

    Hi Im new here and just recently getting into coding and I can't seem to figure what my problem is. I am using this to simulate a picture fading into another. After it finishes one cycle of the imgList it doesnt renew the first picture, it shows it as an invalid link. Any ideas?

    Caleb

    <script type="text/javascript">
    //animate header

    //use array to make list of images
    var imgList=new Array(
    "Live Graphics/header1A.gif",
    "Live Graphics/header1A.gif",
    "Live Graphics/header1A.gif",
    "Live Graphics/header1A.gif",
    "Live Graphics/header1A.gif",
    "Live Graphics/header1b.gif",
    "Live Graphics/header1c.gif",
    "Live Graphics/header1d.gif",
    "Live Graphics/header1e.gif",
    "Live Graphics/header1f.gif",
    "Live Graphics/header1g.gif"
    );

    var frame=1;
    var spriteImage

    function init(){
    setInterval("animate()",200);
    spriteImage=document.getElementById("image");
    };//end init

    function animate(){
    frame++ ;
    if (frame>imgList.length){
    frame=1;
    }//end if
    spriteImage.src=imgList[frame];
    }
    </script>

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try to change to this:
    Code:
    function animate(){
    	frame++	;
    	if (frame >= imgList.length){
    		frame=0; 
    	}//end if
    	spriteImage.src=imgList[frame];
    }
    when frame is equal with imgList.length point out of the array.

    best regards

  • Users who have thanked oesxyl for this post:

    calebandchels (11-05-2009)

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Location
    Florida United States
    Posts
    105
    Thanks
    6
    Thanked 2 Times in 2 Posts
    It worked perfect but just to clarify for the future, because the function pointed directly to the array that was the problem?

    Thank you
    Caleb

    Quote Originally Posted by oesxyl View Post
    try to change to this:
    Code:
    function animate(){
    	frame++	;
    	if (frame >= imgList.length){
    		frame=0; 
    	}//end if
    	spriteImage.src=imgList[frame];
    }
    when frame is equal with imgList.length point out of the array.

    best regards

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by calebandchels View Post
    It worked perfect but just to clarify for the future, because the function pointed directly to the array that was the problem?

    Thank you
    Caleb
    no. The array have length elements and start from 0, that means last element will have index length-1, the index length is out of range.
    I try a poor explanation of this in my previous post.,

    best regards


  •  

    Posting Permissions

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