Hi, ich bin auf ein Problem gestoßen, und zwar
möchte ich das wenn die Kugel an eine bestimmte "Linie" kommt, sie unsichtbar wird.
Aber irgendwie passiert mit der Kugel entweder garnichts, manchmal wird sie unsichtbar und manchmal
zu spät unsichtbar.
Hier die Map Klasse:
Hier meine Kugel Klasse:
Danke schonmal für Hilfe,
Mfg,
Krypthor
möchte ich das wenn die Kugel an eine bestimmte "Linie" kommt, sie unsichtbar wird.
Aber irgendwie passiert mit der Kugel entweder garnichts, manchmal wird sie unsichtbar und manchmal
zu spät unsichtbar.
Hier die Map Klasse:
Java:
public class Map1 extends JPanel implements ActionListener{
Player p;
Player2 p2;
Image img2, p2leben, pleben, p10l, p9l, p8l, p7l, p6l, p5l, p4l, p3l, p2l, p1l, p0l, weapon, weapon2, pflanzen;
Timer time;
int pkill, p2kill;
ImageIcon mp = new ImageIcon(getClass().getResource("Bilder/Leben/MP.png"));
ImageIcon pistol = new ImageIcon(getClass().getResource("Bilder/Leben/Pistol.png"));
ImageIcon plant = new ImageIcon(getClass().getResource("Bilder/Pflanzen.png"));
ImageIcon mp2 = new ImageIcon(getClass().getResource("Bilder/Leben/MP2.png"));
ImageIcon pistol2 = new ImageIcon(getClass().getResource("Bilder/Leben/Pistol2.png"));
public Map1(){
time = new Timer(8, this);
time.start();
p = new Player();
p2 = new Player2();
pkill = 60;
p2kill = 60;
addKeyListener(new AL());
addKeyListener(new AL2());
setFocusable(true);
ImageIcon i = new ImageIcon(getClass().getResource("Bilder/Map1_plaza.png"));
ImageIcon pl10 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben10.png"));
ImageIcon pl9 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben9.png"));
ImageIcon pl8 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben8.png"));
ImageIcon pl7 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben7.png"));
ImageIcon pl6 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben6.png"));
ImageIcon pl5 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben5.png"));
ImageIcon pl4 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben4.png"));
ImageIcon pl3 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben3.png"));
ImageIcon pl2 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben2.png"));
ImageIcon pl1 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben1.png"));
ImageIcon pl0 = new ImageIcon(getClass().getResource("Bilder/Leben/Leben0.png"));
weapon = pistol.getImage();
weapon2 = pistol2.getImage();
pflanzen = plant.getImage();
img2 = i.getImage();
p10l = pl10.getImage(); p9l = pl9.getImage(); p8l = pl8.getImage(); p7l = pl7.getImage(); p6l = pl6.getImage();
p5l = pl5.getImage(); p4l = pl4.getImage(); p3l = pl3.getImage(); p2l = pl2.getImage(); p1l = pl1.getImage(); p0l = pl0.getImage();
p.x = 50;
p.y = 100;
p2.x = 540;
p2.y = 350;
}
public void actionPerformed(ActionEvent e) {
repaint();
checkCollisions();
p.move();
if (p.x <= -1){p.x = p.x + 1;}
if (p.x >= 586){p.x = p.x - 1;}
if (p.y <= 58){p.y = p.y + 1;}
if (p.y >= 382){p.y = p.y - 1;}
if (p.x == 106 && p.y >= 108 && p.y <= 325){p.x = p.x - 1;} //e
if (p.y == 325 && p.x >= 106 && p.x <= 399){p.y = p.y + 1;} //f
if (p.x == 399 && p.y >= 253 && p.y <= 325){p.x = p.x + 1;} //g
if (p.x == 385 && p.y >= 253 && p.y <= 325){p.x = p.x - 1;} //g2
if (p.y == 253 && p.x >= 385 && p.x <= 399){p.y = p.y - 1;} //g3
if (p.y == 302 && p.x >= 106 && p.x <= 399){p.y = p.y - 1;} //h
if (p.x == 132 && p.y >= 178 && p.y <= 304){p.x = p.x + 1;} //i
if (p.y == 182 && p.x >= 130 && p.x <= 148){p.y = p.y + 1;} //j
if (p.x == 147 && p.y >= 180 && p.y <= 246){p.x = p.x - 1;} //k
if (p.x == 161 && p.y >= 107 && p.y <= 246){p.x = p.x + 1;} //k2
if (p.y == 246 && p.x >= 147 && p.x <= 161){p.y = p.y + 1;} //k3
if (p.y == 107 && p.x >= 105 && p.x <= 161){p.y = p.y - 1;} //k4/l
if (p.x == 172 && p.y >= 107 && p.y <= 246){p.x = p.x - 1;} //m
if (p.x == 187 && p.y >= 107 && p.y <= 246){p.x = p.x + 1;} //m2
if (p.y == 107 && p.x >= 172 && p.x <= 187){p.y = p.y - 1;} //m3
if (p.y == 246 && p.x >= 172 && p.x <= 187){p.y = p.y + 1;} //m4
if (p.x == 198 && p.y >= 108 && p.y <= 246){p.x = p.x - 1;} //n
if (p.x == 212 && p.y >= 108 && p.y <= 246){p.x = p.x + 1;} //n2
if (p.y == 107 && p.x >= 198 && p.x <= 486){p.y = p.y - 1;} //n3
if (p.y == 246 && p.x >= 198 && p.x <= 212){p.y = p.y + 1;} //n4
if (p.y == 182 && p.x >= 212 && p.x <= 463){p.y = p.y + 1;} //o
if (p.x == 460 && p.y >= 182 && p.y <= 302){p.x = p.x - 1;} //p
if (p.x == 486 && p.y >= 105 && p.y <= 325){p.x = p.x + 1;} //q
if (p.y == 302 && p.x >= 450 && p.x <= 460){p.y = p.y - 1;} //r
if (p.y == 325 && p.x >= 436 && p.x <= 486){p.y = p.y + 1;} //s
if (p.x == 436 && p.y >= 253 && p.y <= 325){p.x = p.x - 1;} //t
if (p.x == 450 && p.y >= 253 && p.y <= 302){p.x = p.x + 1;} //t2
if (p.y == 253 && p.x >= 436 && p.x <= 450){p.y = p.y - 1;} //t3
if (p.x == 410 && p.y >= 253 && p.y <= 325){p.x = p.x - 1;} //u
if (p.x == 425 && p.y >= 253 && p.y <= 325){p.x = p.x + 1;} //u2
if (p.y == 325 && p.x >= 410 && p.x <= 425){p.y = p.y + 1;} //u3
if (p.y == 253 && p.x >= 410 && p.x <= 425){p.y = p.y - 1;} //u4
if (p.x == 232 && p.y >= 98 && p.y <= 106){p.x = p.x - 1;} //vase1
if (p.x == 254 && p.y >= 98 && p.y <= 106){p.x = p.x + 1;} //vase1
if (p.y == 98 && p.x >= 232 && p.x <= 254){p.y = p.y - 1;} //vase1
if (p.y == 106 && p.x >= 232 && p.x <= 254){p.y = p.y + 1;} //vase1
if (p.x == 346 && p.y >= 98 && p.y <= 106){p.x = p.x - 1;} //vase2
if (p.x == 368 && p.y >= 98 && p.y <= 106){p.x = p.x + 1;} //vase2
if (p.y == 98 && p.x >= 346 && p.x <= 368){p.y = p.y - 1;} //vase2
if (p.y == 106 && p.x >= 346 && p.x <= 368){p.y = p.y + 1;} //vase2
if (p.x == 240 && p.y >= 223 && p.y <= 252){p.x = p.x - 1;} //obst1
if (p.x == 283 && p.y >= 223 && p.y <= 252){p.x = p.x + 1;} //obst1
if (p.y == 223 && p.x >= 240 && p.x <= 283){p.y = p.y - 1;} //obst1
if (p.y == 252 && p.x >= 240 && p.x <= 283){p.y = p.y + 1;} //obst1
if (p.x == 315 && p.y >= 223 && p.y <= 252){p.x = p.x - 1;} //obst2
if (p.x == 358 && p.y >= 223 && p.y <= 252){p.x = p.x + 1;} //obst2
if (p.y == 223 && p.x >= 315 && p.x <= 358){p.y = p.y - 1;} //obst2
if (p.y == 252 && p.x >= 315 && p.x <= 358){p.y = p.y + 1;} //obst2
if (p.x == 229 && p.y >= 325 && p.y <= 330){p.x = p.x - 1;} //vase3
if (p.x == 255 && p.y >= 325 && p.y <= 330){p.x = p.x + 1;} //vase3
if (p.y == 330 && p.x >= 229 && p.x <= 255){p.y = p.y + 1;} //vase3
if (p.x == 343 && p.y >= 325 && p.y <= 330){p.x = p.x - 1;} //vase4
if (p.x == 369 && p.y >= 325 && p.y <= 330){p.x = p.x + 1;} //vase4
if (p.y == 330 && p.x >= 343 && p.x <= 369){p.y = p.y + 1;} //vase4
p2.move();
if (p2.x <= -1){p2.x = p2.x + 1;}
if (p2.x >= 586){p2.x = p2.x - 1;}
if (p2.y <= 58){p2.y = p2.y + 1;}
if (p2.y >= 382){p2.y = p2.y - 1;}
if (p2.x == 106 && p2.y >= 108 && p2.y <= 325){p2.x = p2.x - 1;} //e
if (p2.y == 325 && p2.x >= 106 && p2.x <= 399){p2.y = p2.y + 1;} //f
if (p2.x == 399 && p2.y >= 253 && p2.y <= 325){p2.x = p2.x + 1;} //g
if (p2.x == 385 && p2.y >= 253 && p2.y <= 325){p2.x = p2.x - 1;} //g2
if (p2.y == 253 && p2.x >= 385 && p2.x <= 399){p2.y = p2.y - 1;} //g3
if (p2.y == 302 && p2.x >= 106 && p2.x <= 399){p2.y = p2.y - 1;} //h
if (p2.x == 132 && p2.y >= 178 && p2.y <= 304){p2.x = p2.x + 1;} //i
if (p2.y == 182 && p2.x >= 130 && p2.x <= 148){p2.y = p2.y + 1;} //j
if (p2.x == 147 && p2.y >= 180 && p2.y <= 246){p2.x = p2.x - 1;} //k
if (p2.x == 161 && p2.y >= 107 && p2.y <= 246){p2.x = p2.x + 1;} //k2
if (p2.y == 246 && p2.x >= 147 && p2.x <= 161){p2.y = p2.y + 1;} //k3
if (p2.y == 107 && p2.x >= 105 && p2.x <= 161){p2.y = p2.y - 1;} //k4/l
if (p2.x == 172 && p2.y >= 107 && p2.y <= 246){p2.x = p2.x - 1;} //m
if (p2.x == 187 && p2.y >= 107 && p2.y <= 246){p2.x = p2.x + 1;} //m2
if (p2.y == 107 && p2.x >= 172 && p2.x <= 187){p2.y = p2.y - 1;} //m3
if (p2.y == 246 && p2.x >= 172 && p2.x <= 187){p2.y = p2.y + 1;} //m4
if (p2.x == 198 && p2.y >= 108 && p2.y <= 246){p2.x = p2.x - 1;} //n
if (p2.x == 212 && p2.y >= 108 && p2.y <= 246){p2.x = p2.x + 1;} //n2
if (p2.y == 107 && p2.x >= 198 && p2.x <= 486){p2.y = p2.y - 1;} //n3
if (p2.y == 246 && p2.x >= 198 && p2.x <= 212){p2.y = p2.y + 1;} //n4
if (p2.y == 182 && p2.x >= 212 && p2.x <= 463){p2.y = p2.y + 1;} //o
if (p2.x == 460 && p2.y >= 182 && p2.y <= 302){p2.x = p2.x - 1;} //p
if (p2.x == 486 && p2.y >= 105 && p2.y <= 325){p2.x = p2.x + 1;} //q
if (p2.y == 302 && p2.x >= 450 && p2.x <= 460){p2.y = p2.y - 1;} //r
if (p2.y == 325 && p2.x >= 436 && p2.x <= 486){p2.y = p2.y + 1;} //s
if (p2.x == 436 && p2.y >= 253 && p2.y <= 325){p2.x = p2.x - 1;} //t
if (p2.x == 450 && p2.y >= 253 && p2.y <= 302){p2.x = p2.x + 1;} //t2
if (p2.y == 253 && p2.x >= 436 && p2.x <= 450){p2.y = p2.y - 1;} //t3
if (p2.x == 410 && p2.y >= 253 && p2.y <= 325){p2.x = p2.x - 1;} //u
if (p2.x == 425 && p2.y >= 253 && p2.y <= 325){p2.x = p2.x + 1;} //u2
if (p2.y == 325 && p2.x >= 410 && p2.x <= 425){p2.y = p2.y + 1;} //u3
if (p2.y == 253 && p2.x >= 410 && p2.x <= 425){p2.y = p2.y - 1;} //u4
if (p2.x == 232 && p2.y >= 98 && p2.y <= 106){p2.x = p2.x - 1;} //vase1
if (p2.x == 254 && p2.y >= 98 && p2.y <= 106){p2.x = p2.x + 1;} //vase1
if (p2.y == 98 && p2.x >= 232 && p2.x <= 254){p2.y = p2.y - 1;} //vase1
if (p2.y == 106 && p2.x >= 232 && p2.x <= 254){p2.y = p2.y + 1;} //vase1
if (p2.x == 346 && p2.y >= 98 && p2.y <= 106){p2.x = p2.x - 1;} //vase2
if (p2.x == 368 && p2.y >= 98 && p2.y <= 106){p2.x = p2.x + 1;} //vase2
if (p2.y == 98 && p2.x >= 346 && p2.x <= 368){p2.y = p2.y - 1;} //vase2
if (p2.y == 106 && p2.x >= 346 && p2.x <= 368){p2.y = p2.y + 1;} //vase2
if (p2.x == 240 && p2.y >= 223 && p2.y <= 252){p2.x = p2.x - 1;} //obst1
if (p2.x == 283 && p2.y >= 223 && p2.y <= 252){p2.x = p2.x + 1;} //obst1
if (p2.y == 223 && p2.x >= 240 && p2.x <= 283){p2.y = p2.y - 1;} //obst1
if (p2.y == 252 && p2.x >= 240 && p2.x <= 283){p2.y = p2.y + 1;} //obst1
if (p2.x == 315 && p2.y >= 223 && p2.y <= 252){p2.x = p2.x - 1;} //obst2
if (p2.x == 358 && p2.y >= 223 && p2.y <= 252){p2.x = p2.x + 1;} //obst2
if (p2.y == 223 && p2.x >= 315 && p2.x <= 358){p2.y = p2.y - 1;} //obst2
if (p2.y == 252 && p2.x >= 315 && p2.x <= 358){p2.y = p2.y + 1;} //obst2
if (p2.x == 229 && p2.y >= 325 && p2.y <= 330){p2.x = p2.x - 1;} //vase3
if (p2.x == 255 && p2.y >= 325 && p2.y <= 330){p2.x = p2.x + 1;} //vase3
if (p2.y == 330 && p2.x >= 229 && p2.x <= 255){p2.y = p2.y + 1;} //vase3
if (p2.x == 343 && p2.y >= 325 && p2.y <= 330){p2.x = p2.x - 1;} //vase4
if (p2.x == 369 && p2.y >= 325 && p2.y <= 330){p2.x = p2.x + 1;} //vase4
if (p2.y == 330 && p2.x >= 343 && p2.x <= 369){p2.y = p2.y + 1;} //vase4
ArrayList kugeln = Player.getKugeln();
for (int w = 0; w < kugeln.size(); w++){
Kugel k = (Kugel) kugeln.get(w);
if(k.getVisible() == true){
k.move();
if (k.x <= -1){k.setVisible(false);}
if (k.x >= 586){k.setVisible(false);}
if (k.y <= 58){k.setVisible(false);}
if (k.y >= 382){k.setVisible(false);}
if (k.x == 106 && k.y >= 108 && k.y <= 325){k.setVisible(false);} //e <------------------------Hier die Linie
}
else{
kugeln.remove(w);
}
}
ArrayList kugeln2 = Player2.getKugeln2();
for (int w = 0; w < kugeln2.size(); w++){
Kugel2 k2 = (Kugel2) kugeln2.get(w);
if(k2.getVisible() == true){
k2.move();
}
else{
kugeln2.remove(w);
}
}
if(p.waffe == 1){
weapon = pistol.getImage();
}
if(p.waffe == 2){
weapon = mp.getImage();
}
if(p2.waffe == 1){
weapon2 = pistol2.getImage();
}
if(p2.waffe == 2){
weapon2 = mp2.getImage();
}
}
public void checkCollisions(){
Rectangle r1 = p.getBounds();
Rectangle r2 = p2.getBounds();
ArrayList kugeln = p.getKugeln();
ArrayList kugeln2 = p2.getKugeln2();
if(p2kill == 60){
p2leben = p10l; }
if(p2kill <= 59 && p2kill >= 54){
p2leben = p9l; }
if(p2kill <= 53 && p2kill >= 48){
p2leben = p8l; }
if(p2kill <= 47 && p2kill >= 42){
p2leben = p7l; }
if(p2kill <= 41 && p2kill >= 36){
p2leben = p6l; }
if(p2kill <= 35 && p2kill >= 30){
p2leben = p5l; }
if(p2kill <= 29 && p2kill >= 24){
p2leben = p4l; }
if(p2kill <= 23 && p2kill >= 18){
p2leben = p3l; }
if(p2kill <= 17 && p2kill >= 12){
p2leben = p2l; }
if(p2kill <= 11 && p2kill >= 6){
p2leben = p1l; }
if(p2kill <= 5 && p2kill >= 0){
p2leben = p0l; }
if(pkill == 60){
pleben = p10l; }
if(pkill <= 59 && p2kill >= 54){
pleben = p9l; }
if(pkill <= 53 && p2kill >= 48){
pleben = p8l; }
if(pkill <= 47 && p2kill >= 42){
pleben = p7l; }
if(pkill <= 41 && p2kill >= 36){
pleben = p6l; }
if(pkill <= 35 && p2kill >= 30){
pleben = p5l; }
if(pkill <= 29 && p2kill >= 24){
pleben = p4l; }
if(pkill <= 23 && p2kill >= 18){
pleben = p3l; }
if(pkill <= 17 && p2kill >= 12){
pleben = p2l; }
if(pkill <= 11 && p2kill >= 6){
pleben = p1l; }
if(pkill <= 5 && p2kill >= 0){
pleben = p0l; }
for (int w = 0; w < kugeln.size(); w++)
{
Kugel k = (Kugel) kugeln.get(w);
Rectangle k1 = k.getBounds();
if (k1.intersects(r2))
{
p2kill = p2kill - 6;
k.setVisible(false);
}
else if (k1.intersects(r2))
{
p2kill = p2kill - 1;
}
}
for (int w = 0; w < kugeln2.size(); w++)
{
Kugel2 k2 = (Kugel2) kugeln2.get(w);
Rectangle k3 = k2.getBounds();
if (k3.intersects(r1))
{
p2kill = p2kill - 6;
k2.setVisible(false);
}
else if (k3.intersects(r1))
{
p2kill = p2kill - 1;
}
}
if(p2kill <= 0){
p2tot = true;
System.out.print("Player Tot");
}
if(pkill <= 0){
ptot = true;
System.out.print("Player2 Tot");
}
}
boolean ptot = false;
public boolean pdead(){
return ptot;
}
boolean p2tot = false;
public boolean p2dead(){
return p2tot;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img2, 0, 0, null);
g2d.drawImage(p.getImage(), p.x, p.y, null);
g2d.drawImage(p2.getImage(), p2.x, p2.y, null);
g2d.drawImage(p2leben, 400, 403, null);
g2d.drawImage(pleben, 100, 403, null);
g2d.drawImage(weapon, 20, 400, null);
g2d.drawImage(weapon2, 520, 400, null);
g2d.drawImage(pflanzen, 234, 80, null);
g2d.drawImage(pflanzen, 233, 295, null);
ArrayList kugeln = Player.getKugeln();
for (int w = 0; w < kugeln.size(); w++){
Kugel k = (Kugel) kugeln.get(w);
g2d.drawImage(k.getImage(), k.getX(), k.getY(), null);
}
ArrayList kugeln2 = Player2.getKugeln2();
for (int w = 0; w < kugeln2.size(); w++){
Kugel2 k2 = (Kugel2) kugeln2.get(w);
g2d.drawImage(k2.getImage(), k2.getX(), k2.getY(), null);
}
}
private class AL extends KeyAdapter{
public void keyReleased(KeyEvent e){
p.keyReleased(e);
}
public void keyPressed(KeyEvent e){
p.keyPressed(e);
}
}
private class AL2 extends KeyAdapter{
public void keyReleased(KeyEvent e){
p2.keyReleased(e);
}
public void keyPressed(KeyEvent e){
p2.keyPressed(e);
}
}
}
Hier meine Kugel Klasse:
Java:
public class Kugel {
int y, x;
Image imgk;
boolean visible;
private int richtung = 0;
public Kugel(int startX, int startY, int richtungAmAnfang){
x = startX;
y = startY;
richtung = richtungAmAnfang;
ImageIcon neueKugel = new ImageIcon(getClass().getResource("Bilder/Kugel.png"));
imgk = neueKugel.getImage();
visible = true;
}
public Rectangle getBounds(){
return new Rectangle(x, y, 10, 10);
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public boolean getVisible(){
return visible;
}
public Image getImage(){
return imgk;
}
public void setVisible(boolean isVisible){
visible = isVisible;
}
public void move(){
if(this.richtung == 3){
y = y + 10;
}
if(this.richtung == 4){
x = x - 10;
}
if(this.richtung == 1){
y = y - 10;
}
if(this.richtung == 2){
x = x + 10;
}
if(x > 600){
visible = false;
}
if(y > 400){
visible = false;
}
if(x < 2){
visible = false;
}
if(y < 2){
visible = false;
}
}
}
Danke schonmal für Hilfe,
Mfg,
Krypthor