Java动画仅显示最后一帧

时间:2016-02-03 05:46:58

标签: java animation frame

因此,当我的玩家走路时,动画的最后一帧只会被显示。 playerW [11]是显示的最后一帧。如果我要删除它并离开玩家[10],那么该元素最终只能被剥夺。不知道这里搞砸了什么。我查了一下,但没有找到太多的相关性。

public class Animation {

private int speed;
private int frames;


private int index = 0;
private int count = 0;

private BufferedImage[] images;
private BufferedImage currentImg;


public Animation(int speed, BufferedImage... args){
    this.speed = speed;
    images = new BufferedImage[args.length];

    for(int i = 0; i <args.length; i++){
        images[i] = args[i];
    }

    frames = args.length;
}



public void runAnimation(){
    index++;
    if(index > speed){
        index = 0;
        nextFrame();
    }
}


private void nextFrame(){
    for(int i = 0; i < frames; i++){
        if (count == i){
            currentImg = images[i];
        }

        count++;

        if(count> frames){
            count =0;
        }
    }
}


public void drawAnimation(Graphics g, int x, int y){
    g.drawImage(currentImg, x, y, null);
}


}

public class Player {

private BufferedImage player;  //idle

private BufferedImage[] playerW = new BufferedImage[12];   //walking

private Animation playerWalking;

public Player(double x, double y, Game game){

    this.x = x;
    this.y = y;

    SpriteSheet ss = new SpriteSheet(game.getSpriteSheet());




    //idle///////////////////////////
    player = ss.grabImage(1,1,66,94);  
    /////////////////////////////////







    //walking//////////////////////////////
    playerW[0] = ss.grabImage(1,2,66,100);
    playerW[1] = ss.grabImage(2,3,66,100);
    playerW[2] = ss.grabImage(3,2,64,100);
    playerW[3] = ss.grabImage(4,2,60,100);
    playerW[4] = ss.grabImage(5,2,60,100);
    playerW[5] = ss.grabImage(6,2,64,100);
    playerW[6] = ss.grabImage(7,2,64,100);
    playerW[7] = ss.grabImage(8,2,64,100);
    playerW[8] = ss.grabImage(9,2,64,100);
    playerW[9] = ss.grabImage(10,2,64,100);
    playerW[10] = ss.grabImage(11,2,64,100);
    playerW[11] = ss.grabImage(12,2,64,100);
    ////////////////////////////////////////

    playerWalking = new Animation(6, playerW[0], playerW[1], playerW[2] , playerW[3], playerW[4], playerW[5], playerW[6], playerW[7], playerW[8] , playerW[9] , playerW[10] , playerW[11]);
}

public void tick(){

    playerWalking.runAnimation();

    x+=velX;
    y+=velY;

    if(x<=0){
        x = 0;
    }

    if(x>=625 - 35){
        x = 625 - 35;
    }

    if(y <= 80){
        y = 80;
    }

    if(y >= 280 - 100){
        y = 280-100;
    }



}


public void render(Graphics g){


    if(velX != 0){
        playerWalking.drawAnimation(g,(int)x, (int)y);
    }
    else{  //idle
        g.drawImage(player, (int)x,(int)y,null);    
    }

0 个答案:

没有答案