Jump to content

Can I lock the sprite position update fps to the current animation's fps?


wayfinder
 Share

Recommended Posts

Hi!

 

I'm building a game ( current state here: http://yfinder.de/phasertest/update ), and I'm running into difficulties with animation fps vs. sprite position fps. (I'm scaling to 2x for aesthetic reasons, but I do it with drawImage so the actual game calculations happen with 1px sized pixels)

 

A good example is the spear guy that walks at you early in the level: the animation is running at 8 fps, and i created it in such a way that the sprite advances 3px per frame. I set the x velocity to -24 px per second, so that the new frame is displayed after every 3 pixels of movement. So far, so good.

 

The problem is that the sprite position is updated at 60 fps, while I want it to update in increments of 3 px (ie with 8 fps). Is there a generalized way to implement the sprite position to the fps of the current anim?

Link to comment
Share on other sites

I'm doing something similar with a project I'm working on. The way to do this would be to manually update the body.position in the update loop (make sure body.moves = false) and listen for an 'update' event on the animation. Unfortunately, the only events currently dispatched by animations are on start, complete and loop - there's no 'onFrame' or 'onUpdate' event. I imagine this is possibly on purpose as it could potentially end up generating a huge amount of signals. In the mean time, you could maybe do something like this:

function update() {  if (sprite.animations.currentAnim.frame !== sprite.oldFrame) {    sprite.oldFrame = sprite.animations.currentAnim.frame;    // do your movement updates here, and they will only happen when the frame changes  }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...