由于某种原因,我的敌人计时器不能与英雄(关键听众)一起工作。我试图从面板上移除敌人按钮,它正常工作。 我注意到,如果英雄在僵尸/敌人的道路上,那么没有任何作用。我希望他们彼此面对,这样我才能碰撞。
我正在努力,我意识到我必须点击英雄,然后我可以移动他。
我正在为学校项目这样做。
public class game{
static Timer zom;
static int leftright = 0;
static int updown = 3;
static ImageIcon evilcup = new ImageIcon("C:\\Users\\asher\\Pictures\\zombie-1.jpg");
static JButton enemy = new JButton (evilcup);
public static JFrame myframe = new JFrame("Asher's Game Database");
public static JPanel mypanel = new JPanel();
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
int x = 0, y = 0;
myframe.setSize(1900, 600);
myframe.setDefaultCloseOperation(myframe.EXIT_ON_CLOSE);
myframe.setLocationRelativeTo(myframe);
// all about the panel
mypanel.setBackground(Color.white);
mypanel.setLayout(null);
ImageIcon herocup = new ImageIcon("C:\\Users\\asher\\Pictures\\soldier1.jpg");
final JButton action = new JButton (herocup);
// REMOVING BACKGROUND FOR ACTION
action.setContentAreaFilled(false);
action.setBorderPainted(false);
action.setFocusPainted(false);
// REMOVING BACKGROUND FOR ENEMY
enemy.setContentAreaFilled(false);
enemy.setBorderPainted(false);
enemy.setFocusPainted(false);
// FIRST TIMER FOR THE EVIL ZOMBIE **************************
final Timer zom = new Timer(5, new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int currentx, currenty, newx,newy;
leftright = leftright + 1;
String evil = "C:\\Users\\asher\\Pictures\\mario" + leftright + ".gif"; //declearing pic and adding numbers so we dont have to repeate
ImageIcon evilcup = new ImageIcon(evil); //create new pic using newpic
enemy.setIcon(evilcup);
if (leftright == 3)
leftright =0;
// COORDINATES OF THE EVIL
currentx = enemy.getLocation().x;
currenty = enemy.getLocation().y;
newx = currentx + 15;
newy = currenty;
// IF THE OBJECT REACHS TEH END OF THE FRAME BLOCK HIS WAY!!!!
if (newx >1700)
newx = 1700;
enemy.setLocation(newx, newy);
}// end of sub method
});// for timechange
//SECOND TIMER FOR DELAYING -------------------------------------------
final Timer secondtime = new Timer(2500, new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
zom.setDelay(100);
}//end of method
});// ending of the seconding timer
secondtime.start();
//THIRD TIMER FOR STOPPING-------------------------------------------
if (secondtime.isRunning()){
final Timer thirdtime = new Timer(5000, new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
zom.stop();
secondtime.stop();
}//end of method
});//end of third timer
}
// KEY LISTENER ----------------------------
action.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int currentx, currenty, newx,newy;
// SETTING UP 'C' FOR NUMBERS IN GETTING CODE FOR THE KEYS
int c = e.getKeyCode();
System.out.print (e.getKeyCode());
System.out.print (e.getKeyChar()+ " = ");
if (e.getKeyCode()==38)
zom.start();
//end of if for keycode 38
// _____________________________________________________________
if (e.getKeyCode()==37)
// GO LEFT *********************
{
leftright = leftright -1;
String heromove = "C:\\Users\\asher\\Pictures\\mario" + leftright + ".gif"; //declearing pic and adding numbers so we dont have to repeate
ImageIcon cup5 = new ImageIcon(heromove); //create new pic using newpic
action.setIcon(cup5);
if (leftright == -3)
leftright =0;
currentx = action.getLocation().x;
currenty = action.getLocation().y;
newx = currentx - 15;
newy = currenty;
// IF THE OBJECT REACHS TEH END OF THE FRAME BLOCK HIS WAY!!!!
if (newx <0)
newx = 0;
//STEP 3 : set new location
action.setLocation(newx, newy);
}//end of if for keycode 37
// _____________________________________________________________
if (e.getKeyCode()==39)
// GO RIGHT **********************
{
leftright = leftright +1;
String heromove = "C:\\Users\\asher\\Pictures\\mario" + leftright + ".gif"; //declearing pic and adding numbers so we dont have to repeate
ImageIcon cup5 = new ImageIcon(heromove); //create new pic using newpic
action.setIcon(cup5);
if (leftright == 3)
leftright =0;
currentx = action.getLocation().x;
currenty = action.getLocation().y;
newy = currenty;
newx = currentx + 15;
// IF THE OBJECT REACHS TEH END OF THE FRAME BLOCK HIS WAY!!!!
if (newx >1700)
newx = 1700;
action.setLocation(newx, newy);
}//end of if for keycode 38
// _____________________________________________________________
}//end of the listener
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent d) {
// TODO Auto-generated method stub
}
});//end of the keylistener
// setting up components and bounds
myframe.add(mypanel);
mypanel.add(action);
mypanel.add(enemy);
action.setBounds(1600,200, 100, 200);
enemy.setBounds(40,200, 150, 250);
myframe.setVisible(true);
}//end of method
} //班级的结尾