0

首先对不起我的英语不好

大家好,我正在使用 java 开发 2d 游戏,当想要为一系列图像设置动画时,只加载最后一个图像并且我不知道为什么我的代码看起来是逻辑的,所以当我按下“Q”按钮时,大多数情况下那里的动画。代码:

哥们班

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;


public class Dude {
int x, dx, y, dy;
Image still;
Image walking;
Image effet;
ImageIcon i = new ImageIcon("C:/1.gif");
ImageIcon run=new ImageIcon("C:/NeroRun.gif");
ImageIcon down=new ImageIcon("C:/nero_down.gif");
Image[]attack=new Image[15];
ImageIcon saut_effet=new ImageIcon("C:/saut_effet.gif");
ImageIcon saut=new ImageIcon("C:/saut.gif");
ImageIcon effect=new ImageIcon("C:/saut_effet.gif");
//ImageIcon attack;

public Dude() throws {
    still = i.getImage();
    x = 10;
    y = 260;
    for(int j=0;j<15;j++){
        ImageIcon ii=new ImageIcon("C:/Games/frame-"+(j+1)+".gif");
        attack[j] = ii.getImage();
        System.out.println("Nice");

    }

}

public void move(){
    x = x + dx;
    y = y + dy;

}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public Image getImage(){
    return still;
}

public Image getEffet(){
    return effet;
}

public void keyPressed(KeyEvent e) throws InterruptedException{
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = -3;
        still=run.getImage();
        System.out.println("Left");
        if(y==260){y+=55;}
    }

   if (key == KeyEvent.VK_RIGHT) {
         dx = 3;
        still=run.getImage();
        System.out.println("Right");
        if(y==260){y+=55;}
        effet=null;
    }

   if (key == KeyEvent.VK_DOWN) {
        still=down.getImage();
        System.out.println("Down");
        if(y==260){y+=90;} 
        effet=null;
   }

   if (key == KeyEvent.VK_UP){
       if(y==260){y-=130;}
       effet=effect.getImage();
       still=saut.getImage();
   }

  if(key == KeyEvent.VK_Q){
      if(y==260){y-=135;
      for(int j=0;j<15;j++){
          still=attack[j];
      }

      //attack=new ImageIcon("C:/first_attack.gif");
      //still=attack.getImage();}
      effet=null;
    }
  }    
}

    public void keyReleased(KeyEvent e){
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT){
        dx = 0;
        still=i.getImage();
        y=260;
        effet=null;
    }

    if (key == KeyEvent.VK_RIGHT){
        dx = 0;
        still=i.getImage();
        y=260;
        effet=null;
    }

    if (key == KeyEvent.VK_DOWN){
        dx = 0;
        still=i.getImage();
        y=260;
        effet=null;
    }

    if (key == KeyEvent.VK_UP){
        still=i.getImage();
        y=260;
        effet=null;
    }

    if (key == KeyEvent.VK_Q){
        still=i.getImage();
        y=260;
        effet=null;
    }

}
}

博拉德1类:

包FirstTest;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.newdawn.slick.SlickException;


/**
 *
 * @author SiLeNT J0cK3R
 */
class Board1 extends JPanel implements ActionListener{

Dude p;
Image img;
Timer time;

public Board1() throws SlickException{
    p = new Dude();
    addKeyListener(new AL());
    setFocusable(true);
    ImageIcon i = new ImageIcon("C:/Background.png");
    img = i.getImage();
    time = new Timer(5, this);
    //Music.music();
    time.start();
}
    @Override
    public void actionPerformed(ActionEvent e){
    p.move();
    repaint();
    //System.out.println("Repaint");
}

    @Override
    public void paint(Graphics g){
    super.paint(g);
            Graphics2D g2d = (Graphics2D) g;

            g2d.drawImage(img, 0, 0, null);
            g2d.drawImage(p.getImage(), p.getX(), p.getY(),null);
            //Pour animer les effets loresequ'on effectue un saut
            g2d.drawImage(p.getEffet(),p.getX(),300,null);
            //Pour animer les ennemies

}

private class AL extends KeyAdapter{
    @Override
    public void keyReleased(KeyEvent e){
        p.keyReleased(e);
    }
    @Override
    public void keyPressed (KeyEvent e){
        try {
            p.keyPressed(e);
        } catch (InterruptedException ex) {
            Logger.getLogger(Board1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



}
4

1 回答 1

0
      for(int j=0;j<15;j++){
      still=attack[j];
  }

识别 Q 键时,您似乎只是穿过动画帧(图像)。要使动画可见,动画帧的每次更改之间必须经过一段时间。

我建议您看一些示例和教程。就像在这里找到的 Space Invaders 克隆一样:www.cokeandcode.com

于 2013-02-24T14:55:44.023 回答