Reputation: 5245
I'm trying to do the popular game "Ruzzle" for PC See:
I am stuck on a very silly error i think but for some reason it looks like a property is not working!
I have a class called "check" that has a bool property named Used
. If it set to true the background will change from white to orange. This is achieved with the following :
bool _used;
public bool Used
{
get { return _used; }
set { _used = value; Invalidate(); }
}
void Check_Paint(object sender, PaintEventArgs e)
{
Graphics area = e.Graphics;
if (_used==false)
{
area.FillRectangle(Brushes.White, 0, 0, Width - 1, Height - 1);
...
}
else
{
area.FillRectangle(Brushes.Orange, 0, 0, Width - 1, Height - 1);
...
}
}
I've assigned to mouseClick
an event that i will define in the class Ruzzle
.
The class Ruzzle
handles the game wich basically is a bi-dimentional array of Check
.
public event EventHandler selected;
//in the constructor I've put this
this.MouseClick += new MouseEventHandler(Check_MouseClick);
and finally
void Check_MouseClick(object sender, MouseEventArgs e)
{
if (selected != null)
{
selected(this, e);
}
Invalidate();
}
In the class Ruzzle
I try to change the bool property of the Check
object by doing:
public Ruzzle(Control father)
{
myGame = new Check[4, 4];
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++)
{
myGame[i, j] = new Check(50, father,i,j);
myGame[i, j].Location = new Point((i * 50), (j * 50));
father.Controls.Add(myGame[i, j]);
myGame[i, j].selected += new EventHandler(Ruzzle_selected);
}
}
}
void Ruzzle_selected(object sender, EventArgs e)
{
int r = (sender as Check).Row;
int c = (sender as Check).Column;
myGame[r, c].Used = true;
}
The Row
and Column
properties are working fine becuase if I do a MessageBox.Show I get the correct values. However when it executes the Paint method in the Check
class the Used
boolean won't change !
What am I doing wrong?
Upvotes: 2
Views: 884
Reputation: 17274
I would post this as answer though this one is not going to tell what is the exact issue with your code. The code itself looks fine so you have to debug it. Let me give you some tips on how to debug this issue.
I can see two obvious possible scenarios why your Used
property is false
:
It is not being changed to true
correctly.
It is reset back to false
at some point.
So you have to check both these possibilities. First of all we should ignore all the Check
instances except for the one which is clicked otherwise other instances might distract you while debugging. There are two easy ways to do that:
Instantiate only one instance. In this case you should create a 1x1 field if that is possible in your game.
Distinguish between distances while debugging. There is a nice feature in VS debugger called Make object Id. Basically it allows you to assign a numeric unique identifier to some object while debugging. Later you can whether specific instance is the one that you marked before by observing its Id in debugger. If you haven't used this feature before please check out the link above.
Ok, now you are debugging only instance of Check
class. Let's start with checking whether property is set correctly. There are several points where you should check your property value, for example:
Paint
handler where you have the condition.
Setter of Used
property when calling the Invalidate
method. _used
should be definitely true
.
At the end of Ruzzle_selected
handler. Used
property should be true
here and you should check it. If it is true
then we can conclude that at least it is set correctly.
Now let's check whether property Used
is not reset back to false
. As far as I can see in your code you should have no places where you might actually want to reset it. Which means that if you'll find such a behavior then it would probably the bug you are looking for. I assume that you do not access your _used
field anywhere except for the Used
property. In this case debugging this possible issue is very easy, you just have to put a breakpoint in the setter of your property and see if it is set false
. In order to avoid breakpoint being hit when you set it to true
you can use the breakpoint condition feature. In your case the condition should be something like value == false && _used == true
.
Please try debugging using these steps and let us know if it helps you. Otherwise it might be something that I have missed, in this case I'll extend the answer.
Upvotes: 1