Your code does not compile and is missing a main method to be able to execute it.
Printable View
Your code does not compile and is missing a main method to be able to execute it.
Can you explain what is supposed to happen when the WASD keys are pressed and when the white square goes to the side of the black square?
I think I actually managed to solve it.
Thanks for all help,
Ah
Part of your problem was that the object is larger than the point at the top left corner.
Some suggestions:
Code:class ac extends JPanel {
public int speed = 1;
Rectangle[] rec = {new Rectangle(100, 100, 100, 100)}; //Rectangle
Rectangle pt = new Rectangle(120, 130, 5,5 ); //Current Character Position;
in the key listeners
pt.y -= speed;
and in the paint method:
g.setColor(Color.BLACK);
g.fillRect(rec[0].x, rec[0].y, rec[0].width, rec[0].height);
//Level;
g.setColor(Color.WHITE);
g.fillRect(pt.x, pt.y, pt.width, pt.height);
Now I encountered even one more problem...
My character is walking in the white areas, so he is walking into the cubes.
But when he are supposed to change cube, I mean when I add,
if(rec[0].contains(pt) *Here the thing I want to show starts* || rec[1].contains(pt))
he stays in the rec[0] area. :-/ How can I solve this?
Oh, did'nt see you wrote something. I'll take a look.
But he is still getting stuck in the walls.
What stops the moving of the small white square when you are pressing one of the WASD keys?
Look at your logic there.
Do you need to move the small white square back inside of the black square.
He stops when hitting the walls. It's like he is going one pixel too much.
Is there any way to tell him that he can't walk that extra pixel?
What does your logic do when HE hits a wall? What happens to future presses of the key that moved HIM to hit the wall.Quote:
He stops when hitting the walls
Test BEFORE changing the x,y if HE will hit a wall and don't move HIM if the new x,y hits a wall.Quote:
Is there any way to tell him that he can't walk that extra pixel?
Have the "boundary" wall one or two pixels inside of the visual wall.
Hmm, what do you mean with a boundary wall? :o
A rectangle inside of the visual rectangle.
Visual at 10, 10, 100, 100
boundary at 11, 11, 98, 98
Not really necessary, your code should detect a wall and not go past or into it.
Are you sure there are no other word then constaints(); i can use in order to see the walls?
He is going one pixel too much every time.
Your code should detect that BEFORE the movementQuote:
He is going one pixel too much every time.
Say the wall is at x = 100
and the object is at x = 99 and moving to the right (+x)
If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99
Do you mean methods?Quote:
no other word then constaints()
Read the API doc to see if any other methods would be of use.
Hmm, the code is testing it before the movement :-oQuote:
Your code should detect that BEFORE the movement
Say the wall is at x = 100
and the object is at x = 99 and moving to the right (+x)
If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99
How do you want me to stay at 99? -.-
Basically: if the move will be into the wall do not make it. What order can you set, test and change the values that will prevent moving into the wall. Look at the order you are doing those things.
Post the lines of code you are using to keep the object from moving into a wall.
Can't I like test collision for only one side of the cube?
For an example when it hits the left wall the left movement stops.
That would make me to solve it.
Thanks,
Ah
Why only one side? Is this just for testing some new code?Quote:
Can't I like test collision for only one side of the cube?
Post the lines of code you are using to keep the object from moving into a wall.
I think I actually managed to solve it now.
Made 2 different rectangles, one for left/up & one for right/down.
Works perfect!
Thanks!