Here is what I 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 will go on, but I think you get the gist. What's wrong with my code? The images won't change as my "house_bought" variable changes.
In my HTML part, I simply put this:
<div id="image_centrale">
<img src="img/image_centrale/1_house_0.jpg" name="main" />
</div>
Note: This is an upcoming full html/js game website.
Why do you use src
and innerHTML
at the same time? src
is all you need, and there is no need for eval
as 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 this code example, I hard-coded the values for the variables just to make it a working example, so you will always see the first image. Change it to suite your needs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.