reset password
Author Message
dstreff
Posts: 5
Posted 21:22 Jan 27, 2018 |

I'm looking at the `setXPos()` method I have in my NPC class. My current error check checks if the value being passed is below zero, then if so, sets the value to zero. However, when I pass the value using the `walk()` function for the first time, no matter what direction is specified, it will be zero. So if I do a `walk("right")` call twice in the NPCMain class, I'll be at position 1.0 instead of 2.0. (The same of course applies to the `setYPos()` method).

Is this expected behavior? I didn't want to make an assumption since we're setting the default to -999.0 instead of just 0.0.

mdomin52
Posts: 3
Posted 01:09 Jan 28, 2018 |

This makes sense if you didn't set the position of the NPC yourself. By default, the position is at -999. The first walk method ("any direction") will set it to 0. Then a second walk method ("right") will set it to 1. 

But I believe you are not supposed to change the position if the value is negative: "If the new value is not positive, display an error message saying so and do not change the values."

Last edited by mdomin52 at 01:12 Jan 28, 2018.
kknaur
Posts: 540
Posted 07:43 Jan 28, 2018 |

Good points all.  An easy way to handle this is as what was suggested.   Once the walk function has been called for the first time, you can set both x and y to be 0 and then proceed from there.  You could also have some logic that requires the user to set x and y to a positive position before allowing the walk function to work.  I am interested to see how people resolve this issue.