简体   繁体   English

Java游戏雪碧卡住了

[英]Java Game Sprite Stuck

I am having a problem with a stuck sprite as soon as I switch to another JPanel via CardLayout. 通过CardLayout切换到另一个JPanel时,我就遇到了卡住的精灵的问题。

import java.awt.CardLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList;
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel;

public class Game extends JFrame implements Runnable {

    private viewA v1;
    private viewB v2;
    private viewC v3;
    private viewD v4;
    private Player player;
    private CardLayout c1;
    private JPanel contPanel;

    // ############## VIEW A BUTTONLISTENER
    public class ViewAButtonList implements ActionListener{

        public void actionPerformed(ActionEvent ev){
            try{
            c1.show(contPanel, "1");

            } catch (Exception ex){

            ex.printStackTrace();
            }

        }


    }
    // ############## VIEW B BUTTON LISTENER
    public class ViewBButtonList implements ActionListener{

        public void actionPerformed(ActionEvent ev){
            try{
            c1.show(contPanel, "2");

            } catch (Exception ex){

            ex.printStackTrace();
            }

        }


    }
    // ############## VIEWC BUTTON ACTIONEVENT
    public class ViewCButtonList implements ActionListener{

        public void actionPerformed(ActionEvent ev){
            try{
            c1.show(contPanel, "3");

            } catch (Exception ex){

            ex.printStackTrace();
            }

        }


    }
    // ############## VIEW D BUTTON ACTION EVENT
    public class ViewDButtonList implements ActionListener{

        public void actionPerformed(ActionEvent ev){
            try{
            c1.show(contPanel, "4");

            } catch (Exception ex){

            ex.printStackTrace();
            }

        }


    }





    public Game() {
        player = new Player();
        // load player settings from server
        //  ..
        //    contPanel = new JPanel();
     //load views    v1 = new ViewA(Player); v2=new ViewB(Player); v3 = new ViewC(Player); v4 =new ViewD(Player);

     c1 = new CardLayout();

     contPanel.setLayout(c1);


     contPanel.add(v1,"1");

     contPanel.add(v2,"2");

     contPanel.add(v3"3");

     contPanel.add(v4,"4");

        c1.show(contPanel, "2");
        currPos =2;

        this.add(contPanel);

        setSize(652, 480);
        setLocationRelativeTo(null);
        setTitle("GAME");
        setResizable(false);
        setVisible(true);//go to end of view B (x=0), change to View A, close view B


       // Create 4 of each button to place in each seperate view (to swtich back and forth)
      // ................


    }

    public static void main(String[] args) {
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new Game();
             }
         });
    }

    @Override   public void run() {         // TODO Auto-generated method stub
    } }

Now.. everything works from the standpoint of switching between screens.. but my Player sprite gets stuck on screen after switch, but on initial load I can move the sprite around with the arrow keys. 现在..从屏幕之间切换的角度来看,一切正常。.但我的Player精灵在切换后卡在屏幕上,但是在初始负载下,我可以使用箭头键在周围移动精灵。 I am not sure if passing the Player on the new ViewA(Player) might be the culprit.. but I have a feeling that it is.. can't figure out what I am doing wrong.. 我不确定在新的ViewA(Player)上传递播放器是否是罪魁祸首..但我有这样的感觉..无法弄清楚我在做什么错..

Here's theplayer: 这是玩家:

import java.awt.Image; 导入java.awt.Image; import java.awt.Rectangle; 导入java.awt.Rectangle; import java.awt.event.KeyEvent; 导入java.awt.event.KeyEvent;

import java.util.ArrayList; 导入java.util.ArrayList;

import javax.swing.ImageIcon; 导入javax.swing.ImageIcon;

public class Player { 公开课玩家{

 private String playersprite = "playersprite.png"; private int dx; private int dy; private int x; private int y; private int width; private int height; private boolean isFired; private boolean visible; private Image image; private ArrayList missiles; public Player() { ImageIcon ii = new ImageIcon(this.getClass().getResource(playersprite)); image = ii.getImage(); width = image.getWidth(null); height = image.getHeight(null); missiles = new ArrayList(); visible = true; isFired=false; //default spawn location x = 600; y = 400; } public void move() { x += dx; y += dy; if(x<=0){x=0;} if(x>=640-20){x=640-20;} if(y<=0){y=0;} if(y>=400){y=400;} } public int getX() { return x; } public int getY() { return y; } public void setX(int x){ this.x=x; } public void setY(int y){ this.y=y; } public Image getImage() { return image; } public ArrayList getMissiles() { return missiles; } public void setVisible(boolean visible) { this.visible = visible; } public boolean isVisible() { return visible; } public Rectangle getBounds() { return new Rectangle(x, y, 32, 32); } public void keyPressed(KeyEvent e) { System.out.println("X: "+x+",Y: "+y); int key = e.getKeyCode(); if (key == KeyEvent.VK_SPACE) { if(isFired==false){ isFired=true; fire(); } else return; } if (key == KeyEvent.VK_LEFT) { dx = -2; } if (key == KeyEvent.VK_RIGHT) { dx = 2; } if (key == KeyEvent.VK_UP) { dy = -2; } if (key == KeyEvent.VK_DOWN) { dy = 2; } } public void fire() { System.out.println("Player used weapon"); // missiles.add(new Missile(x, y)); } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = 0; } if (key == KeyEvent.VK_RIGHT) { dx = 0; } if (key == KeyEvent.VK_UP) { dy = 0; } if (key == KeyEvent.VK_DOWN) { dy = 0; } if (key == KeyEvent.VK_SPACE) { isFired=false; } } } 

KeyListener require that the component they are registered are focusable AND have focus, this means that when you click something like a JButton focus moves to the button and the component with a KeyListener no longer has keyboard focus and therefore will no longer receive key events. KeyListener要求它们注册的组件具有焦点并且具有焦点,这意味着当您单击诸如JButton焦点之类的内容时,会将其移到该按钮上,并且具有KeyListener的组件不再具有键盘焦点,因此将不再接收按键事件。

Instead, it is recommended that you use Key Bindings as this API has the capacity to overcome these limitations 相反,建议您使用按键绑定,因为此API可以克服这些限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM