Eep² Posted March 26, 2016 Share Posted March 26, 2016 I'm trying to make a script that will change an image several times after the image is clicked. So, if the image is clicked once, the image changes, if clicked again, the image changes again, etc. I'm trying to use a variable that increases in amount but it doesn't seem to be working for some reason. Any ideas? Thanks. function changepic(img_name,img_src) {document[img_name].src=img_src;} function changepic1() { var o = 0; if (!o) { changepic('circles','circular_infinity1.png'); o++; } else if (o==1) { changepic('circles','circular_infinity_intersected1a.png'); o++; } else {changepic('circles','circular_infinity1a.png')} o = !o } Link to comment Share on other sites More sharing options...
davej Posted March 26, 2016 Share Posted March 26, 2016 Why are you using a boolean operator on a number? Why would you expect a dynamic local variable to not be zero when you set it to zero? Link to comment Share on other sites More sharing options...
Eep² Posted March 26, 2016 Author Share Posted March 26, 2016 This is how I script in Second Life and it works fine, except for this change: function changepic(img_name,img_src) {document[img_name].src=img_src;} function changepic1() { var o = 0; if (!o) { changepic('circles','circular_infinity1.png'); o++; } else if (o==1) { changepic('circles','circular_infinity_intersected1a.png'); o++; } else { changepic('circles','circular_infinity1a.png'); o = 0; } } But it still doesn't work past the first if. The boolean operator (==) is used to make sure o does indeed equal a specific #. Regardless, just using "=" doesn't work either. A dynamic local variable can or can't be zero; hence why I test for it in the beginning to begin the if-elses. Got any actual answers instead of questions? Link to comment Share on other sites More sharing options...
davej Posted March 26, 2016 Share Posted March 26, 2016 o will always be zero, because you just set it to zero, and zero is a falsy value, so !o will always yield a truthy value. Declare and initialize o outside the function, and don't use ! on a number. Link to comment Share on other sites More sharing options...
Eep² Posted March 27, 2016 Author Share Posted March 27, 2016 Thanks! Declaring the variable outside the function fixes it. "!o" works as expected too. Link to comment Share on other sites More sharing options...
davej Posted March 27, 2016 Share Posted March 27, 2016 Why not just say o == 0 so that it is clear that o merely sequences 0,1, 2 ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now