squirellplaying
05-30-2004, 03:21 PM
I'm writing a program where when the user clicks the button it starts the race. Right now I'm just trying to get the images to move. The problem is I can't get it to work with passing variables in. When I learned it I used document.getElementById('id').style.left = leftLocation. But that was for one image. I'm trying to make one function to move 3 images so I passed in the variables that had the id's of the 3 images. The problem is, h only increases to 20 and stops, or 10 keeps increasing by 10 to 20 so I only see 20. Also my images don't move.
I need to make the three images move across the screen.
<html>
<head>
<style>
#hImg1{
width:100px;
height:67.4px;
position:absolute;
top:10px;
}
#hImg2{
width:100px;
height:67.4px;
position:absolute;
top:77.4px;
}
#hImg3{
width:100px;
height:67.4px;
position:absolute;
top:144.8px;
}
#startButton{
position: relative;
top:150px;
}
</style>
<script>
function startRace(){
raceTimer1 = setInterval("moveHorse(document.getElementById('hImg1'))", 1000)
raceTimer2 = setInterval("moveHorse(document.getElementById('hImg2'))", 1000)
raceTimer3 = setInterval("moveHorse(document.getElementById('hImg3'))", 1000)
}
function moveHorse(h){
a=h.style.left
b=parseInt(a)
b = b + 10
h = b
}
</script>
</head>
<body>
<img src="horse1.png" style="left:10px;" id="hImg1" /><br />
<img src="horse2.png" style="left:10px;" id="hImg2" /><br />
<img src="horse3.png" style="left:10px;" id="hImg3" /><br />
<input type="button" id="startButton" value="Start the Race!" onclick="startRace()" />
</body>
</html>
I need to make the three images move across the screen.
<html>
<head>
<style>
#hImg1{
width:100px;
height:67.4px;
position:absolute;
top:10px;
}
#hImg2{
width:100px;
height:67.4px;
position:absolute;
top:77.4px;
}
#hImg3{
width:100px;
height:67.4px;
position:absolute;
top:144.8px;
}
#startButton{
position: relative;
top:150px;
}
</style>
<script>
function startRace(){
raceTimer1 = setInterval("moveHorse(document.getElementById('hImg1'))", 1000)
raceTimer2 = setInterval("moveHorse(document.getElementById('hImg2'))", 1000)
raceTimer3 = setInterval("moveHorse(document.getElementById('hImg3'))", 1000)
}
function moveHorse(h){
a=h.style.left
b=parseInt(a)
b = b + 10
h = b
}
</script>
</head>
<body>
<img src="horse1.png" style="left:10px;" id="hImg1" /><br />
<img src="horse2.png" style="left:10px;" id="hImg2" /><br />
<img src="horse3.png" style="left:10px;" id="hImg3" /><br />
<input type="button" id="startButton" value="Start the Race!" onclick="startRace()" />
</body>
</html>