计时 javascript 和 src 更改?

发布于 2024-10-21 12:28:00 字数 648 浏览 7 评论 0原文

好的,所以我有这个 javascript,当我按下箭头键时,可以使用箭头键发送这些 javascript。我有一个协作脚本,可以使正确的('img')命令移动图像,但我试图在图像随着 src 更改和延迟而移动时更改图像。帮助?

<script type="text/javascript">
document.onkeydown = KeyCheck;       
function KeyCheck(event) {

  var spacebar=32
  var KeyID = event.keyCode;
  switch(KeyID) {

      case 39:
        right('img'); 
        document.getElementById('img').src = 'guyr.png';
        setTimeout("right('img'); 
        document.getElementById('img').src = 'runr.png'; 
        setTimeout("right('img'); 
        document.getElementById('img').src = 'guyr.png';",100);",100);
      break;

   }

}
</script>

ok so i have this javascript to use the arrow keys to send these javascripts when i press arrowkeys. i have a collaborating script that makes the right('img') command move the image but im trying to change the image as it moves with src changes and delays. Help?

<script type="text/javascript">
document.onkeydown = KeyCheck;       
function KeyCheck(event) {

  var spacebar=32
  var KeyID = event.keyCode;
  switch(KeyID) {

      case 39:
        right('img'); 
        document.getElementById('img').src = 'guyr.png';
        setTimeout("right('img'); 
        document.getElementById('img').src = 'runr.png'; 
        setTimeout("right('img'); 
        document.getElementById('img').src = 'guyr.png';",100);",100);
      break;

   }

}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-10-28 12:28:00

更改图像的 src 属性将触发异步 GET 请求,并且在浏览器缓存图像之前一开始可能会很慢。看起来你正在实现某种游戏?我不会更改单个图像的 src ,而是最初隐藏所有图像,并切换要显示/隐藏的图像的 visibility CSS 属性。这样,您就可以预取加载期间需要的所有图像,并简单地显示/隐藏元素作为 onkeydown 事件的处理程序。

Changing the src attribute of an image is going to trigger an asynchronous GET request, and may be slow at first before the browser has cached the images. It looks like you are implementing a game of some sort? Instead of changing the src of a single image, I would keep all of the images hidden initially, and toggle the visibility CSS attribute of images you want to show/hide. That way, you can pre-fetch all of the images that you'll need during load time, and simply show/hide elements as the handlers for your onkeydown events.

与他有关 2024-10-28 12:28:00

如果您想做某种游戏或动画,请尝试阅读一些相关内容,了解如何构建游戏循环、动画等。

但是,如果您只是做某种测试,请尝试这样的事情,这不是一个很好的选择。漂亮的代码,但动画的想法应该可行:

document.onkeydown = KeyCheck;

function KeyCheck(event) {

    var spacebar = 32;
    var KeyID = event.keyCode;

    switch (KeyID) {

    case 39:

        function guy(fn) {
            right('img');
            document.getElementById('img').src = 'guyr.png';
            setTimeout(function() {
                fn(guy);
            }, 100);
        }
        function run(fn) {
            right('img');
            document.getElementById('img').src = 'runr.png';
            setTimeout(function() {
                fn(run);
            }, 100);
        }

        guy(run);

        break;
    }

}

一个函数调用另一个函数,通过参数接收它,以便继续动画。

If you want to do some sort of a game or animation, try to read a little about it, how to structure your game loop, animations and etc.

But if you are doing just some sort of test try something like this, is not a beautiful code but the idea of an animation should work:

document.onkeydown = KeyCheck;

function KeyCheck(event) {

    var spacebar = 32;
    var KeyID = event.keyCode;

    switch (KeyID) {

    case 39:

        function guy(fn) {
            right('img');
            document.getElementById('img').src = 'guyr.png';
            setTimeout(function() {
                fn(guy);
            }, 100);
        }
        function run(fn) {
            right('img');
            document.getElementById('img').src = 'runr.png';
            setTimeout(function() {
                fn(run);
            }, 100);
        }

        guy(run);

        break;
    }

}

One function calls another, receiving it by parameter, in order to to continue the animation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文