Hello Guys!
I have to make a Rush hour game for my project. The basic idea of Rush hour is to "Unstuck" you car and get it to the exit by moving other vehicles out of the way. My problem is that I want to create multiple vehicles of the same type ( By using a factory ) and have them start at different X or Y positions depending on the vehicle. As an example I will use a "Bus":
rushhour_example.jpg
Now, the idea is that the bus can ONLY move in the Y directions and is stuck to it's X position. The problem is, because I use the same factory for the busses they snap to the same X position as soo as I start moving them ( which at the moment is x = 0 ). So what I want them to do is stay at the x position they are at while I a moving the Y direction. Here is my code for the bus: (All my methods, classes etc. are in Dutch, sorry for that)
package RushHourSpel; import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public class Bus extends SpelFiguren implements MouseMotionListener { private int clickedY, clickedX; public Bus(int x, int y) { this.setSize(lengthBusX, lengthBusY); this.setBackground(Color.BLUE); this.addMouseMotionListener(this); this.setLocation(x, y); } @Override public void mouseDragged(MouseEvent e) { int muisY = e.getY(); int verschilY = clickedY - muisY; int busY = this.getY() - verschilY - 75; //int muisX = e.getX(); //int verschilX = clickedX - muisX; //int busX = this.getX() - verschilX - 20; this.setLocation(LocationBusX, busY); // Vanaf Hieronder Border Collisions if(busY > 350) { setLocation(LocationBusX, 350); } if(busY < 0) { setLocation(LocationBusX, 0); } // Vanaf Hieronder Collisions met andere spelfiguren. try { if(collisionRodeAuto()) { this.setLocation(LocationBusX, 80); System.out.println("Collision!"); } if(collisionAuto()) { this.setLocation(LocationBusX, 250); System.out.println("Collision!"); } } catch(NullPointerException ex) { } } public boolean collisionRodeAuto() { return SpeelLevels.rd.getBounds().intersects(this.getBounds()); } public boolean collisionAuto() { return SpeelLevels.auto1.getBounds().intersects(this.getBounds()); } @Override public void mouseMoved(MouseEvent e) { } }
package RushHourSpel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; public class SpeelLevels extends SpelFiguren implements ActionListener{ static int BusX; static RodeAuto rd; static Auto auto1; private JPanel speelveld; private int LocationAutoX = 300, LocationAutoY = 400, LocationRdX = 0, LocationRdY = 230; public SpeelLevels(){ this.setSize(500, 500); this.setLayout(null); rd = new RodeAuto(20, 20); rd.setLocation(LocationRdX, LocationRdY); speelveld = new JPanel(); speelveld.setSize(500,500); speelveld.setLayout(null); for (int i = 0; i < 1; i++) { BusX = 160 + i * 60; bus2 = new Bus(BusX, 50); bus2.setLocation(BusX, 50); speelveld.add(bus2); } for (int i = 0; i < 1; i++) { int BusX = 260 + i * 60; bus1 = new Bus(BusX, 150); bus1.setLocation(BusX, 150); speelveld.add(bus1); } this.add(speelveld); auto1 = new Auto(20, 20); auto1.setLocation(LocationAutoX, LocationAutoY); speelveld.add(rd); speelveld.add(auto1); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
package RushHourSpel; import javax.swing.JPanel; public class SpelFiguren extends JPanel { protected int lengthCarX; protected int lengthCarY; protected int lengthBusX; protected int lengthBusY; protected int LocationBusY, LocationRdX = 0, LocationRdY = 230, LocationBusX2 = 200, LocationBusY2; protected static int LocationBusX; protected static Bus bus1; static Bus bus2; public SpelFiguren(){ lengthCarX = 150; lengthCarY = 60; lengthBusX = 60; lengthBusY = 230; } }