Results 1 to 14 of 14
Thread: Can't get the Rectangle to move.
- 03-21-2012, 09:37 PM #1
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Can't get the Rectangle to move.
Hello everyone.
I'm currently working on a game idea that I have and I struck a problem. The problem is that I have two rectangles that need to interact whit each other. One is a character rectangles that is controlled by the player and the other is a "Push Wall" that is a rectangles that should move when "pushed" by the character rectangle. What happens is the method that should move the "Push Wall" does not change the x and y coordinates but is geting called. If I put a command to move the wall in the PushWall class's paint method it moves. Like this:
But not like this:Java Code:public void paint(Graphics g) { x++; g.setColor(Color.RED); g.fillRect(x, y, size, size); }
If anyone can help it would be truly appreciated. Thank you.Java Code:public void movePW(int dir) { switch (dir) { case MOVE_LEFT: x -= size; break; case MOVE_RIGHT: x += size; break; case MOVE_UP: y -= size; break; case MOVE_DOWN: y += size; break; } }
Also if you need more info please ask.
P.S. Sorry for bad English.
-
Re: Can't get the Rectangle to move.
I have a feeling that your problem is in code that you've not shown us yet.
- 03-22-2012, 05:25 PM #3
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
I actually managed to figure it out. I just made the veriables that I need to change static and it worked perfectly don't really know why though...
For some reason not all methods were updated whit the new values... If anyone can tell me why this is please do. And yes it was in the code that I haven't yet showed cuz that code was just an exemple. Still, tnx for your help.
-
Re: Can't get the Rectangle to move.
- 03-23-2012, 05:54 PM #5
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
-
Re: Can't get the Rectangle to move.
- 03-24-2012, 10:29 AM #7
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
For some reason I can post it all in one post so I'll do it in separate ones. Here you go:
The Main class:
Java Code:package example; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Main extends Applet implements Runnable, KeyListener { private static final long serialVersionUID = 1L; private static final int MOVE_LEFT = 0; private static final int MOVE_RIGHT = 1; private static final int MOVE_UP = 2; private static final int MOVE_DOWN = 3; private Thread th; private Character ch; private PushWall pw; @Override public void init() { setBackground(Color.DARK_GRAY); setSize(800, 600); addKeyListener(this); ch = new Character(); pw = new PushWall(); th = new Thread(this); th.start(); } @Override public void paint(Graphics g) { pw.paint(g); ch.paint(g); } @Override public void run() { while (true) { repaint(); try { Thread.sleep(17); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()) { case KeyEvent.VK_LEFT: ch.moveCharacter(MOVE_LEFT); break; case KeyEvent.VK_RIGHT: ch.moveCharacter(MOVE_RIGHT); break; case KeyEvent.VK_UP: ch.moveCharacter(MOVE_UP); break; case KeyEvent.VK_DOWN: ch.moveCharacter(MOVE_DOWN); break; } } @Override public void keyReleased(KeyEvent ke) { } @Override public void keyTyped(KeyEvent ke) { } }
- 03-24-2012, 10:30 AM #8
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
The Character class:
Java Code:package example; import java.awt.Color; import java.awt.Graphics; public class Character { private static final int MOVE_LEFT = 0; private static final int MOVE_RIGHT = 1; private static final int MOVE_UP = 2; private static final int MOVE_DOWN = 3; private int size = 40; private int x = 2 * size, y = 2 * size; private PushWall pw; public Character() { pw = new PushWall(); } public void paint(Graphics g) { g.setColor(Color.LIGHT_GRAY); g.fillRect(x, y, size, size); } public void moveCharacter(int dir) { switch (dir) { case MOVE_LEFT: x -= size; if (isCollisionWhitPushWall(x, y)) pw.movePushWall(dir); break; case MOVE_RIGHT: x += size; isCollisionWhitPushWall(x, y); if (isCollisionWhitPushWall(x, y)) pw.movePushWall(dir); break; case MOVE_UP: y -= size; isCollisionWhitPushWall(x, y); if (isCollisionWhitPushWall(x, y)) pw.movePushWall(dir); break; case MOVE_DOWN: y += size; isCollisionWhitPushWall(x, y); if (isCollisionWhitPushWall(x, y)) pw.movePushWall(dir); break; } } public boolean isCollisionWhitPushWall(int x, int y) { if (x == pw.getX() && y == pw.getY()) return true; else return false; } }
- 03-24-2012, 10:32 AM #9
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
Lastly the PushWall class, the one that has the problem (I think):
Java Code:package example; import java.awt.Color; import java.awt.Graphics; public class PushWall { private static final int MOVE_LEFT = 0; private static final int MOVE_RIGHT = 1; private static final int MOVE_UP = 2; private static final int MOVE_DOWN = 3; private int size = 40; /* These are the variables that I make static to fix the problem */ private static int x = 200, y = 160; public void paint(Graphics g) { g.setColor(Color.ORANGE); g.fillRect(x, y, size, size); } public void movePushWall(int dir) { switch (dir) { case MOVE_LEFT: x -= size; break; case MOVE_RIGHT: x += size; break; case MOVE_UP: y -= size; break; case MOVE_DOWN: y += size; break; } } public int getX() { return x; } public int getY() { return y; } }
- 03-24-2012, 10:34 AM #10
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
That's it. Just an example that I made. Hope you can tell me what's the problem.
-
Re: Can't get the Rectangle to move.
OK, your main problem is that you have more than one PushWall object.
In your Main class you have a PushWall variable, pw, that is initialized in Main's init method, and which is displayed in the Applet:
You also have another PushWall object in a variable of the same name, pw, in the Character class. This object is never displayed, but instead you try to move it:Java Code:public class Main extends Applet implements Runnable, KeyListener { private static final long serialVersionUID = 1L; //... code deleted for sake of brevity and clarity private PushWall pw; // declared here @Override public void init() { setBackground(Color.DARK_GRAY); // ... pw = new PushWall(); // initialized here // ... } @Override public void paint(Graphics g) { pw.paint(g); // and displayed here ch.paint(g); }
The key to understanding your problem is that these two PushWall objects are completely distinct. Only one is being displayed, the one declared and initialized in the Main class, and only one is being moved, the one declared and initialized in the Character class, and you found that moving the second non-displayed PushWall object, had no effect on the PushWall object that was displayed in the Applet. To solve this, you made the variables that represent the PushWall's position static, which changes them from variables that are held by and unique to each object to variables that are held by the class. Now you can think of this as every PushWall object shares the very same x and y variable, and changing the value held by x for one PushWall object will have an effect on all PushWall objects. And yes, this works, but at a price, that price being that a PushWall object now doesn't have it's very own x and y but instead must share it with the class.Java Code:class Character { private static final int MOVE_LEFT = 0; // ... private PushWall pw; // declared here public Character() { pw = new PushWall(); // initialized here } //... public void moveCharacter(int dir) { switch (dir) { case MOVE_LEFT: x -= size; if (isCollisionWhitPushWall(x, y)) pw.movePushWall(dir); // moved here break; // ... etc
But why is this bad? Say you take the next logical extension to your program and decide that you want not one PushWall object displayed but rather several, all scattered about the applet. If you do this, you'll find that while yes, you can create multiple PushWall objects, you can't place them in different locations since they all share the very same x and y variable, and this limits your program unnecessarily.
A better solution is not to have this program create two PushWall objects but rather only create one, one that is both displayed in Main and moved in Character. You would do this by declaring PushWall variables in both the Main and Character class, but only initializing the PushWall object once, in Main. And then passing this reference into your Character object so that it can have a reference to the very same PushWall object as is being displayed. This can be done using either constructor parameters or a setter method.
For example this could work:
in your Main.java
And in Character.javaJava Code:public class Main extends Applet implements Runnable, KeyListener { private static final long serialVersionUID = 1L; //... private Character ch; private PushWall pw; // declared here @Override public void init() { setBackground(Color.DARK_GRAY); // ... ch = new Character(); pw = new PushWall(); // initialized here ch.setPushWall(pw); // ****** note new method ****** // ... }
Now you can make PushWall's x and y non-static and all is good.Java Code:class Character { private static final int MOVE_LEFT = 0; // ... private PushWall pw; // declared here public Character() { // pw = new PushWall(); // get rid of this } // ***** new method **** public void setPushWall(PushWall pw) { this.pw = pw; }
- 03-24-2012, 03:12 PM #12
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Re: Can't get the Rectangle to move.
Thank you very much! It works perfectly, you're the best!
-
- 03-24-2012, 03:29 PM #14
Member
- Join Date
- Mar 2012
- Location
- Lithuania
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
how to move a rectangle with arrow keys,
By jmu2101 in forum AWT / SwingReplies: 3Last Post: 09-27-2011, 01:13 AM -
does rectangle contain or overlap another rectangle?
By Xycose in forum New To JavaReplies: 6Last Post: 11-30-2010, 11:29 PM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
To make slider resize rectangle and to move pictures
By mneskovic in forum New To JavaReplies: 11Last Post: 05-21-2010, 11:07 AM -
Rectangle Intersection
By Gwindow in forum Java 2DReplies: 1Last Post: 04-24-2008, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
. Nevertheless thanks again!

Bookmarks