jquery - JavaScript move character. Change image on clicking right arrow -
hi i'm making rpg game. want move character arrows. working right now. want change image when clicking let's right arrow. right when click right arrow code:
function rightarrowpressed() { var element = document.getelementbyid("image1").src = "/img/run_1.png"; element = document.getelementbyid("image1"); element.style.left = parseint(element.style.left) + 5 + 'px'; }
the image change's run_1.png
ant stays that. image slides in 1 pose. how change image again when click right arrow ?
html
<img id="image1" src="{{auth::user()->char_image}}" style="position:absolute;left:0; top:0;height: 45px; image-rendering: -webkit-optimize-contrast;">
you can use counter variable:
var counter = 0; function rightarrowpressed() { counter = (counter === 4) ? 1 : counter+1; var image = "/img/run_"+counter+".png"; var element = document.getelementbyid("image1"); element.src = image; var left = parseint(element.style.left)+5; element.style.left = left + "px"; }
and make every time right click, different image used.
Comments
Post a Comment