jQuery change image src attribute

余生长醉 提交于 2019-12-12 18:12:04

问题


Right now I'm trying make some browser based game. But I have little questions. Please open below code and inspect it.

http://jsfiddle.net/thisizmonster/YVz6L/4/

  1. Is there any better solutions handle keyboard inputs?
  2. While moving, i'm changing image with Running soldier. But as you can see my soldier not running, but after stopped press key, it will start run. Why its happening?

回答1:


1) I wonder whether it would be better to only move your character on keydown and stop him on keyup. You'd have to be careful though, as not all elements support the method. I've seen examples before where a form element is placed offscreen (e.g. position:absolute; left:-999px;), and given focus as the page loads. The form field then accepts keypresses and the key handlers are assigned to it.

2) Is because you're constantly resetting the image src to the run image, which means it keeps going to the first frame. You could get round this by incrementing a counter while the key is down, and only changing the image when the counter == 1. You'd also need to detect the release of the key, so you can change the image back and reset the counter to 0.



来源:https://stackoverflow.com/questions/7425821/jquery-change-image-src-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!