Tried debugging. Still dont know why the code is not working

*爱你&永不变心* 提交于 2019-12-13 02:56:40

问题


I am currently making a Simon Says game, and it seems that when it runs through my algorithm, it doesn't fully complete it.

I have tried debugging the problem, and found out WHERE the source of the problem is, but I am not sure what to fix exactly.

I am using Corona sdk, Lua as my language. Before this function, I have stated light = 2.

It is supposed to decrement light by 1 each time until it is <= 0. After, it is supposed to run that Boolean expression, but it is not.

Here is part of my code:

started = false
pattern = true
gameOver = false

function starting(event)

    if (started == false) then
        started = true
    end


    if (event.phase == "ended") then


        start()
        count = count + 1
        startText.isVisible = false


    if (started == true) then

        if(count%20 == count - math.floor(count/20)*20) then

            clicked = 0

            if(light >= 0) then
                light = light - 1
            end

        end

        if (pattern == true) then

        --BELOW DOESNT WORK--
            if (light <= 0) then

                if (patternIndex >= table.getn(pat)) then
                    --randomizes lights--

                    clicked = math.random(1,10)+1
                    table.insert(pat,clicked)
                    patternIndex = 0
                    pattern = false

                else
                    clicked = pat[patternIndex] 
                    patternIndex = patternIndex + 1
                end


                light = 1

            end

        elseif (patternIndex == table.getn(pat)) then
            pattern = true
            patternIndex = 0
            light = 2

        end
 end

 startText: addEventListener( "touch", starting)

I have noted what does not work. It seems that, "if(light <=0) then" does not go through when I tried testing it by putting "print("hello"). I will post my Start() function as well if that helps:

function start()
    pat = {}
    random = math.random(9)
    patternIndex = 0
    light = 2
    clicked = 0
    count = 0
end

edit:

here is my OnTouchListener function:

function onTouchListener(event)

    if (pattern == false and gameOver == false) then

            if(event.target == btnclick1) then
                clicked = 1
                count = 1
            elseif(event.target == btnclick2) then
                clicked = 2
                count = 1
            elseif(event.target == btnclick3) then
                clicked = 3
                count = 1
            elseif(event.target == btnclick4) then
                clicked = 4
                count = 1
            elseif(event.target == btnclick5) then
                clicked = 5
                count = 1
            elseif(event.target == btnclick6) then
                clicked = 6
                count = 1
            elseif(event.target == btnclick7) then
                clicked = 7
                count = 1
            elseif(event.target == btnclick8) then
                clicked = 8
                count = 1
            elseif(event.target == btnclick9) then
                clicked = 9
                count = 1
            end

            if(clicked ~= 0) then

                if(pat[patternIndex]  == clicked) then

                    patternIndex = patternIndex + 1

                else
                    gameOver = true
                end

            end

    elseif (gameOver) then
            start()
            gameOver = false
    end    
end

btnclick1: addEventListener( "touch", onTouchListener)
btnclick2: addEventListener( "touch", onTouchListener)
btnclick3: addEventListener( "touch", onTouchListener)
btnclick4: addEventListener( "touch", onTouchListener)
btnclick5: addEventListener( "touch", onTouchListener)
btnclick6: addEventListener( "touch", onTouchListener)
btnclick7: addEventListener( "touch", onTouchListener)
btnclick8: addEventListener( "touch", onTouchListener)
btnclick9: addEventListener( "touch", onTouchListener)

回答1:


Add print function after conditions. That will help with debuging of flow of your program. Also add printing of the variables, it will help a lot



来源:https://stackoverflow.com/questions/31259005/tried-debugging-still-dont-know-why-the-code-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!