-4
I have a problem related to a game that I’m creating where the character drops a bomb in the position that he is in. In the first execution of the method bomba()
execution goes as ordered.
However, in the other executions of the method it only executes a part of the method.
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
new Game();
}
}
class Personagem extends JLabel{
public Personagem(int x ,int y) {
player(x,y);
}
ImageIcon persoImg = new ImageIcon(getClass().getResource("personagem.gif"));
public JLabel personagem = new JLabel(persoImg);
public void player(int x,int y) {
personagem.setBounds(x, y, 100, 100);
}
}
class Game extends JFrame {
public Game() {
bombas.carregarbomba();
movimento();
componentes();
janela();
}
int y = 300;
int x = 400;
int contador = 0;
public Personagem perso = new Personagem(x, y);
public Bomba bombas = new Bomba();
ImageIcon bombaImg = new ImageIcon(getClass().getResource("bomba.gif"));
public JLabel bombfoto = new JLabel(bombaImg);
public JTextField text = new JTextField(":" + contador);
private JPanel contentPane;
public void componentes() {
Font font = new Font("Courier", Font.BOLD, 25);
bombfoto.setBounds(0, 0, 50, 50);
text.setBounds(60, 0, 50, 50);
}
public void janela() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(150, 100, 1000, 700);
contentPane = new JPanel();
contentPane.setLayout(null);
componentes();
add(bombas.bombaJLabel[bombas.getCont()]);
add(perso.personagem);
setVisible(true);
}
public void movimento() {
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char movimento = e.getKeyChar();
if (movimento == 'w') {
y -= 20;
}
if (movimento == 's') {
y += 20;
}
if (movimento == 'd') {
x += 20;
}
if (movimento == 'a') {
x -= 20;
}
if (movimento == 'e') {
bombas.bomba(x, y);
bombas.setCont(contador);
contador++;
}
perso.personagem.setBounds(x, y, 100, 100);
}
});
}
}
class Bomba extends JLabel {
ImageIcon bombaImg = new ImageIcon(getClass().getResource("bomba.gif"));
ImageIcon explosao = new ImageIcon(getClass().getResource("explosao.gif"));
public int cont = 0;
public JLabel[] bombaJLabel = new JLabel[8];
int xbomb = 0;
int ybomb = 0;
public void carregarbomba() {
for (int i = 0; i < bombaJLabel.length; i++) {
bombaJLabel[i] = new JLabel(bombaImg);
bombaJLabel[i].setSize(50, 50);
bombaJLabel[i].setVisible(false);
}
}
public void bomba(int x, int y){
try {
xbomb = x + 30;
ybomb = y + 30;
bombaJLabel[cont].setLocation(xbomb, ybomb);
bombaJLabel[cont].setVisible(true);
if (bombaJLabel[cont].isVisible()) {
ActionListener detonar = new ActionListener() {
public void actionPerformed(ActionEvent e) {
bombaJLabel[cont].setIcon(explosao);
bombaJLabel[cont].setBounds(xbomb -= 180, ybomb -= 300, 400, 400);
ActionListener duracaoexplocao = new ActionListener() {
public void actionPerformed(ActionEvent e) {
bombaJLabel[cont].setVisible(false);
}
};
javax.swing.Timer timer = new javax.swing.Timer(1200, duracaoexplocao);
timer.setRepeats(false);
timer.start();
}
};
javax.swing.Timer timer2 = new javax.swing.Timer(2000, detonar);
timer2.setRepeats(false);
timer2.start();
}
}
catch(ArrayIndexOutOfBoundsException ex) {}
}
public int getCont() {
return cont;
}
public void setCont(int cont) {
this.cont = cont;
}
}
copied a link to github with the full project
– Donztt
Dude, think about who’s gonna help you, you think it’s cool to force a guy to have to go to an outside link to access your code and help you? And who can’t access this link? Therefore, it is important and recommended that you provide a [mcve] and add it to the question by clicking [Edit].
– user28595
@Donztt As Articuno said, the ideal is that all the code needed to reproduce the problem is in the question, and links are just complements. Links can stay off the air or change (and until blocked by corporate firewalls, depending on the link and the company) and then whoever tries to help you will not have the complete information. If the original code is too large, try to reduce it and create a [mcve] <-- read this page, there are several tips on how to fix your code to post it here.
– hkotsubo
I put the main part of the problem,
– Donztt
@Donztt your code is a [mcve]? If I paste in my IDE, will it perform? Because if it is not, it is no use pasting the entire code. I recommend that you visit the link that both hkotsubo and I suggested and try to create an example that anyone can test, without testing it is difficult to help you.
– user28595
I believe that perform the problem as to this is in relation to the images that the program uses
– Donztt
Then you understand that it is not possible for others to execute. How will we help you if we cannot execute? Again, I recommend you go to the link to learn how to turn your code into a [mcve] so that it is possible to test and help.
– user28595
I’m looking at your code and I know I can go in there on github and get the rest because what was posted here is incomplete. However, you have not described what your problem is. What is the method for which you need help? What is the problem you are having? What’s wrong with that code?
– Victor Stafusa
now just copy and paste that’s working
– Donztt
I’m looking at your code. I think you’ve been forgetting a few keywords
static
.– Victor Stafusa
I’m not used to creating everything in a class and ended up forgetting even kk
– Donztt
I am writing an answer to your question. When you finish I post it.
– Victor Stafusa