简体   繁体   English

使用JavaScript动画/移动精灵时口吃

[英]Stutter when animating / moving sprite with JavaScript

I'm attempting to create an animation by moving a sprite image across a div. 我试图通过在div上移动精灵图像来创建动画。 The sprite image contains each frame of the animation. 精灵图像包含动画的每个帧。 The size of the "canvas" is 600px by 624px. “画布”的大小是600px乘624px。 Each frame on the sprite sheet is positioned every 600px and I'm moving the image 600px at a time. 精灵表上的每个帧每600px定位,我一次移动图像600px。

Here is what I have so far... voyced.com/crownacre/www/demo/sprite.html 这是我到目前为止所做的 ... voyced.com/crownacre/www/demo/sprite.html

I'm using the following JavaScript to move the image across the screen... 我正在使用以下JavaScript在屏幕上移动图像...

(function myLoop(i) {
    setTimeout(function() {
        defImg.css({
            right: '-=600'
        });
        if(--i) myLoop(i); //  decrement i and call myLoop again if i > 0
    }, 60) // delay ms
})(114); // number of frames in the sprite

I've used several sprites all floated left as the total width of sprite sheets in 69000px, which causes even more issues if I use just one image! 我已经使用了几个漂浮在左边的精灵作为69000px的精灵表的总宽度,如果我只使用一个图像就会导致更多的问题! Hence why I have 4 at the moment. 因此,为什么我现在有4个。

So... The problem I am having is that the animation pauses briefly several times. 所以...我遇到的问题是动画暂停几次。 It seems fine in Firefox (for me), but you notice it in Chrome and you can't miss it in IE. 它似乎在Firefox(对我来说)很好,但你在Chrome中注意到它,你不能错过它在IE中。

It also always stutters every 16200px, making me think this is related to moving 1 sprite into the next on the screen. 它也总是每16200px口吃,让我觉得这与将1个精灵移动到屏幕上的下一个精灵有关。

Ideas please people? 想请人吗?

Thanks in advance! 提前致谢!

Have you tried using a sprite animation plugin? 你尝试过使用精灵动画插件吗?

http://www.spritely.net/ http://www.spritely.net/

Does what you want, seems to run well on their demo. 你想要什么,似乎在他们的演示中运行良好。

Let me first say: The huge images you're trying to display as a sprite isn't exactly what sprites/animations are used for. 首先我要说的是:您尝试显示为精灵的巨大图像并不是精灵/动画的用途。 You can better look into a real <canvas> solution (especially when looking at your animation), but that would require some more complex JavaScript skills. 您可以更好地查看真正的 <canvas>解决方案(特别是在查看动画时),但这需要一些更复杂的JavaScript技能。

Anyway, the problem with the stutter is because you're using several images that are all float ed to the left , and position the slider with the right property. 无论如何,口吃的问题是因为你使用了几个floatleft ,并使用right属性position slider Each time another image needs to be displayed, a stutter can be noticed. 每次需要显示另一个图像时,可以注意到口吃 This might have something to do with the browser engine, needing to paint the actual image (which is hard, since they're pretty big). 这可能与浏览器引擎有关,需要绘制实际图像(这很难,因为它们非常大)。

So, instead of using several images, you could also use one (take note, you might want to make this a .JPG or .GIF since they tend to be more compact than .PNG ) and use actual CSS sprites with background-position . 因此,您可以使用一个(请注意,您可能希望将其设置为.JPG.GIF因为它们往往比.PNG更紧凑),并使用具有background-position 实际 CSS精灵,而不是使用多个图像。

Here's an example that uses your code, and one single image . 这是一个使用您的代码和一个图像的示例 Good luck! 祝好运!

Thanks again for the feedback guys. 再次感谢您的反馈。 I used a combination of your tips that have helped me solve the issue I was having. 我使用了一些提示,帮助我解决了我遇到的问题。

  1. Spritely has helped immensely. Spritely帮助极大。 Essentially it is doing the same as what @marcoK suggested, and adjusting the background-position property. 基本上它与@marcoK建议的相同,并调整背景位置属性。 This plugin also provides a fool proof way of controlling each frame of the sprite, as well as creating callbacks when it reaches a specified frame - awesome! 这个插件还提供了一种控制精灵每个帧的简单方法,以及当它到达指定帧时创建回调 - 太棒了!

  2. The other issue was the huge sprite. 另一个问题是巨大的精灵。 Mobile safari won't allow anything larger than 3MP so the max size I could make the image was 4800x624. 移动野生动物园不允许任何大于3MP的东西,所以我可以拍摄的最大尺寸为4800x624。 I have 15 of these each as a separate animation that calls the next when it reaches the last frame. 我将其中的15个作为单独的动画,当它到达最后一帧时调用下一个动画。 I was very sceptical about this working smoothly, but it does, and in all browsers. 我非常怀疑这种工作顺利进行,但确实如此,并且在所有浏览器中都是如此。

I'm not overly happy with the number of request it makes but after optimising the pngs the file size isn't too bad if I add a pre-loader. 我对它的请求数量并不是很满意,但是在优化了png之后,如果我添加一个预加载器,文件大小也不会太糟糕。

Really pleased with the outcome... http://www.crownacre.voyced.com/ and one more reason not to use Flash! 真的很满意结果...... http://www.crownacre.voyced.com/还有一个不使用Flash的理由!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM