Touchscreen and TFT sharing UNO pins help needed

Hello I am using TFTLCD and Touchscreen libraries to make a game. How ever I need an answer how to I setup pins for reading and writing when using these libraries togehter.

Since I am using 2.6" touc-lcd which has sd memory card reader which I am using also (because there are my questions) I have read that I may need to be carefull to setup correct pins when using touchscreen. So I believe either sd card or lcd uses same pins than touch panel so I need to intialize those pins when I need them.

My code atm:

void loop(void){  
  initalizequestions(questioncount); //here subroutine picks random questions from sdcard database into public array
  for (int k=1, k == questioncount, k++) {
    clearscreen();
    writequestion(k); //this subroutine formats current question from sdcard database into public variables, then question is printed to the screen. 
    
    // if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
    //pinMode(XP, OUTPUT);
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    //pinMode(YM, OUTPUT);
         

    while(1) {
           if (touched(1)) break; //waits if area (1) is touched it will give points and then return 1 (true) and we brake while loop and continue to next question.
    }
    if (k == questioncount) {
     clearscreen();
     writeendscreen();
     
     // if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
     //pinMode(XP, OUTPUT);
     pinMode(XM, OUTPUT);
     pinMode(YP, OUTPUT);
     //pinMode(YM, OUTPUT);
     
     while(1) {
      if (touched(2)) brake; //waits if area (2) is touched it will return 1 (true) and we brake while loop and game starts from beginning
     } 
    }
  }   
}

At there is have 2 times making touch screen intializations pins to output for the waiting touch routine. Do I need to do these 2 times or all the times when I look for touch and when I am doing writing to the screen do I need to setup the pins again to input?

If I need to always switch them I make some subrutine that handles touch screen intialisation when needed but If I can put these definitions to setup routine like these then I dont need to float them in my script.

Can someone assistant me?

Edited a bit.

Okay dokey I was able to figure this on my own. It seams that Touchscreen with 2.4" in touchshield shares pins with TFT pins in Arduino UNO and may cause weard behaviours when using touch screen libraries and then using tft libraries. So I wrote this tiny function to overcome this behaviour:

//Returns correct pin modes for TFT library so thay writing to the screen behaves normally.
//Touchscreen shares pins and it's methods may not leave pin settings correct after usage so you must add this function to end of touch routine.  
void returnTFTpins() {
   pinMode(XM, OUTPUT);
   digitalWrite(XM, LOW);
   pinMode(YP, OUTPUT);
   digitalWrite(YP, HIGH);
   pinMode(YM, OUTPUT);
   digitalWrite(YM, LOW);
   pinMode(XP, OUTPUT);
   digitalWrite(XP, HIGH); 
}

Thank you for giving yourself the correct answer and posting it here. I had the ame issue wth a Velleman display that uses shared pins. So in one routine it showed up that I get touchscreen reading from out of nowwhere while writing to the screen. I had to call your function before and after writing to the screen to get rid of the problem though...