我正在制作一个游戏,玩家图标应始终按照他走的方向旋转,但图标不会旋转为什么会这样?这是他旋转的方法:
public static void turnPlayer(Direction d){
if (pt == Playere.Kaneki){
RotatedIcon ri = new RotatedIcon(new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png")));;
if (d == Direction.DOWN && p.getDirection() == Direction.RIGHT)
ri.setDegrees(90);
if (d == Direction.LEFT && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
System.out.println("Went as kaneki!");
playerImage.setIcon(ri);
playerImage.repaint();
RepaintPlayer();
p.setDirection(d);
}
if (pt == Playere.Touka){
RotatedIcon ri = new RotatedIcon(new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png")));
if (d == Direction.DOWN && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
System.out.println("Went as touka!");
playerImage.setIcon(ri);
playerImage.repaint();
RepaintPlayer();
p.setDirection(d);
}
}
如果您需要RepeintPlayer()方法:
public static void RepaintPlayer(){
if (p.isAttacking() == true){
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
playerImage.setIcon(img);
System.out.println("WHY IT NO WORKING????? EVEN DO I PUT THISSS?");
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_hit.png"));
playerImage.setIcon(img);
}
}else{
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
playerImage.setIcon(img);
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
playerImage.setIcon(img);
}
}
MovePlayer(p.getX(), p.getY());
playerImage.repaint();
f.repaint();
}
如果你需要全班:
public class TheRealGame{
private static boolean running = false;
private static boolean paused = false;
private static boolean right = false, left = false, up = false, down = false;
private static JFrame f;
private static ArrayList<JLabel> ae = new ArrayList<JLabel>();
private static Player p;
private static Playere pt;
private static JLabel playerImage;
private static boolean info = false;
private static JLabel iy, ix, im, in, iu;
public static void main(Playere playertype){
pt = playertype;
p = new Player(pt);
f = new JFrame();
f.setVisible(true);
f.setSize(700, 700);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
p.attack();
}
if (e.getKeyCode() == KeyEvent.VK_F3){
if (info == true){
info = false;
iy.setVisible(false);
ix.setVisible(false);
im.setVisible(false);
in.setVisible(false);
iu.setVisible(false);
}else if (info == false){
info = true;
iy.setVisible(true);
ix.setVisible(true);
im.setVisible(true);
in.setVisible(true);
iu.setVisible(true);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
p.attack();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
});
playerImage = new JLabel();
iy = new JLabel();
ix = new JLabel();
im = new JLabel();
in = new JLabel();
iu = new JLabel();
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 300, 15);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("free play - tokyo ghoul");
Start();
p.Paint();
}
public static void resume(){
if (paused == false){
return;
}
}
public static void pause(){
if (paused == true){
return;
}
}
public static void Stop(){
if (running == false){
return;
}
running = false;
}
public static void Start(){
running = true;
new Thread(new Runnable(){
@Override
public void run() {
int last = 0, u = 0;
while (running == true){
if (paused != true){
if (up == true){
p.move(p.getX(), p.getY()-1);
if (p.getDirection() != Direction.UP)
turnPlayer(Direction.UP);
}
if (down == true){
p.move(p.getX(), p.getY()+1);
if (p.getDirection() != Direction.DOWN)
turnPlayer(Direction.DOWN);
}
if (left == true){
p.move(p.getX()-1, p.getY());
if (p.getDirection() != Direction.LEFT)
turnPlayer(Direction.LEFT);
}
if (right == true){
p.move(p.getX()+1, p.getY());
if (p.getDirection() != Direction.RIGHT)
turnPlayer(Direction.RIGHT);
}
if (info == true){
int l = 10-last;
iy.setText("y: "+p.getY());
ix.setText("x: "+p.getX());
im.setText("enemys: "+ae.size());
in.setText("next enemy: "+l);
iu.setText("Updated "+u+" times.");
RefreshInfo();
}
if (p.getY() == +292){
p.move(p.getX(), -336);
}
if (p.getY() == -336){
p.move(p.getX(), 292);
}
if (p.getX() == +681){
p.move(-14, p.getY());
}
if (p.getX() == -14){
p.move(681, p.getY());
}
RepaintAllEnemys();
Enemy.UpdateAll();
RepaintPlayer();
if (info != true){
f.repaint();
}
if (last == 10){
Random r = new Random();
int x = 1+r.nextInt(2), y = 1+r.nextInt(2), distance = 1+r.nextInt(570), nx = 0, ny = 0;
if (x == 1){
}
last = 0;
}
last++;
u++;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void RefreshInfo(){
f.remove(iy);
f.remove(ix);
f.remove(im);
f.remove(in);
f.remove(iu);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 100, 15);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
f.repaint();
}
public static void UpdateAll(){
}
public static void Paint(JLabel imgs, int x, int y, File file){
ImageIcon img = new ImageIcon(file.getPath());
imgs.setIcon(img);
imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
imgs.setLocation(x, y);
imgs.setVisible(true);
f.add(imgs);
imgs.setVisible(true);
}
public static void Repaint(JLabel l){
f.remove(l);
l.setBounds((int)l.getLocation().getX(), (int)l.getLocation().getY(), l.getWidth(), l.getHeight());
f.add(l);
}
public static void addAE(JLabel l){
ae.add(l);
}
public static void RepaintAllEnemys(){
for (int i = 0; i < ae.size(); i++){
ae.get(i).repaint();
}
}
public static void MovePlayer(int x, int y){
playerImage.setLocation(x, y);
playerImage.repaint();
f.repaint();
}
public static void RepaintPlayer(){
if (p.isAttacking() == true){
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
playerImage.setIcon(img);
System.out.println("WHY IT NO WORKING????? EVEN DO I PUT THISSS?");
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_hit.png"));
playerImage.setIcon(img);
}
}else{
if (pt == Playere.Kaneki){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
playerImage.setIcon(img);
}
if (pt == Playere.Touka){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
playerImage.setIcon(img);
}
}
MovePlayer(p.getX(), p.getY());
playerImage.repaint();
f.repaint();
}
public static void paintplayer(){
if (pt == Playere.Kaneki){
ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png"));
playerImage.setIcon(imgs);
playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
playerImage.setLocation(274, 277);
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
}
if (pt == Playere.Touka){
playerImage.setIcon(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")));
playerImage.setBounds(p.getX(), p.getY(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconWidth(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconHeight());
playerImage.setLocation(274, 277);
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
}
}
public static void turnPlayer(Direction d){
if (pt == Playere.Kaneki){
RotatedIcon ri = new RotatedIcon(new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png")));;
if (d == Direction.DOWN && p.getDirection() == Direction.RIGHT)
ri.setDegrees(90);
if (d == Direction.LEFT && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
System.out.println("Went as kaneki!");
playerImage.setIcon(ri);
playerImage.repaint();
RepaintPlayer();
p.setDirection(d);
}
if (pt == Playere.Touka){
RotatedIcon ri = new RotatedIcon(new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png")));
if (d == Direction.DOWN && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.RIGHT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.DOWN)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.UP && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.LEFT)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
if (d == Direction.RIGHT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2);
if (d == Direction.DOWN && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2);
if (d == Direction.LEFT && p.getDirection() == Direction.UP)
ri.setDegrees(Math.PI / 2+Math.PI / 2+Math.PI / 2);
System.out.println("Went as touka!");
playerImage.setIcon(ri);
playerImage.repaint();
RepaintPlayer();
p.setDirection(d);
}
}
public enum Direction{
LEFT, UP, RIGHT, DOWN;
}
}
答案 0 :(得分:0)
我不确定,但我有猜测。
我可以看到您始终在RepaintPlayer()
方法playerImage.setIcon(ri);
之后立即调用turnPlayer
。 RepaintPlayer()
使用此调用将图标设置为原始 .png 图片:playerImage.setIcon(img);
。
我建议您暂时删除RepaintPlayer()
方法调用,看看是否会使图标按预期转动。 (你可能很难测试所有的从 - 到方向组合。)