Solved Kryonet How to update sprite animation in client/server

Hello, I’m implementing Kryonet with Libgdx and is working properly, is beaultiful Android + Desktop multiplayer.

I can update the player position and send the information:
if playerMoved - sendMsg - ReceiveMsg - ChangePosition.

In the other side (receiver) the position is updated in real time but the SPRITE ANIMATION is not updated… I’m trying to implement this but maybe the problem is inside my Sprite/Player.

Here my Player.handleInput (if return true, send a msg that “player moved” and I get the new position with getX(), getY()):

	public boolean handleInput(float delta)
	{
		if (Gdx.input.isKeyPressed(Keys.RIGHT))
			setXManual(5*delta);
			else if (Gdx.input.isKeyPressed(Keys.LEFT))
				setXManual(-5*delta);
				else
				{
					stateTime += Gdx.graphics.getDeltaTime()*1.5F;
					sprite.setRegion(Assets.animationStop.getKeyFrame(stateTime, true)); // sprite animation
				}
		
		if (isMoved)
		{
			isMoved = false;
			return true;
		}
		else
			return false;
	}
	
		public void setXManual(float x)
		{
			playerRec.x += x;
			
			if (x >0)
			{
	    		stateTime += Gdx.graphics.getDeltaTime()*1.5F;
	    		sprite.setRegion(Assets.animationRight.getKeyFrame(stateTime, true)); // sprite animation
			}
			else if (x<0)
			{
		    	stateTime += Gdx.graphics.getDeltaTime()*1.5F;
		    	sprite.setRegion(Assets.animationLeft.getKeyFrame(stateTime, true)); // sprite animation
			}
			isMoved = true;
		}

If player1 is moving, player1 can see the animation, player2 can’t see the animation. (vice-versa).

How can I update the sprite ANIMATION to be seen on other client.

You could send a packet to “start” walking, and another packet to stop walking. I’m assuming you have a “stationary” animation and a walking one. If you’re trying to sync the EXACT frames, this won’t work because there’s always network lag.

I’m sending only when the method return true (isMoved). My character has animation stopped and moving, so I think I need to send the “animation changes” in every frame, but I don’t know how or a way to deal with animations coming from other player.

To update the position of player1 in the client player2 I only need to send the X and Y and update the player1 inside the client of player2.

In this piece of code:

stateTime += Gdx.graphics.getDeltaTime()*1.5F;
sprite.setRegion(Assets.animationStop.getKeyFrame(stateTime, true));

What information do I need to send to update the animation in player2 side?


Edited:
Interesting, if I change the method “public synchronized void” to “public void” the animation happens. I only need to set some switch/case with some state to only need to send an integer value, so I can change the sprite properly in second player side without heavy animation/sprite packet.

Now I’m so confused, when and where I use “public synchronized void” ? :S

Synchronized methods basically allow one thread to use a method (until the method finishes) at a time. Kryonet has threads for its networking; so does GL. Maybe the synchronized block was stopping other threads from using it. (I have very little experience with synchronized blocks, but you can find a bit more data on the official documentation here)

just save previous x and y and compare it to the new x and y if it changes just update the animation or something…

Might I add, does it really, really matter if the server knows exactly what the state time of an animation is?

Is best not to just inform the server:

"Hey I just entered the walk state. Let everyone know ".

The clients should know how to draw and time the animations.

Yes, correct. I’m new on multiplayer and network, so I’m reading a lot of stuff to understand a little bit more how it works and some practice.

My animation is working now and I’m so happy that I can play my own multiplayer game.
I’m playing this two multiplayer games with two open windows (localhost) and trying to understand what I need/don’t need to send to the server, I have a lot of doubts right now (such as lightning map, shaders in mutiplayer), but I think I’m learning as well, kryonet helps a lot because it’s easy to use.

http://heroesofumbra.com/
https://bitbucket.org/code6/multiplayerspacegame

You should try and identify the core state of your objects. Have it live in a single place, i.e., minimizing the state.

Ideally, for instance, to render the scene you should be able to pass only a single blob of state to the renderer to render the whole scene. This way you’ll only have to pass blobs of state around instead of trying to haphazardly synchronize data between multiple clients.

Simply include the state of the player with the packets you’re sending now to update their position etc.

Just include different states such as IDLE, WALKING, ATTACKING, SHOOTING (or whatever the states may be).

if (playerState == Player.WALKING){
	animateThatStateHere();
}

Hopefully you get the idea.

Although if you want to OPTIMIZE EXTREMELY, you can handle all of that sort of stuff client side but just including the state of the player with the players information is way easier and definitely doesn’t cost much in terms of bandwidth as it’s just 4 bytes.