首先抱歉我的英语不好
大家好我正在使用java开发2D游戏,当想要动画一系列图像时,只加载最后一张图像&我不知道为什么我的代码似乎是逻辑 因此,当我按下“Q”按钮时,那里的动画最多。 代码:
Dude Class
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;
}
}
}
Borad1课程:
包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);
}
}
}
}
答案 0 :(得分:0)
for(int j=0;j<15;j++){
still=attack[j];
}
识别Q键时,您似乎只是浏览动画帧(图像)。要使动画可见,必须在动画帧的每次更改之间传递一段时间。
我建议你看看一些例子和教程。就像在此处找到的Space Invaders克隆一样:www.cokeandcode.com