I made them serializable, the Animation holds an int[] instead of a bufferedImage which is converted back and fourth in the set/get methods. I have it working, its just that im sending way to much useless data over the socket. There are little jitters occasionally even when both client and host are on localhost.
I was trying to think of how I could sum up the state of the sprite list in a string or some primitive array. Like when a sprite is created maybe I could have a SpriteID field, then I could send info like
“sprite:294 x:2 y:8 animation:up animationProgress:873”. Or maybe I could just let the clients keep track of how far along the animation is and just have the host send information about when the animation changes.
My thought behind the sprite ID is that the host/client sprite list may not stay in the same order, so each sprite would have a number associated, which I guess would mean in need a sprite factory class.
Here is my animation class by the way, i thought i posted it in the first one, guess not:
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.io.*;
/**
The Animation class manages a series of images (frames) and
the amount of time to display each frame.
*/
public class Animation implements Serializable{
private ArrayList<AnimFrame> frames;
private int currFrameIndex;
private long animTime;
private long totalDuration;
/**
Creates a new, empty Animation.
*/
public Animation() {
this(new ArrayList<AnimFrame>(), 0);
}
public Animation(BufferedImage img) {
this();
frames.add(new AnimFrame(img, 1));
}
private Animation(ArrayList<AnimFrame> frames, long totalDuration) {
this.frames = frames;
this.totalDuration = totalDuration;
start();
}
public void setAnimTime(long time) {
animTime = time;
}
public long getAnimTime() {
return animTime;
}
public int getDuration() {
return (int)totalDuration;
}
/**
Creates a duplicate of this animation. The list of frames
are shared between the two Animations, but each Animation
can be animated independently.
*/
public Object clone() {
return new Animation(frames, totalDuration);
}
/**
Adds an image to the animation with the specified
duration (time to display the image).
*/
public synchronized void addFrame(BufferedImage image,
long duration)
{
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}
/**
Starts this animation over from the beginning.
*/
public synchronized void start() {
animTime = 0;
currFrameIndex = 0;
}
/**
Updates this animation's current image (frame), if
neccesary.
*/
public synchronized void update(long elapsedTime) {
if (frames.size() > 1) {
animTime += elapsedTime;
if (animTime >= totalDuration) {
animTime = animTime % totalDuration;
currFrameIndex = 0;
}
while (animTime > getFrame(currFrameIndex).endTime) {
currFrameIndex++;
}
}
}
/**
Gets this Animation's current image. Returns null if this
animation has no images.
*/
public synchronized BufferedImage getImage() {
if (frames.size() == 0) {
return null;
}
else {
return getFrame(currFrameIndex).getImage();
}
}
private AnimFrame getFrame(int i) {
return (AnimFrame)frames.get(i);
}
public void reset(){
animTime = 0;
currFrameIndex = 0;
}
private class AnimFrame implements Serializable {
int[] imageData;
int height, width;
long endTime;
public AnimFrame(BufferedImage image, long endTime) {
width = image.getWidth();
height = image.getHeight();
imageData = new int[width * height];
image.getRGB(0, 0, width, height, imageData, 0, width);
this.endTime = endTime;
}
public BufferedImage getImage() {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, imageData, 0, width);
return image;
}
}
}