Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have two lists which work with an XML file which holds questions and answers. At the moment the project displays all the questions at the same time so they "flicker" on the screen and iterate between each item in the list.

Whats the easiest way (coding) to pause the random generated list on any particular questions?

Question class:

class question
{
    public string questionString;
    public string apple;
    public string pear;
    public string orange;

    //the ? indicate the int can be nullable so it can accept the string item
    int? correctAnswer;

    public question(string newquestionString)
    {
        parseQuestion(newquestionString);
    }

    public void parseQuestion(string newquestionString)
    {
        List<string> questionComponents = newquestionString.Split('|').ToList<string>();

        questionString = questionComponents[0];
        apple = questionComponents[1];
        pear = questionComponents[2];
        orange = questionComponents[3];

        correctAnswer = Int32.Parse(questionComponents[4]);

    }

In Game1:

    Random q = new Random();
    int i = q.Next(questions.Count);



    spriteBatch.Begin();
    spriteBatch.DrawString(myfont, questions[i], new Vector2(100 + 100 *i, 100), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].questionString, new Vector2(100 + 100, 100), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].apple, new Vector2(100 + 100, 200), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].orange, new Vector2(100 + 100, 300), Color.Black);
    spriteBatch.DrawString(myfont, myQuestions[i].pear, new Vector2(100 + 100, 400), Color.Black);
    spriteBatch.End();
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Does the code labeled "Game1" occur in the Draw(GameTime gameTime) method? If so, the problem looks as though you draw a different question each frame not that you need to draw slower. If you draw the same thing every frame, there should be no flicker; I would adjust the code like this:

// private class level variable
int currentQuestion = 0;


// in the Update(GameTime gameTime) method
if(userChoseAnswer) // check input and see if you should advance to the next question
{
    Random q = new Random();
    // might want to add code here to avoid showing 
    // questions already shown? unless that wont help your game
    currentQuestion = q.Next(questions.Count);  
}


// in the Draw(GameTime gameTime) method
spriteBatch.Begin();
spriteBatch.DrawString(myfont, questions[currentQuestion], new Vector2(100 + 100 *i, 100), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].questionString, new Vector2(100 + 100, 100), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].apple, new Vector2(100 + 100, 200), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].orange, new Vector2(100 + 100, 300), Color.Black);
spriteBatch.DrawString(myfont, myQuestions[currentQuestion].pear, new Vector2(100 + 100, 400), Color.Black);
spriteBatch.End();
share|improve this answer
    
+1 This. A question and answer game shouldn't worry about timing unless they're timed questions, but the primary sentinel value should be checking whether a user has answered in the first place. –  Ray Dey Apr 21 '11 at 21:42
    
I think your answer is better than mine :). Small addendum A lot of newbies have problems with detecting a keypress (because its hard to hold a button for less than 15ms. So here is the code that to detect a key press: KeyBoardState oldState; if(Keyboard.GetState.IsKeyDown(Keys.A) && !oldState.IsKeyDown(Keys.A)){ userChoseAnswer = true; } oldState = Keyboard.GetState(); –  Roy T. Apr 22 '11 at 7:51
    
@Roy a good point on keypress. –  Nate Apr 22 '11 at 17:35
    
I've tried using this but I've got problems with the keyboardstate method that Roy T has given. Its giving an error for the line "GetState" saying its a method used incorrectly. I tried making a variable for the Update method for currentState but that hasnt worked either... :( –  Tom Apr 25 '11 at 13:01
    
Edit: I got it working, I made a local variable within the Update method but I forgot to put "= new KeyBoardState" which has made it work. Now when the game runs, it still displays all the questions one after another and doesnt stop.. –  Tom Apr 25 '11 at 13:06
show 1 more comment

The update and draw loops run roughly 60 times per second (30 on WPF, and it also depends on how much your computer is doing in each loop and how fast the computer is). To update the questions slower you need to keep track of the time passed. You can do that as following

float elapsedSeconds = 0; 
public void Update(GameTime gameTime){
     elapsedSeconds += (float)gameTime.ElapsedGameTime.TotalSeconds;
     if(elapsedSeconds > TimeIWantAQuestionToDisplay)
     {
       elapsedSeconds = 0;
       question = q.Next(questions.Count);
     }  
}
share|improve this answer
2  
This would work for showing a question for a given length of time, but I suspect the question should remain visible until the player chooses an answer. –  Nate Apr 21 '11 at 19:20
    
Ah yes, that is probably what Tom meant he was a bit unclear in his wording though. +1 for you sir :) –  Roy T. Apr 22 '11 at 7:53
    
I apologize if my wording was not clear, yes the answers to the questions signify an object within a game. Each question answer (three different fruit) need to be assigned for the answers –  Tom Apr 25 '11 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.