Kugeln werden nicht unsichtbar

Krypthor

Aktives Mitglied
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:

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
 
Java:
   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
ja wer das nicht sieht ;-)
 
Das ist die Bedingung für den Spieler wo im Level er hin kann und wo nicht^^?!
Das hab ich mit der Kugel genauso versucht aber es klappt ja nicht.
Java:
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);} //Das hier ist die Linie wo k nicht drüber kann, bzw. wo sie unsichtbar bei Berührung werden soll.
 
Die Kugel ist mehr damit beschäftigt diese krude If Struktur sich durchzulesen, die hat gar keine Zeit mehr unsichtbar zu werden...

Außerdem glaube ich, dass da nach setVisible(false) auch noch ein repaint() folgen muss.
 
Ich habs ehrlich versucht nachzuvollziehen, aber ohne es auszuprobieren, ist es bei dem - so muss man wohl sagen - schlechten Quelltext schwer möglich was zu finden.

Würdest du deinen ganzen Quelltext + Bilder hier in einem zip-ordner hochladen, würde ich es ausprobieren. Habe keine Lust alle Bilder selbst zu erzeugen.

Schonmal ein paar Anmerkungen:
Für die nicht begehbaren Bereiche irgendwie Objekte erzeugen und nicht hunderte ifs...,
Code:
ImageIcon neueKugel = new ImageIcon(getClass().getResource("Bilder/Kugel.png"));
So lädst du für jede Kugel das Bild (es sei denn es wird automatisch gecached), lade das Bild am Anfang einmal. Zur Not in einer statischen Variablen.

Code:
boolean getVisible(){
Boolean-getter benennt man meistens: isVisible(). Ich dachte erst daran, die Methode würde sichtbar machen^^
 
Hallo,

da Du nun selber erkannt hast, dass diese Art der programmierung weder wartbar noch wirklich geil ist:

Benutze für den Anfang ein Array:

Code:
int[][] map = new int[100][100];

Nun kannst du Zahlencodierungen machen:

0 = leer
1 = kugel
2 = spieler
4 = block

---

nun kannst du ein Objekt Kugel erschaffen, dass sich "bewegt" (Move(x,y)) und zwar relativ zu ihrer aktuellen Position.

Die Kugel kennt die Karte und kann nun schauen auf dem feld x + move_x und y + move_y sich befindet.

Wenn 2 dann spieler tod
wenn 4, dann "werde unsichtbar?"

Das kann man nun beliebig komplex ausbauen.

Gruß,

Martin
 
okay danke, aber könntest du mir nen gefallen tun und das alles in einen code zusammenfassen(also sozusagen in die Klasse "Map")? Weil ich weis leider noch nicht genau wo ich was hinpacken muss und wie der erkennt, wo das jetzt auf meinem
Bild von der Map ist.

Was genau zeigt das [100][100] an? Ist es die Anzahl an Zahlen oder wird dadurch ein 100*100 quadraht erzeugt?

Entschuldige bitte meine ganzen Fragen, aber ich hab noch nirgendwo ein gutes (und für mich verständliches) Tutorial gefunden.
Wäre echt nett wenn du einfach ein kleinen Beispiel in Form einer Klasse machen könntest, denn so lern und versteh ichs am schnellsten.

Mfg,

Krypthor
 
Bisher ist nicht mal klar, wofür... Der Code sieht aus, als hätte jemand versucht, "so lange irgendwelchen Code hinzuschreiben bis irgendwas cooles rauskommt", aber WAS genau du machen willst, hast du noch nicht beschrieben...
 
Ich brauchte einfach ein Beispiel für den Leveleditor in Form einer Klasse,
das Level ist egal, denn mir ist einfach nicht klar, wie man "Wände" machen kann.
Vorher habe ich das immer mit if Bedingungen gemacht und das war viel zu umständlich.
Ich hab das mal undgefähr so gesehen:
Java:
final short level[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

und ich weis das das die Positionen auf der Map sind auf denen sich der Spieler bewegen/nicht bewegen darf.
Allerdings weis ich nicht wie man das macht, dass der Spieler sich jetzt auch nicht drüberbewegt.
Welcher Befehl gibt das genau an?
Das gleiche wäre interessant zu wissen ob und wie das mit zwei Bildern funktioniert.
Also wenn der Spieler z.B. ein anderes Bild berührt (mit intersects) das er das nicht überqueren
kann, bzw ein Stück zurückgesetzt wird.

Ich hoffe ich konnte so ein bisschen beschreiben worin mein Problem liegt.
Ein kleinen Beispiel würde mir sehr helfen.

Vielen Dank schonmal,
Krypthor
 
Hallo,

da haste doch schonmal ein Feld.

Und löse erst ein Problem, dann kommt das mit den Bildern auch schon.

So nun fügst du in dein array eine 2 ein. Das ist dein Spieler.

Und nun sags du dem spieler gehe eins nach rechts. Wie geht das?

Du addierst 1 zu einer x koordinate und schaust, was auf dem nächsten feld ist: Wenn 1 dann geht nicht.
 
Wie ist der Code für "wenn 1 dann geht das nicht" 😀 ?
Und wenn ich das so mache dann bewegt sich der Charakter doch immer so ruckartig, also immer feld für feld wie eine Schachfigur?!
 
Java:
if(neuesFeld == 1)
   geht nicht //
else {
   move to new field.
}
}

Du solltest einen Schritt nach dem anderen machen. Wenn du diese Basics beherscht, dann kannst du auch die übergäge zwischen den Feldern animieren.

Gruß,

Martin
 
Die Map hätte ich jetzt so vom Aufbau her gemacht, allerdings weis ich noch nicht wie man das alles jetzt miteinander verbindet,
Bei "checkCollisions" muss ja dann die if abfrage der Zahlen rein, aber wie und wo setz ich 2 als die Player Startposition?
Und wie lässt man den Spieler zeichnen, auf den verschiedenen Zahlen?
Java:
public class Interface extends JPanel implements ActionListener{
Player p;
Image img2, img3;
Timer time;
final short level[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
  0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

	public Interface(){
		time = new Timer(8, this); 
		time.start();
		addKeyListener(new AL());
		setFocusable(true);
		ImageIcon i = new ImageIcon(getClass().getResource("Bilder/Unbenannt.png"));
		img2 = i.getImage();
	}
	
	public void actionPerformed(ActionEvent e) {
		repaint();
		checkCollisions();
		p.move();
	}
	
	public void checkCollisions(){
	}
	
	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(img3, ?, ?, null);
	}
	
	private class AL extends KeyAdapter{
		public void keyReleased(KeyEvent e){
			p.keyReleased(e);
		}
		public void keyPressed(KeyEvent e){
			p.keyPressed(e);
		}
	}
}
 
Schreib' dir ein Programm, mit dem du ein Bild anzeigen kannst.
Wenn das funktioniert, schmeiß' es in die Mülltonne.
Schreib' dir ein Programm, wo du Tasteneingaben abfragst.
Wenn das funktioniert, schmeiß' es in die Mülltonne.
Schreib dir ein Programm, wo du mit den Pfeiltasten ein Bild bewegen kannst.
Wenn das funktioniert, schmeiß' es in die Mülltonne.
Schreib dir ein Programm wo du eine TXT-Datei zeilenweise einliest.
Wenn das funktioniert, schmeiß' es in die Mülltonne.
Schreib' dir ein Programm, wo du ein Raster aus verschiedenen Bildern anhand des Inhalts einer TXT erstellst.
Wenn das funktioniert, schmeiß' es in die Mülltonne.
Zwischendurch lange und viel nachdenken.
Und dann überleg' nochmal, wie du das strukturieren könntest, was du jetzt durch das Hinschreiben von ganz viel Quellcode erreichen wolltest. Ob das ein Leveleditor oder ein Spiel werden sollte, ist noch nicht ganz klar.

Ebensowenig ist mir (und wohl auch anderen) klar, welche Form von Hilfe du hier erwartest. Du kannst nach allgemeinen Ansätzen fragen, oder nach konkreten Lösungen für konkrete Probleme. Aber ein paar hundert Zeilen zusammengestümperten Quäl-Kot posten, und dreist die Forderung stellen: "Joa, postet doch mal richtigen Code, der mein Problem löst (aber welches das ist, sag' ich nicht)" führt sicher nicht zum gewünschten Erfolg.
(Warst du mal unter dem Namen "DeveloperX" hier registriert?)
 
Warst du mal unter dem Namen "DeveloperX" hier registriert?
Nein, wieso? Das ist mein erster Account in diesem Forum.

Ich erwarte nicht das ihr mir all die Arbeit abnehmt und für mich irgendein Programm schreibt was ich nicht hinbekomme, was du aufgelistet hast kann ich alles bis auf das Letzte, ich habe bisher nur einfach kein gescheites Tutorial gefunden wo der gesamte Aufbau des Leveleditors mal einfach beschrieben ist, ich hab mich echt ewig mit diesem Beispiel auseinandergesetzt :
Pacman
aber ich verstehe einfach noch nicht ganz was wo und wie gemacht wird, der gesamte Code ist mir noch etwas zu unübersichtlich, deswegen hatte ich nur gehofft, dass mir jemand einfach ein ganz kurzes Aufbaubeispiel zeigen kann, damit ich es verstehe.

Tut mir leid das es etwas falsch rübergekommen ist, mir würde es auch reichen wenn mir jemand einen Link mit einem guten Tutorial schickt.

Trotzdem vielen Dank,

Krypthor
 
Lese dir alle Tutorials auf der Seite der reihe nach durch. Pacman ist eins der letzteren und schwereren (relativ gesehen natürlich)

Sich in fremden Code rein versetzen ist immer etwas müheselich, besonders als Anfänger und dort auf der Seite weil da nur sehr wenig erklärt wird, aber es ist trotzdem verständlich wenn man die Basics intus hat. Wenn das nicht der Fall ist, solltest du vllt Java lernen, bevor du in Java etwas realisieren möchtest ?!

Von oben nach unten...!

The Java 2D games tutorial

Grüße
 
Mit dem Levelaufbau hab ich das jetzt endlich hinbekommen 😉
Das Problem was ich noch habe ist, wie man den Spieler jetzt bewegt, bzw. wie man in
der Klasse Spieler die aktuelle char Position verändert.

Hier ist meine bisherige Klasse Spieler:
Java:
public class Player{
	int x, y, dx, dy, nx, nx2, left, richtungs;
	int richtung, magazin, magazinMP, waffe, richtungy, richtungx;
	Image still;
	ImageIcon i = new ImageIcon("Spielersteht.png");
	boolean löschen;
	Map map;
	int m, n, ny, mx;
	char[][] chaar = new char[m][n];
	
	public Player(){
		still = i.getImage();
		mx = richtungx;
		ny = richtungy;
	}
	
	public int Richtung(){
		return richtung;
	}
	public Image getImage(){
		return still;
	}

	public void keyPressed(KeyEvent e){
		int key = e.getKeyCode(); 
	
		if (key == KeyEvent.VK_A){
			richtungx = - 1;
			richtungy = 0;
		}
		if (key == KeyEvent.VK_D){
			richtungx = + 1;
			richtungy = 0;
		}
		if (key == KeyEvent.VK_W){
			richtungy = 0;
			richtungx = - 1;
		}
		if (key == KeyEvent.VK_S){
			richtungy = 0;
			richtungx = + 1;
		}
	}

	public void keyReleased(KeyEvent e){
		int key = e.getKeyCode(); 
	
		if (key == KeyEvent.VK_A){
			richtungx = 0;
			richtungy = 0;
		}
		if (key == KeyEvent.VK_D){
			richtungx = 0;
			richtungy = 0;
		}
		if (key == KeyEvent.VK_W){
			richtungy = 0;
			richtungx = 0;
		}
		if (key == KeyEvent.VK_S){
			richtungy = 0;
			richtungx = 0;
		}
		System.out.println(x + "=x ");
		System.out.println(y + "=y ");
	}
}

Und hier meine Map:
Java:
public class Map extends JPanel implements ActionListener{
Player p;
Timer time;
char[][] array;
ImageIcon i1 = new ImageIcon("Kugel.png");
Image img = i1.getImage();
int m, n;

	public Map(){
		time = new Timer(8, this); 
		time.start();
		p = new Player();
		addKeyListener(new AL());
		setFocusable(true);
	}
	
	public char[][] initArray(){
		char[][] ch = new char[5][5];
		ch[0][0] = '-';ch[1][0] = '-';ch[2][0] = '-';ch[3][0] = '-';ch[4][0] = '-';
		ch[0][1] = '|';ch[1][1] = ' ';ch[2][1] = ' ';ch[3][1] = ' ';ch[4][1] = '|';
		ch[0][2] = '|';ch[1][2] = ' ';ch[2][2] = ' ';ch[3][2] = ' ';ch[4][2] = '|';
		ch[0][3] = '|';ch[1][3] = ' ';ch[2][3] = ':';ch[3][3] = ' ';ch[4][3] = '|';
		ch[0][4] = '-';ch[1][4] = '-';ch[2][4] = '-';ch[3][4] = '-';ch[4][4] = '-';
		return ch;
	}
	
	public void actionPerformed(ActionEvent e) {
		repaint();
		playerMove();
	}
	
	public void playerMove(){
		for(int n=0 ; n < 5; n++){
			for(int m=0; m < 5; m++){
				
				if(array[m][n] == ' '){
					
				}
				if(array[m][n] == '|'){
					
				}
				if(array[m][n] == ' '){
					
				}
			}
		}
	}
	public void init(){
		setSize(200, 200);
	}
	public void paint(Graphics g) {
		
		super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			array = initArray();
			// n = Zeilen
			// m = Spalten
			for(int n=0 ; n < 5; n++){
				for(int m=0; m < 5; m++){
					
					if(array[m][n] == '-'){
						g2d.drawImage(img, 20*m, 20*n, null);
					}
					if(array[m][n] == '|'){
						g2d.drawImage(img, 20*m, 20*n, null);
					}
					if(array[m][n] == ':'){
						g2d.drawImage(p.getImage(), 20*m, 20*n, null);
					}
					
				}
			}
	}
	
	private class AL extends KeyAdapter{
		public void keyReleased(KeyEvent e){
			p.keyReleased(e);
		}
		public void keyPressed(KeyEvent e){
			p.keyPressed(e);
		}
	}
}

Ich hatte mir das so gedacht, dass wenn man die Taste "A" beispielsweise drückt, in der Map Klasse abgefragt wird ob sich neben ihm ein "begehbares Feld" befindet und welche Taste gedrückt wurde. Hat aber nicht geklappt, wahrscheinlich weil ich die neue Position falsch angegeben hatte?!
Und wie wird der gezeichnete Spieler von der ":" Startposition gelöscht?
 

Neue Themen


Zurück
Oben