...

View Full Version : Need help with this script for animating pic new to forum!!!!!



calebandchels
11-05-2009, 06:12 PM
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>

oesxyl
11-05-2009, 06:17 PM
try to change to this:


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

calebandchels
11-05-2009, 06:23 PM
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


try to change to this:


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

oesxyl
11-05-2009, 06:26 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum