Flickering text issue

Hi all,

Firstly, I have to say I love this app! I started out with zero coding experience and have loved guessing, browsing, cut/pasting my way forward and have so far managed to solve all the issues I have encountered, albeit minor no doubt! But have now hit my first problem that has totally stumped me.

I’m trying to create a basic piece of code that prints text on screen, but only when the right key is touched. The basic idea of the Code is below

main

function setup()
    displayMode(FULLSCREEN)
    function touched(touch)
        showKeyboard()
    end
end

function keyboard(key)
     if  string.byte(key) == 97 then
        Text:draw()         
     end
end

Text class


function Text:draw()
    fontSize(55)
    font("AmericanTypewriter-Bold")
    fill(233, 170, 6, 255)
    text("hello",500,350)
end

My problem is that the minute I have it as part of an if function it causes the text to flicker on the screen. I can only assume that because the draw function tries to run 60 fps it runs the if function 60 times and once ‘97’ (a) has been pressed it holds that in the keyboardBuffer and every time the draw function runs it prints the text on screen, causing it to flicker? If i press key ‘97’ often enough it finally stops flickering… Assuming this is because it has printed it so many times on screen.

If insert similar code, but without the if, it doesn’t flicker. In my mind i need to clear the keyboardBuffer straight after Text:draw()? Is this possible, or any other suggestions?

Cheers

Karl

Sorry I don’t know why my example code all stayed on the same lines, it didn’t preview, like that, i hope it all still makes sense.

@Karl You need 3 ~ characters before and after your code. You only had 2. I changed your code a little and added comments to explain what I did. It can be changed a lot of other ways to make it work also.


displayMode(FULLSCREEN)    -- set full screen mode

-- Codea setup function
function setup()    
    textDraw=false     -- set textDraw to false at startup
end

-- Codea draw function
function draw()
    background(40,40,50)    -- set background color each draw cycle
    if textDraw then        -- time to call textDraw function ?
        Text:draw()         -- if yes, call Text function each time
    end
end

-- Codea touched function
function touched(touch)
    showKeyboard()        -- show keyboard when screen is touched
end

-- Codea keyboard function
function keyboard(key)
    if  string.byte(key) == 97 then   -- was the  'a'  key pressed   
        textDraw=true                   -- if yes, set textDraw to true             
    end         
end

-- Text class
Text = class()

-- Text class draw function
function Text:draw()   
    fontSize(55)     
    font("AmericanTypewriter-Bold")     
    fill(233, 170, 6, 255)     
    text("hello",500,350) 
end

@dave1707 Thank you, worked a treat.