Expand|Select|Wrap|Line Numbers
- public class Enemy{
- private BufferedImage[] bEnemyhit = {SpriteSheet.getSprite(6, 0), SpriteSheet.getSprite(7, 0),
- SpriteSheet.getSprite(8, 0), SpriteSheet.getSprite(9, 0),};
- private SpriteAnimation aEnemyHit = new SpriteAnimation(bEnemyhit, 10, false); //set loop vairable in spriteAnimation.class to false
- private SpriteAnimation animationEnemyHit = aEnemyHit;
- private boolean flag = false;
- ...
- public void enemyMove() //this method is in main game loop
- {
- x -= dx;
- if(!flag){ //start only onces
- this.animationEnemyHit.start();
- flag = true;
- }
- this.animationEnemyHit.update();
- }
- ...
- }
Expand|Select|Wrap|Line Numbers
- public class SpriteAnimation {
- private int frameCount; // Counts ticks for change
- private int frameDelay; // frame delay 1-12 (You will have to play around with this)
- private int currentFrame; // animations current frame
- private int animationDirection; // animation direction (i.e counting forward or backward)
- private int totalFrames; // total amount of frames for your animation
- private boolean stopped; // has animations stopped
- private boolean loop; //loop for ever or loop onces
- private List<SpriteFrame> SpriteframeObject = new ArrayList<SpriteFrame>(); // Arraylist of frames
- public SpriteAnimation(BufferedImage[] frames, int frameDelay, boolean shouldLoop) {
- this.frameDelay = frameDelay;
- this.stopped = true;
- this.loop = shouldLoop; //see if it has to loop or no
- for (int i = 0; i < frames.length; i++) {
- addFrame(frames[i], frameDelay);
- }
- this.frameCount = 0;
- this.frameDelay = frameDelay;
- this.currentFrame = 0;
- this.animationDirection = 1;
- this.totalFrames = this.SpriteframeObject.size();
- }
- public void start() {
- if (!stopped) {
- return;
- }
- if (SpriteframeObject.size() == 0) {
- return;
- }
- stopped = false;
- }
- public void stop() {
- if (SpriteframeObject.size() == 0) {
- return;
- }
- stopped = true;
- }
- public void restart() {
- if (SpriteframeObject.size() == 0) {
- return;
- }
- stopped = false;
- currentFrame = 0;
- }
- public void reset() {
- this.stopped = true;
- this.frameCount = 0;
- this.currentFrame = 0;
- }
- private void addFrame(BufferedImage frame, int duration) {
- if (duration <= 0) {
- System.err.println("Invalid duration: " + duration);
- throw new RuntimeException("Invalid duration: " + duration);
- }
- SpriteframeObject.add(new SpriteFrame(frame, duration));
- currentFrame = 0;
- }
- public BufferedImage getSprite() {
- return SpriteframeObject.get(currentFrame).getFrame();
- }
- public void update() {
- if (!stopped) {
- frameCount++;
- if (frameCount > frameDelay) {
- frameCount = 0;
- currentFrame += animationDirection;
- if (currentFrame > totalFrames - 1) {
- currentFrame = 0;
- //if not looping for ever than stop
- if(!loop)
- {
- stopped = true;
- }
- }
- else if (currentFrame < 0) {
- currentFrame = totalFrames - 1;
- if(!loop)
- {
- stopped = true;
- }
- }
- }
- }
- }
- }
right now what is happing is that it only prints 1st image than stops. i want to do one full ciyle than stop. any ideas?