jQuery更改图像src属性 [英] jQuery change image src attribute

查看:106
本文介绍了jQuery更改图像src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在尝试制作一些基于浏览器的游戏。但我没有什么问题。请打开以下代码并进行检查。

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. 有没有更好的解决方案来处理键盘输入?

  2. 在移动的同时,我正在使用Running soldier改变图像。但是你可以看到我的士兵没有跑,但是在按下按键之后,它将开始运行。为什么会发生这种情况?


推荐答案

1)我想知道只移动你的是否更好在keydown上的角色并阻止他在keyup上。但是你必须要小心,因为并非所有元素都支持该方法。我已经看过将表单元素放在屏幕外的示例(例如position:absolute; left:-999px;),并在页面加载时给定焦点。然后表单字段接受按键,并为其分配键处理程序。

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)因为你经常将图像src重置为运行图像,这意味着它继续前进到第一帧。您可以通过在键关闭时递增计数器来绕过此计数器,并且仅在计数器== 1时更改图像。您还需要检测键的释放,以便您可以更改图像并重置反对0。

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.

这篇关于jQuery更改图像src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆