// DirectionalSprite Class // DirectionalSprite.java // Imports import java.awt.*; import java.util.Random; public class DirectionalSprite extends Sprite { protected static final int[][] velDirs = { {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1} }; protected Image[][] image; protected int direction; public DirectionalSprite(Component comp, Image[] img, Point pos, Point vel, int z, int ba, int d) { super(comp, img[d], pos, vel, z, ba); image[0] = img; setDirection(d); } public DirectionalSprite(Component comp, Image[][] img, int f, int fi, int fd, Point pos, Point vel, int z, int ba, int d) { super(comp, img[d], f, fi, fd, pos, vel, z, ba); image = img; setDirection(d); } public int getDirection() { return direction; } public void setDirection(int dir) { // Set the direction if (dir < 0) dir = 7; else if (dir > 7) dir = 0; direction = dir; // Change the velocity velocity.x *= velDirs[dir][0]; velocity.y *= velDirs[dir][1]; // Set the image setImage(image[dir]); } public void setVelocity(Point vel) { velocity = vel; // Change the direction if (vel.x == 0 && vel.y == 0) return; if (vel.x == 0) direction = (vel.y + 1) * 2; else if (vel.x == 1) direction = vel.y + 1; else if (vel.x == -1) direction = -vel.y + 6; } }