This site requires JavaScript, please enable it in your browser!
Greenfoot back
alfonsc
alfonsc wrote ...

2017/4/14

Else is directly called even though if argument is true

alfonsc alfonsc

2017/4/14

#
for (int i = 0 ; i < 8 ; i++)
            {
                if (getY() - i == 0)
                {
                    break;
                }
                else if (PlayerVsPlayer.tile[getY() - i][getX()] == 0)
                {
                    getWorld().addObject(new Range(1, this, getX(), getY()), getX(), getY() - i);
                }
                else if (PlayerVsPlayer.tile[getY() - i][getX()] == 1 && color == "black" || PlayerVsPlayer.tile[getY() - i][getX()] == 2 && color == "white")
                {
                    getWorld().addObject(new Range(2, this, getX(), getY()), getX(), getY() - i);
                }
                else
                {
                    break;
                }
            }
When I run the code like this, it doesn't create any object. However, if I take the last else it does. Any suggestions?
danpost danpost

2017/4/14

#
I would check the tile array to see if the location ( x, y-1 ) has a value of '1' for a "white" type or a value of '2' for a "black" type. If so, this would prevent any objects from being created. Without the else, this condition would allow the 'for' loop to continue and create objects.
alfonsc alfonsc

2017/4/15

#
I'm sure the tile's value is 0. So it should go always to the second condition until it isn't. Thing is: if "else" is there, no object is created. If it isnt, second condition is met.
danpost danpost

2017/4/15

#
alfonsc wrote...
I'm sure the tile's value is 0. So it should go always to the second condition until it isn't. Thing is: if "else" is there, no object is created. If it isnt, second condition is met.
What does that have to do with this
danpost wrote...
I would check the tile array to see if the location ( x, y-1 ) has a value of '1' for a "white" type or a value of '2' for a "black" type. If so, this would prevent any objects from being created. Without the else, this condition would allow the 'for' loop to continue and create objects.
alfonsc alfonsc

2017/4/15

#
it doesnt meet the condition youre specifying. The aforementioned tile's valu equals to 0 so it shoul go for the first else if. But it doesn't unless theres no else.
danpost danpost

2017/4/15

#
Let us re-write the code to eliminate possible reasons for this to happen. Try this:
for (int i = getY()-1; i > 0 ; i--)
{
    if (PlayerVsPlayer.tile[i][getX()] == 0)
    {
        getWorld().addObject(new Range(1, this, getX(), getY()), getX(), i);
    }
    else if (PlayerVsPlayer.tile[i][getX()] == 1 && color == "black" || PlayerVsPlayer.tile[i][getX()] == 2 && color == "white")
    {
        getWorld().addObject(new Range(2, this, getX(), getY()), getX(), i);
    }
    else
    {
        break;
    }
}
See what this does.
alfonsc alfonsc

2017/4/16

#
Sorry for the late answer. It works, but I don't understand what you did differently. From what I got you basically implemented the first condition in the for loop itself.
alfonsc alfonsc

2017/4/16

#
Ahh ok now I got it.
danpost danpost

2017/4/16

#
alfonsc wrote...
Ahh ok now I got it.
I did not realize I had changed anything until I looked at it again. Now that I see what change I did make, I see that you probably could have just change your first line to this:
for (int i = 1 ; i < 8 ; i++)
where the 'i' int offset starts at one instead of zero. Because you had it start at zero, none of the first three conditions were being met and therefore the break in the last else was being executed (and hence, no actors). Without the last 'else if', you were passing over any same color objects and continuing to check for valid tiles (and hence, actors). Basically we were looking at the wrong tile for the cause of the issue -- the issue resided with the tile the current actor is located on; that is, not (x, y-1), but (x, y). But you did not want to include that tile in the loop anyway, hence the aforementioned change.
alfonsc alfonsc

2017/4/16

#
Yeah I know. I just realised that. Thanks for your help!
You need to login to post a reply.