Hallo liebes Forum, da ihr mir bis jetzt immer so super helfen konntet, dachte ich ich frag einfach nochmal 
Ich habe in meinem Script ein "public void wand" erstellt damit ich ohne immer 30 Zeilen schreiben zu müssen wände erstellen kann.
Das Problem ist: Die Konsole gibt nur Fehlermeldungen raus.
Es liegt an der aufforderung "wand();"
jetzt meine Frage: Wie muss ich es richtig machen?
Liebe Grüße!
z.135 mein "public void"
z.179 mein "wand();"
Darunter 3 Wände per "Hand" erstellt, welche auch 1A funktionieren.
Ich habe in meinem Script ein "public void wand" erstellt damit ich ohne immer 30 Zeilen schreiben zu müssen wände erstellen kann.
Das Problem ist: Die Konsole gibt nur Fehlermeldungen raus.
Es liegt an der aufforderung "wand();"
jetzt meine Frage: Wie muss ich es richtig machen?
Liebe Grüße!
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Move {
private JFrame frame;
int time;
int x;
int y;
int raumx=250;
int raumy=250;
int schrit=25;
int portx;
int porty;
int newport=0;
int score=0;
int move=0;
int speed=0;
int wall1x=3;
int wall1y=4;
int wall2x=3;
int wall2y=3;
int wall3x=2;
int wall3y=3;
int stopl=0;
int stopr=0;
int stopu=0;
int stopd=0;
Graphics2D g2d;
public Move() {
frame = new JFrame("Catch the ball!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
MovePanel panel = new MovePanel();
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
if(move==0&&stopu==0){
y-=1;
move=1;
}
stopd=0;
stopl=0;
stopr=0;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if(move==0&&stopd==0){
y+=1;
move=1;
}
stopu=0;
stopl=0;
stopr=0;
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if(move==0&&stopl==0){
x-=1;
move=1;
}
stopd=0;
stopu=0;
stopr=0;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if(move==0&&stopr==0){
x+=1;
move=1;
}
stopd=0;
stopl=0;
stopu=0;
}
else if (e.getKeyCode() == KeyEvent.VK_X) {
if(speed==0){
speed=1;
} else if(speed==1){
speed=0;
}
}
if (x==raumx/schrit){
x=raumx/schrit-1;
}
if (y==raumy/schrit){
y=raumy/schrit-1;
}
if (x==-1){
x=0;
}
if (y==-1){
y=0;
}
frame.repaint();
}
});
frame.add(panel);
Timer look = new Timer(100, panel);
look.setRepeats(true);
look.start();
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Move();
}
class MovePanel extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private int counter;
public MovePanel() {
super();
counter = 0;
}
public Dimension getPreferredSize() {
return new Dimension(raumx, raumy);
}
public void wand(int wallx, int wally){
if (wallx==x-1 && wally==y){
stopl=1;
}
if (wallx==x+1 && wally==y){
stopr=1;
}
if (wallx==x && wally==y-1){
stopu=1;
}
if (wallx==x && wally==y+1){
stopd=1;
}
if (wallx==portx && wally==porty){
newport=0;
}
g2d.setColor(new Color(Integer.parseInt("000000", 16)));
g2d.fillRect(wallx*schrit, wally*schrit, schrit, schrit);
}
public void paintComponent(Graphics g) {
int boxX=schrit*x;
int boxY=schrit*y;
int boxU=schrit;
int maxX=raumx/25-1;
int maxY=raumy/25-1;
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(Integer.parseInt("7CFC00", 16)));
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
if (newport==0){
portx=(int)(Math.random()*maxX);
porty=(int)(Math.random()*maxY);
newport=1;
} else if (portx==x && porty==y){
newport=0;
score+=1;
System.out.println(score);
repaint();
}
wand(2, 3);
//Eine Wand
if (wall1x==x-1 && wall1y==y){
stopl=1;
}
if (wall1x==x+1 && wall1y==y){
stopr=1;
}
if (wall1x==x && wall1y==y-1){
stopu=1;
}
if (wall1x==x && wall1y==y+1){
stopd=1;
}
if (wall1x==portx && wall1y==porty){
newport=0;
repaint();
}
g2d.setColor(new Color(Integer.parseInt("000000", 16)));
g2d.fillRect(wall1x*schrit, wall1y*schrit, schrit, schrit);
//Eine Wand
if (wall2x==x-1 && wall2y==y){
stopl=1;
}
if (wall2x==x+1 && wall2y==y){
stopr=1;
}
if (wall2x==x && wall2y==y-1){
stopu=1;
}
if (wall2x==x && wall2y==y+1){
stopd=1;
}
if (wall2x==portx && wall2y==porty){
newport=0;
repaint();
}
g2d.setColor(new Color(Integer.parseInt("000000", 16)));
g2d.fillRect(wall2x*schrit, wall2y*schrit, schrit, schrit);
//Eine Wand
if (wall3x==x-1 && wall3y==y){
stopl=1;
}
if (wall3x==x+1 && wall3y==y){
stopr=1;
}
if (wall3x==x && wall3y==y-1){
stopu=1;
}
if (wall3x==x && wall3y==y+1){
stopd=1;
}
if (wall3x==portx && wall3y==porty){
newport=0;
repaint();
}
g2d.setColor(new Color(Integer.parseInt("000000", 16)));
g2d.fillRect(wall3x*schrit, wall3y*schrit, schrit, schrit);
//Eine Wand
if(move==0){
g2d.setColor(new Color(Integer.parseInt("FFB90F", 16)));
} else if (move==1){
g2d.setColor(new Color(Integer.parseInt("D60000", 16)));
}
g2d.fillRect(boxX, boxY, boxU, boxU);
g2d.setColor(new Color(Integer.parseInt("000000", 16)));
g2d.fillOval(portx*25, porty*25, schrit, schrit);
}
public void actionPerformed(ActionEvent e) {
setState(counter);
counter = (counter % 10) + 1;
}
private void setState(int i) {
if(move==1){
if(i %2 == 1) {
time+=1;
}
else {
time+=1;
}
}
if(time == 3&& speed==1){
time=0;
move=0;
}
if(time == 5&& speed==0){
time=0;
move=0;
}
repaint();
}
}
}
z.135 mein "public void"
z.179 mein "wand();"
Darunter 3 Wände per "Hand" erstellt, welche auch 1A funktionieren.
Zuletzt bearbeitet: