So, I've tried to learn how to use methods so I dont have to use the same script to handle something in vain. But
what I'm having problems with is returning a variable.
package javagametest; import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Color; public class JavaGameTest{ public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ createAndShowGUI(); } }); } public static void createAndShowGUI(){ JFrame f = new JFrame("JFrame test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new myPanel()); f.pack(); f.setVisible(true); } } class myPanel extends JPanel{ //Global static int Room_Width = 966; static int Room_Height = 600; //The red box variables private double RedBoxX = (int) (Math.random() * 640); private double RedBoxY = (int) (Math.random() * 480); private int RedBoxW = 32; private int RedBoxH = 32; private int RedBoxVert = 1; private int RedBoxHoro = 1; private double RedBoxSpeed = 0.3; public myPanel(){ } private void MoveObject(){ switch (RedBoxVert) { case 0: RedBoxY-=RedBoxSpeed; break; case 1: RedBoxY+=RedBoxSpeed; break; } switch (RedBoxHoro) { case 0: RedBoxX-=RedBoxSpeed; break; case 1: RedBoxX+=RedBoxSpeed; break; } } static int checkCollisionHOROSONTAL(double x, double y, int horo, int width){ if ((x + width >= Room_Width)) {horo = 0;} if ((x <= 0)) {horo = 1;} return horo; } static int checkCollisionVertical(double x, double y, int vert1, int height){ if ((x + height >= Room_Height)) {vert1 = 0;} if ((x <= 0)) vert1 = 1; return vert1; } @Override public Dimension getPreferredSize(){ return new Dimension(Room_Width,Room_Height); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); { super.paintComponent(g); MoveObject(); checkCollisionHOROSONTAL(RedBoxX, RedBoxY, RedBoxHoro, RedBoxW); checkCollisionVertical(RedBoxX, RedBoxY, RedBoxVert, RedBoxH); int RedBoxXINT = (int)RedBoxX; int RedBoxYINT = (int)RedBoxY; g.setColor(Color.RED); g.fillRect(RedBoxXINT, RedBoxYINT, RedBoxW, RedBoxH); g.setColor(Color.BLACK); g.drawRect(RedBoxXINT, RedBoxYINT, RedBoxW, RedBoxH); repaint(); } } }
I want the "Return vart1" to indicate if the variable RedBoxVert should be on or off (as you can see in the paramaters " checkCollisionVertical(RedBoxX, RedBoxY, RedBoxVert, RedBoxH);" ), but it's not working.
I've also tryed to use public int instead of static int, but I have no clue on what I should do.
I would really appreciate if someone could help!