Help with if.
Hello
Im making a game there i have a wall that i dont want people walk through , its a simple 2d game.
I have used this code for the end of the map:
if(x != 0){
//here will the character move.
}
it works fine , but know i need a if to look if the character is at some special cords like:
if(x!= 560 && y!=200){
//here will be the char movement.
}
but it will be exactly like
if ( x!=560){
if(y!=200){
}
}
I want it to if i am at the position 560,200 not the hole x 200 line or the y line.
I want it to do: If(im not at cord 500,200) {
//move
}
Please help me with this. thx
If I understand your question correctly then it's just the following?
if (x == 560 && y == 200) {
// At the co-ordinates
}
Or am I missing something really fundamental?
Look:
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if(x != 0) { // The left edge of the map
if ( x!=440 && y!=484) { //And here i try to check if the char arent
//at cords 440,484 but it will check if the character is at the whole
//440,1 440,2 440,3 440,4 and more. How would i do if i wanted it
//to check only if the character is position 440,484. not the hole
//440 line, 484 line.
x-=44;
repaint();
}
}
}
Is it possible that i want to do?
And how?
Message was edited by:
javaguy387
then i think you should use < or > sign
because it checks if the character is lesser or greater to the wall.
if ( x <= 440 && y <=484)
I think you'll want to use OR instead of AND:
if ( x!=440 || y!=484)
// not at (400,484)
Combined with the left edge:
if ( x>0 && (x!=440 || y!=484) )
// not in wall and not at (400,484)
Let me explain:
(x==440 && y==484) means we are at the point.
(x==440 || y==484) means we are horizontally or vertically aligned with the point (on the cross).
now (x==440 && y==484) can be rewritten as ! (x!=440 || y!=484)
and (x==440 || y==484) can be rewritten as ! (x!=440 && y!=484)
Your problem was working through the logic, if you have problems with this type of stuff, read up on boolean algebra http://hyperphysics.phy-astr.gsu.edu/hbase/electronic/diglog.html
where A is x == 440, B is y == 484
If !(A AND B), applying DeMorgan's, you convert from !(A AND B) to !A OR !B
this essentially means that as long as x is not equal to 440 or y is not equal to 484, you will be able to move, in the event that x is 440 and y is 484, both terms will be false and the character is unable to move.
Conversely, you might want to consider something along the lines of
(if x == 440 && y == 484)
{
// ha ha, you can't move
}
else
{
// move
}
if it makes it more readable/easy to understand