Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. 路 By Nesbox

Key Press

A topic by NobodysSon created Jan 31, 2020 Views: 1,195 Replies: 9
Viewing posts 1 to 4

I'm having a problem with the last button that was pressed remaining active after it is released so that it triggers things later in my code.

Here is an example of what I mean.

--script: lua
state = "one"
function TIC()
    cls(0)
    
    if btn(4) then
        if state == "one" then
            state = "two"
        end
        
        if state == "two" then
            state = "three"
        end
        
        if state == "three" then
            state = "one"
        end
    end    
    
    if state == "one" then
        print("First state",0,0)
    end
    
    if state == "two" then
        print("Second state",0,0)
    end
    
    if state == "three" then
        print("Third state",0,0)
    end
    
end
I've wrestled with this for awhile trying both btn and btnp but I haven't gotten anywhere. Any help would be greatly appreciated!
(3 edits)

The problem is the if statements - if you press button 4 the state becomes 'two' but then you're immediately checking the state again . As the state is now 'two' it becomes 'three' and so on. So, in a single pass of TIC() your state goes 1->2->3->1 so it always prints as state one. The fix is to use 'else' clauses like this:

state = "one"
function TIC()
    cls(0)
    if btnp(4) then
        if state == "one" then
            state = "two"  
        elseif state == "two" then
            state = "three"     
        elseif state == "three" then
            state = "one"
        end     end    
    
    if state == "one" then
        print("First state",0,0)
    elseif state == "two" then
        print("Second state",0,0)
    elseif state == "three" then
        print("Third state",0,0)
    end
end

I guess you'd also want to use btnp() as I've indicated otherwise the program will cycle through the states very quickly  (try it, using trace() instead of print to see the state changing) - probably not what you want!

Thank you that solved my issues! I understand now what I was doing wrong.

TIC-80 seems a good environment for learning how to code.

(+1)

You could make the code much more compact like this, it depends whether you really want to show the state names in text:

state = 1
statenames={'One','Two','Three'}
function TIC()
    cls(0)   
    if btnp(4) then
        state=state+1
        if state==4 then state=1 end
    end
    print(statenames[state],0,0)     
end
State names might be 'menu', 'play', 'game over' or 'attack', 'flee', 'defend' etc :D

Thanks for the pointer! I'm learning both Lua and TIC-80 as I go.

Great, good luck!

(1 edit)

i have a bit problem

x=50

p=0

e=0

function TIC()

cls()

if btn(3) then x=x+1 end

if btn(2) then x=x-1 end

spr(256, 50, 50)

end


when i run it the button didn't works :(

i use TIC 80 on mobile

im beginner,i hope somebody help me...

(+1)

I take it you want to move the sprite across the screen when you press a key?

If so, you're not using the value of x in your code - change spr(256,50,50) to spr(256, x, 50) and it should work.

ok,thank you so much 馃槂