Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SFML 2.1] Sprite's texture doesn't change properly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 mentor   Members   -  Reputation: 102

Like
0Likes
Like

Posted 27 November 2013 - 04:30 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



Sponsor:

#2 LorenzoGatti   Crossbones+   -  Reputation: 2762

Like
0Likes
Like

Posted 28 November 2013 - 06:12 AM

You don't set visited=true properly: you put the assignment in the branch for newly clicked nodes.

At most one node, the last one you clicked, should have clicked==true. This allows you to track that node with a variable, vastly simplifying logic. There's also no need to reassign unchanging values of "tex".

int clicked =-1;
//no clicked field in nodes
//initialize w[i].tex to NOTVISITED

//event digestion
for (int i=0; i&lt;size; i++) {
    if (gm.MouseClicked(w[i].getSpr(), win, ev)) { //win - window, ev - event
        //the previous clicked node, if it exists and we clicked on another node, becomes visited
        if (clicked>=0 and clicked !=i){
            w[clicked].tex=VISITED;
            //and in case the "visited" field is needed for other purposes:
            w[clicked].visited=true;
        }
        //the newly clicked node becomes clicked
        clicked=i;
        w[clicked].tex=CLICKED;
    }
}

//update
for (int i=0; i&lt;size; i++) {
    w[i].Update(win, gm, ev); //gm - gameobject
}

Produci, consuma, crepa




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS