SFML community forums
Help => Graphics => Topic started by: mentor on November 27, 2013, 07:27:15 pm
-
Hi,
I have two classes - Graph and Node. In Graph class I declare an array of Nodes. In the Update method of class Graph I have a loop for checking each node... So, I have 25 nodes. At the beginning, each node has a texture "NOTVISITED". If I click on a node it should change its texture to "CLICKED". When I click on another node, my first node changes its texture to "VISITED" and the now-clicked one changes texture to "CLICKED". That works ok, but there's one problem. Namely, if there's a node with a texture "CLICKED" and I click on another node, first node changes texture to "VISITED", but just after I do some mouse movement or I click again... And it should change its texture to "VISITED" right after clicking on another node. I'm just learning coding 2d apps and I have no idea how to fix it. Could someone help me?
Here's the code of Graph::Update:
for (int i=0; i<size; i++) {
if (gm.MouseClicked(w[i].getSpr(), win, ev)) { //win - window, ev - event
for (int j=0; j<size; j++) {
if (w[j].clicked) { w[j].clicked=false; break; }
}
if (!w[i].visited) w[i].visited=true;
w[i].clicked=true;
}
if (w[i].clicked)
w[i].tex=CLICKED;
else {
if (w[i].visited)
w[i].tex=VISITED;
else
w[i].tex=NOTVISITED;
}
w[i].Update(win, gm, ev); //gm - gameobject
}
w[] is my array of Nodes
-
Where's the code where you tell your sprite to change to the new texture?
-
My understanding of this code is:
We take a node (w[]). First, let's check, if user clicked on this node. If yes, then let's check, if there already is a "clicked" node somewhere - if yes, then change its "clicked" value to "false". Then we can set "clicked" value of our clicked node to "true".
Now, let's check, if our node (w[]) is "clicked", and if yes, then change its texture:
if (w[i].clicked)
w[i].tex=CLICKED;
if it's not "clicked", then just check, if user clicked on this node earlier:
else {
if (w[i].visited)
w[i].tex=VISITED;
else
w[i].tex=NOTVISITED;
}
I tried to add:
w[j].tex=VISITED;
here:
for (int j=0; j<size; j++) {
if (w[j].clicked) { w[j].clicked=false; w[j].tex=VISITED; break; }
}
But this didn't help. Maybe I should change textures somewhere else?