Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite's texture doesn't change properly  (Read 1561 times)

0 Members and 1 Guest are viewing this topic.

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Sprite's texture doesn't change properly
« 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:

Code: [Select]
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

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Sprite's texture doesn't change properly
« Reply #1 on: November 27, 2013, 08:19:25 pm »
Where's the code where you tell your sprite to change to the new texture?
DSFML - SFML for the D Programming Language.

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Sprite's texture doesn't change properly
« Reply #2 on: November 27, 2013, 08:33:15 pm »
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:
Code: [Select]
if (w[i].clicked)
            w[i].tex=CLICKED;
if it's not "clicked", then just check, if user clicked on this node earlier:
Code: [Select]
else {
            if (w[i].visited)
                w[i].tex=VISITED;
            else
                w[i].tex=NOTVISITED;
        }

I tried to add:
Code: [Select]
w[j].tex=VISITED;here:
Code: [Select]
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?
« Last Edit: November 27, 2013, 08:35:03 pm by mentor »

 

anything