Advertisement Jump to content
Sign in to follow this  
mentor

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

This topic is 1875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!