Question

The program detects left and right arrow keys. If any of those keys are pressed, my Knight object which is an instance of GImage class goes through an array of strings(those strings are directory path to my animation of the Knight object.

The problem with my code is that I cannot seem to find a proper fluid animation with the java code that I came up with. When I press the arrow keys, the animations loops quick and then it slows down but when I lift the arrow key and press again, it animation still loops quick and then it slows down.

Each movement animation has 4 frames of animation.

Here is a reference to the library I am using http://jtf.acm.org/javadoc/student/acm/graphics/GObject.html#pause%28double%29

Any tips is appreciated.

import java.awt.event.KeyEvent;

import acm.graphics.GImage;

import acm.program.GraphicsProgram;



public class Castlevania extends GraphicsProgram {



    public void init(){
        setGameBackground();
        knight = new Knight("knight anim/knightFaceLeft/knight1.png", 500, 500);
        add(knight);
        setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        addKeyListeners();


    }


    private void setGameBackground() {
        // TODO Auto-generated method stub
        gameBackgroundImage = new GImage("link sprites/zelda_background.png");
        add(gameBackgroundImage);
    }



    public void keyPressed(KeyEvent e){ 

        /* Knight's Movement
         * 
         */
        int knightMovementKey = e.getKeyCode();
        if(knightMovementKey == KeyEvent.VK_LEFT)
        {

            this.xSpeed = 10;
            ySpeed = 0;


            knight.setImage(KnightFaceLeft[knightFrame]);
            knight.move(-xSpeed, ySpeed);

            knightFrame ++; 

            if(knightFrame >= KnightFaceLeft.length)
            {
                knightFrame = 0;
            }


        }

        else if(knightMovementKey == KeyEvent.VK_RIGHT){

            knight.setImage(KnightFaceRight[knightFrame]);
            knight.move(xSpeed,ySpeed);
            knightFrame++;

            this.xSpeed = 10;
            ySpeed = 0;

            if(knightFrame>=KnightFaceRight.length){

                knightFrame = 0;
            }

        }
        knight.pause(DELAY);
    }

    private String[] KnightFaceLeft = {"knight anim/knightFaceLeft/knight1.png","knight anim/knightFaceLeft/knight2.png","knight anim/knightFaceLeft/knight3.png","knight anim/knightFaceLeft/knight4.png"}; //Add in as many images as you want for your animation
    private String[] KnightFaceRight = {"knight anim/knightFaceRight/knight1.png","knight anim/knightFaceRight/knight2.png","knight anim/knightFaceRight/knight3.png","knight anim/knightFaceRight/knight4.png"}; 
    private GImage gameBackgroundImage;
    private Knight knight;
    private final int APPLICATION_WIDTH = 1200;
    private final int APPLICATION_HEIGHT = 800;
    private int knightFrame = 0;
    private int xSpeed ; //the number of pixels to move in x
    private int ySpeed = 0; //0 so you only move horiontally
    private double DELAY = 35;
}

import acm.graphics.GImage.*;

public class Knight extends GImage {

                public Knight(String imageDirectory, double knight_Location_XCoord, double knight_Location_YCoord) {
                    super(imageDirectory,knight_Location_XCoord, knight_Location_YCoord);
                }
            }
Était-ce utile?

La solution

Your call to pause() is blocking the event dispatch thread. In this context, use acm.util.SwingTimer to pace the animation in the timer's ActionListener.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top