Sign in to follow this  
Followers 0
01271

Setting dynamic animation speed in melonjs version 4+

3 posts in this topic

The documentation for sprite animation is a bit out of date and a new method setAnimationSpeed(speed) or setAnimationSpeed(identifier, speed) should be added.

I have a sprite and an animation

this._super(me.Sprite, 'init', [339, 480, settings]);
		this.addAnimation('normal', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],100);
		this.setCurrentAnimation('normal');

This animates at a delay of 100 ms.

I can change the animation delay by adding another parameter to the addAnimation method.

I could not change the delay afterwards with the solutions I found.

I have seen this answer: https://groups.google.com/forum/#!topic/melonjs/mk6mVcMoVKo

but it is no longer relevant.

this.anim.normal.animationspeed isn't a part of melonjs anymore and changing the other animationspeed settings doesn't have any effect.

This is I think, because of a change from a 'global' animation delay to a frame-based animation delay.

The new method for changing the speed of an animation should now be:

	setAnimationSpeed: function(identifier, speed) {
		for (var i = this.anim[identifier].frames.length - 1; i >= 0; i--) {
			this.anim[identifier].frames[i].delay = speed;
		}
	}

There should be a note on the sprite and/or renderable pages about the animation speed able to be individually set on a per-frame basis. It could be useful for example if some character is looking to the left and then turning quickly to the right the frame set wouldn't have to look like this: [0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[1],[2],[3],[4],[5],[5],[5],[5],[5],[5],[5],[5] and could just have a long delay on the 0 and 5 frames.

Every X.0.0 version should result in a new version of the wiki and documentation with the changes you're making. Relevant changes to each could be made to affected pages after.

Share this post


Link to post
Share on other sites

Can I ask why you're changing the animation speed after the animation is created? We would recommend you just create additional animations if you need a slower or faster version using the same frames but a different speed.

Share this post


Link to post
Share on other sites

hi @01271

you try this it will come

this._super(me.Sprite, 'init', [339, 480, settings]);

this.addAnimation('normal',[{ name: 0, delay: 600 }, { name: 1, delay: 600 },{name: 2, delay: 600 },{name: 3, delay: 600 },{name: 4, delay: 600 },{name: 5, delay: 600 },{name: 6, delay: 600 },{name: 7, delay: 600 },{name: 8, delay: 600 },{name: 9, delay: 600 }]);

this.setCurrentAnimation('normal');

 

give delay how much you want.:)

01271 likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.