Keyboard in different Scenes? (SOLVED)

I have a problem with the keyboard when changing scenes, or “flipping between tabs”. I want my game to run where you enter text and press return and it flips to the next scene. This works until you have to press the return key again. If I do function keyboard(k) on both, it just goes past the next scene. I can’t get it to work where I go
function Q2:keyboard(k)
and I don’t know why. (Q2 is a scene name, by the way) Is it possible to have separate keyboard functions for two different scenes? Any help is appreciated.

Show us the code! :slight_smile:

@MistReaper Each class could have it’s own keyboard. But since the keyboard is only being used by one person, you really don’t need seperate keyboards. You just need to know which scene you’re in to use what’s being keyed.

if k == RETURN then
    userAnswer = keyboardBuffer()
    if userAnswer == "rosaline" then
        rightAnswers = rightAnswers + 1
        Scene.Change "question 2"
        else
            Scene.Change "question 2"
    end
end

end

There is the code. I have defined all these variables and everything works fine if I take this code out of the next Scene. The “Scene.Change” thing is a Scene management thing, but you don’t have to be familiar with it to see what I am trying to do here. But I think it registers the “RETURN” press in both Scenes for some reason and I don’t know why. Any ideas?
@toadkick @dave1707

@MistReaper The keyboardBuffer() doesn’t get cleared until you do a showKeyboard(). So you need to hide the keyboard and then show it again. See keyboardBuffer() in the manual for more info. There needs to be a command that clears the buffer without having to hide then show the keyboard.

Also, since the scene has to change when Return is pressed, and it doesn’t matter if the answer is correct, it could be simplified like this (not solving your problem, just a little optimisation to your code)

if k == RETURN then
    userAnswer = keyboardBuffer()
    if userAnswer == "rosaline" then
        rightAnswers = rightAnswers + 1
    end
        
    Scene.Change "question 2"
end

@dave1707 I tried but it didn’t help, is there something I’m doing wrong?
Here is my code, but I can’t see anything…

Q1 = class()

function Q1:init(x)

-- you can accept and set parameters here
rightAnswers = 0
userAnswer = ""
questionText = "At the beginning of the story, who is romeo in love with?"
showKeyboard()

end

function Q1:draw()

-- Codea does not automatically call this method
background(0, 45, 255, 255)

-- Draws all text on the screen
fontSize(45)
fill(0, 0, 0, 255)
font("CourierNewPSMT")
text(userAnswer, WIDTH/2,500)

fontSize(45)
fill(0, 0, 0, 255)
font("Baskerville-Italic")
text(questionText, WIDTH/2, 700)

fontSize(45)
fill(0, 0, 0, 255)
font("Courier-Oblique")
text("Right Answers:   ", WIDTH/2, 400)
text(rightAnswers, 700, 400)

end

function Q1:touched(touch)

-- Codea does not automatically call this method
showKeyboard()

end

function keyboard(k)

-- If the user presses return, their answer will be recorded and checked to see if it is correct before moving to the next question.
if k == RETURN then
    userAnswer = keyboardBuffer()
    if userAnswer == "rosaline" then
        hideKeyboard()
        rightAnswers = rightAnswers + 1
        Scene.Change "question 2"
        else
            hideKeyboard()
            Scene.Change "question 2"
    end
end

end

Q2 = class()

function Q2:init(x)

-- you can accept and set parameters here
userAnswer = ""
questionText = "Who told Romeo to look at other women?"
showKeyboard()

end

function Q2:draw()

-- Codea does not automatically call this method
background(0, 45, 255, 255)

-- Draws all text on the screen
fontSize(45)
fill(0, 0, 0, 255)
font("CourierNewPSMT")
text(userAnswer, WIDTH/2,500)

fontSize(45)
fill(0, 0, 0, 255)
font("Baskerville-Italic")
text(questionText, WIDTH/2, 700)

fontSize(45)
fill(0, 0, 0, 255)
font("Courier-Oblique")
text("Right Answers:   ", WIDTH/2, 400)
text(rightAnswers, 700, 400)

end

function Q2:touched(touch)

-- Codea does not automatically call this method
showKeyboard()

end

function keyboard(k)

-- If the user presses return, their answer will be recorded and checked to see if it is correct before moving to the next question.
if k == RETURN then
    userAnswer = keyboardBuffer()
    if userAnswer == "benvolio" then
        rightAnswers = rightAnswers + 1
        Scene.Change "question 3"
        else
            keyboardBuffer()
            Scene.Change "question 3"
    end
end

end

@MistReaper Your 2 keyboard routines needs a class prefix. Q1:keyboard(k) and Q2:keyboard(k). Then in the normal keyboard routine, you would call those 2 routines.

But, as mentioned by me before, when I do function Q1:keyboard(k) or the same with 2, the return button doesn’t do anything… It makes complete sense why I need the class prefixes but I have no idea why they prevent the code from working…

@MistReaper You don’t have the classes set up right. You’re missing the “self” reference on all of your variables. Without knowing what you have in mind, do you really need classes. Will the different scenes have multiple questions, or will you just have a bunch of questions. If you’re doing just a bunch of question/answers, the you can simplify everything by using tables.

@dave1707 I am just doing a bunch of Q/As, but I don’t know how to correctly use tables. I think it may be easier for me to just set up the classes correctly now that I’ve started like this. How do I make the “self” reference work?

@MistReaper My suggestion is to stop what you’re doing and spend a few hours to learn about tables. You’re doing way to much code trying to use classes and you’re just wasting a lot of your time. You’re going to have to learn tables anyways, so you might as well start now. I’ll give you a simple example as soon as I write it.

@dave1707 Ok thanks :slight_smile:

@MistReaper Here’s a version using tables.


displayMode(FULLSCREEN)

function setup()
    wrong=""
    qa=1
    userAnswer=""
    rightAnswers=0
    question={}
    table.insert(question,"At the beginning of the story, who is romeo in love with?")
    table.insert(question,"Who told Romeo to look at other women?")
    answer={}
    table.insert(answer,"rosaline")
    table.insert(answer,"benvolio")   
end

function draw()
    background(0, 45, 255, 255)
    fontSize(17)
    text("tap screen for keyboard",WIDTH/2,100)
        
    fontSize(45)
    fill(0, 0, 0, 255)
    font("CourierNewPSMT")
    if wrong=="" then
        userAnswer=keyboardBuffer()
    end
    text(userAnswer, WIDTH/2,500)
    
    font("Baskerville-Italic")
    text(question[qa], WIDTH/2, 700)
    
    font("Courier-Oblique")
    text("Right Answers: ", WIDTH/2, 400)
    text(rightAnswers, 700, 400)
    
    if wrong~="" then
        font("Courier-Oblique")
        text("Right Answers: ", WIDTH/2, 400)
        text(wrong, WIDTH/2, 600)
    end
end

function keyboard(k)
    if k == RETURN then
        userAnswer = keyboardBuffer()
        if userAnswer == answer[qa] then
            rightAnswers = rightAnswers + 1
            wrong="Correct"
        else
            wrong="Wrong"
        end  
        qa=qa+1
        if qa>#question then
            qa=1
        end
        hideKeyboard()
    end
end

function touched(t)
    if t.state==BEGAN then
        showKeyboard()
        wrong=""
    end
end

@MistReaper I made a change to the above code.

@dave1707 Thanks so much, this fixed my problem! You forgot to hide the keyboard when they got thee question wrong though :stuck_out_tongue_winking_eye: .

Also, if I were to make it so that when the player gets the answer wrong then it still continues, would I use the part inside the “if userAnswer == answer[ga]” statement, but without the “rightAnswers = rightAnswers + 1” part?

@MistReaper I made some changes to the above code.

@MistReaper I posted the wrong code, the correct code is above.

@dave1707 I can’t thank you enough for that. I really appreciate the help you always offer when I have a problem. :wink:

@MistReaper, I believe @SkyTheCoder has a slideshow/presentation about tables that helped me out, if you need help