javascript - Why isn't this image changing function working? -
here wrote:
var img_current = 0; var image0 = new image() image0.src = "img/image_centrale/1_house_0.jpg" var image1 = new image() image1.src = "img/image_centrale/1_house_1.jpg" var image2 = new image() image2.src = "img/image_centrale/1_house_2.jpg" function affich_centrale() { if (house_bought == 1 && neighborhood_bought == 0) { img_current = 1; document.images.main.src = eval("image" + img_current + ".src").innerhtml = 'image_centrale'; } else if (house_bought == 2 && neighborhood_bought == 0) { img_current = 2; document.images.main.src = eval("image" + img_current + ".src").innerhtml = 'image_centrale'; } else{}
this go on, think gist. what's wrong code? images won't change "house_bought" variable changes.
in html part, put this:
<div id="image_centrale"> <img src="img/image_centrale/1_house_0.jpg" name="main" /> </div>
note: upcoming full html/js game website.
why use src
, innerhtml
@ same time? src
need, , there no need eval
well. try this:
<div id="image_centrale"> <img src="" name="main" /> </div> <script type="text/javascript"> var img_current = 0; var house_bought = 1; var neighborhood_bought = 0; var image0 = new image() image0.src = "img/image_centrale/1_house_0.jpg" var image1 = new image() image1.src = "img/image_centrale/1_house_1.jpg" var image2 = new image() image2.src = "img/image_centrale/1_house_2.jpg" function affich_centrale() { if (house_bought == 1 && neighborhood_bought == 0) { img_current = 1; document.images.main.src = image0.src; } else if (house_bought == 2 && neighborhood_bought == 0) { img_current = 2; document.images.main.src = image1.src; } else { } } affich_centrale(); </script>
in code example, hard-coded values variables make working example, see first image. change suite needs.
Comments
Post a Comment