Reputation: 717
The only thing that I do inside the animation loop is update the x and y coordinates but the circle is still not moving as smoothly as it should. This is the fiddle. I am using CraftyJS to animate the circle. Here is the code that does the animation:
.bind("EnterFrame", function (eventData) {
this.x += this.xDirection;
this.y += this.yDirection;
if (this.x < 0) this.xDirection *= -1;
if (this.y < 0) this.yDirection *= -1;
if (this.x > (0.96*gWidth)) this.xDirection *= -1;
if (this.y > (0.96*gHeight)) this.yDirection *= -1;
});
Rest of the calculations are done just once and I don't think just a bunch of multiplications should make the animation lag. Any help on how to make the animation smooth will be appreciated.
I failed to mention earlier that xDirection
is equal to 0.005*gWidth
and yDirection
is equal to 0.005*gHeight
. If gWidth
is 600
the ball is still moving just 3px
. Is it really that fast? I don't want to specify the width in pixels (gWidth is the screen size) because then the gameplay will be different on different devices. Is there some way to move the circle quickly while still keeping the animation smooth?
Upvotes: 2
Views: 206
Reputation: 135
Changing from 'fixed' to 'variable' steptype smoothed things out for me. After Crafty.init, call Crafty.timer.steptype():
const _step = 20;
Crafty.init(gWidth, gHeight, document.getElementById('game'));
Crafty.timer.steptype('variable', _step);
// ...
You may also want to update your EnterFrame to take in to account time elapsed since the last frame:
.bind("EnterFrame", function (eventData) {
let dt = eventData.dt;
this.x += this.xDirection * dt / _step;
this.y += this.yDirection * dt / _step;
// ...
Upvotes: 1