Zammy:First I want to say that you have wrote great book, its my 4th book that I try to read on XNA and it is have been pleasant experience so far unlike some others. I have some constructive critique I am going to share with you when I have completely done the book.
Thanks for the kind words. I look forward to reading your feedback.
Zammy:On paper sounds very logical but when I try to work out with your algorithm things get messy.
I didn't provide an algorithm. I just described the new feature that you should implement.
![Stick out tongue [:P]](/cs/emoticons/emotion-4.gif)
Zammy:So I started with making a method to control a variable
// the amount of scroll
private int yScroll = 0;
private int lastScrollWheelValue;
private void CheckScroll(MouseState mouse1)
{
if (mouse1.ScrollWheelValue > lastScrollWheelValue)
yScroll++;
if (mouse1.ScrollWheelValue < lastScrollWheelValue)
yScroll--;
lastScrollWheelValue = mouse1.ScrollWheelValue;
}
that I just add to the drawing position of the rectangle in the drawing loop. That made the thing scroll ok in drawing but not when the mouse need to detect on which square it is hovering over.
...
After that long post I just wanted to ask to give me any tips how I can do the exercise with your version of the code? :)
Actually your basic approach is the one that I would take myself. And as you rightly point out, you have to keep the drawing separate from the input.
The point at which you and I diverge is what leads you to an implementation that "gets messy".
Keep thinking of the grid as an array of states. Keep indexing it with simple, integer-based X and Y values. Don't worry about the rectangles that get drawn on the screen. You have to keep the data separate from the presentation.
Here's a quick implementation that I threw together. As you can see, my "m_scrollY" is equivalent to your "yScroll", and the logic for both is very similar.
protected int m_scrollY = 0;
protected int m_lastMouseScroll = 0;
protected override void Update(GameTime gameTime)
{
...
// get the mouse state, and record its current location
MouseState mouse1 = Mouse.GetState();
CursorPosition.X = mouse1.X;
CursorPosition.Y = mouse1.Y;
// which grid cell is this mouse over now?
int gx = (int)Math.Floor(CursorPosition.X / CELL_WIDTH);
int gy = (int)Math.Floor(CursorPosition.Y / CELL_HEIGHT);
int deltaScroll = mouse1.ScrollWheelValue - m_lastMouseScroll;
if (deltaScroll != 0) { deltaScroll /= Math.Abs(deltaScroll); }
m_lastMouseScroll = mouse1.ScrollWheelValue;
m_scrollY = (m_scrollY + deltaScroll) % GRID_HEIGHT;
if (m_scrollY < 0) { m_scrollY += GRID_HEIGHT; }
for (int y = 0; y < GRID_HEIGHT; y++) // for each row
{
int adjustedY = (y + m_scrollY) % GRID_HEIGHT;
for (int x = 0; x < GRID_WIDTH; x++) // for each column
{
// if the current cell is under the mouse
if (gx == x && gy == adjustedY)
{
// start the fade effect
BlockStates[x, y].EffectAge = EFFECT_DURATION;
...
You use the adjustedY to check the cursor position in Update, but all references to the state array remain the same (indexed by "y" rather than "adjustedY").
On the drawing side of things, just update the outer loop so that the pos.Y value is calculated from the adjustedY (rather than the plain-Jane "y" value).
for (int y = 0; y < GRID_HEIGHT; y++) // for each row
{
// row as pixel
int adjustedY = (y + m_scrollY) % GRID_HEIGHT;
pos.Y = adjustedY * 32 + 2;
I hope this helps you. If you still have questions, let me know.
-- joe
There are 10 kinds of people in this world -- Those that can count in binary, and those that can't.