Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2011, 10:33 PM   PM User | #1
swydell
New Coder

 
Join Date: Nov 2011
Location: I live in Hollywood, CA
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
swydell is an unknown quantity at this point
Question animating a gif

I'm trying to create this animated gif array. Everything is in place, almost exactly like another one I did successfully. But I must be overlooking something here. If anyone can take a look at the code and give me suggestions I would appreciate it. Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>puppies</title>
<script type="text/javascript">
var rusty=new Array(7);
rusty[0]="puppies/puppy.gif";
rusty[1]="puppies/puppy0.gif";
rusty[2]="puppies/puppy1.gif";
rusty[3]="puppies/puppy2.gif";
rusty[4]="puppies/puppy3.gif";
rusty[5]="puppies/puppy4.gif";
rusty[6]="puppies/puppy5.gif";
var myPup=[0];

function getPup(){

if(myPup<rusty.length){
myPup = 0;
}
else{
document.getElementById("pup").src="puppies/"+ rusty[myPup];
myPup = myPup+1;

}



}
setTimeout("getPup()", 1000)
</script>
</head>

<body>
<h1>Puppies</h1>
<img src="puppies/puppy0.gif" id="pup" width="97" height="60" alt="puppy" onclick="getPup()"/>


</body>
</html>



Last edited by swydell; 11-09-2011 at 10:37 PM.. Reason: I didn't see the code I had inserted
swydell is offline   Reply With Quote
Old 11-09-2011, 11:14 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
What is this line supposed to do?
Code:
var myPup=[0];
With this line you are creating an array with just one element. So myPup[0]==0. But later you want to use myPup as an integer. So I think you should change the line to
Code:
var myPup=0;
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
swydell (11-10-2011)
Old 11-10-2011, 12:46 AM   PM User | #3
swydell
New Coder

 
Join Date: Nov 2011
Location: I live in Hollywood, CA
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
swydell is an unknown quantity at this point
I made the change. You can see it here. Thanks. Yet, 'm still not getting the animation. Each image is a different pose of the puppy to make it appear running. I think my logic is wrong. Any suggestions.?

var myPup=0;

function getPup(){

if(myPup<rusty.length){
myPup = 0;
}
else{
document.getElementById("pup").src="puppies/"+ rusty[myPup];
myPup = myPup+1;

}



}
setTimeout("getPup()", 1000)
</script>
</head>

<body>
<h1>Puppies</h1>
<img src="puppies/puppy0.gif" id="pup" width="97" height="60" alt="puppy" onclick="getPup()"/>
swydell is offline   Reply With Quote
Old 11-10-2011, 07:57 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Yes, and you would have also seen it even without a decent debugger tool

Just step through your code with pencil and paper and note variable values.

You'll see the following
1. myPup is set to 0
2. a timer is started that will call getPup() after one second
3. in getPup() you check if myPup is less than the length of the array (which is true of course as 0<7). In that case you set myPup to 0 again

This is where the problem is. I think you only need to change myPup<rusty.length to myPup==rusty.length

In that case
3. in getPup() you check if myPup is equal to the length of the array (which is false of course as 0 is not equal to 7)
4. in the else branch you have code to show rusty[0] and increase myPup by 1

Second problem here: There is no timer that will call getPup() again. setTimeout will only work once.

So EITHER you change setTimeout to setInterval OR you call setTimeout again at the end of the function getPup()
devnull69 is offline   Reply With Quote
Old 11-16-2011, 08:31 PM   PM User | #5
clausrei
New Coder

 
Join Date: Sep 2011
Location: United Kingdom
Posts: 72
Thanks: 2
Thanked 3 Times in 3 Posts
clausrei is an unknown quantity at this point
GIF or JS animation !???

Don't quite understand, why you try to make an animated GIF with a Java Script? Animation is a fetcher of the GIF picture format.

Have a look at this:
http://www.youtube.com/watch?v=f-9_HwRcd1A
clausrei is offline   Reply With Quote
Old 11-17-2011, 01:31 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Why JavaScript?

As long as you are using GIF images, you could simply create an animated GIF using a graphical tool, such as Photoshop or even GIMP. GIMP is a freeware graphical tool, so that you have nothing to pay for.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 12-07-2011, 09:42 AM   PM User | #7
asok_coding
New to the CF scene

 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
asok_coding is an unknown quantity at this point
Quote:
Originally Posted by swydell View Post
I'm trying to create this animated gif array. Everything is in place, almost exactly like another one I did successfully. But I must be overlooking something here. If anyone can take a look at the code and give me suggestions I would appreciate it. Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>puppies</title>
<script type="text/javascript">
var rusty=new Array(7);
rusty[0]="puppies/puppy.gif";
rusty[1]="puppies/puppy0.gif";
rusty[2]="puppies/puppy1.gif";
rusty[3]="puppies/puppy2.gif";
rusty[4]="puppies/puppy3.gif";
rusty[5]="puppies/puppy4.gif";
rusty[6]="puppies/puppy5.gif";
var myPup=[0];

function getPup(){

if(myPup<rusty.length){
myPup = 0;
}
else{
document.getElementById("pup").src="puppies/"+ rusty[myPup];
myPup = myPup+1;

}



}
setTimeout("getPup()", 1000)
</script>
</head>

<body>
<h1>Puppies</h1>
<img src="puppies/puppy0.gif" id="pup" width="97" height="60" alt="puppy" onclick="getPup()"/>


</body>
</html>


Hi i am new to this forum. i want to tell u some changes in code as shown below:

var mayPup;
function getPup(){

if(myPup<rusty.length){
document.getElementById("pup").src="puppies/"+ rusty[myPup];
myPup = myPup+1;

if(myPup==rusty.length)
myPup = 0;

}

}
asok_coding is offline   Reply With Quote
Reply

Bookmarks

Tags
array, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.