尝试调试.仍然不知道为什么代码不起作用 [英] Tried debugging. Still dont know why the code is not working

查看:106
本文介绍了尝试调试.仍然不知道为什么代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作《 Simon Says》游戏,看来当它运行在我的算法中时,并不能完全完成它.

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.

我正在使用Lua的Corona sdk作为我的语言.在执行此功能之前,我已声明light = 2.

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

应该每次将光减1直到< =0.之后,应该运行该布尔表达式,但不是.

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)

我已经注意到什么是行不通的.当我尝试通过放置"print("hello")进行测试时,似乎似乎没有通过"if(light< = 0)then". 如果有帮助,我也会发布Start()函数:

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

这是我的OnTouchListener函数:

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)

推荐答案

在条件后添加打印功能.这将有助于调试程序流程.同时添加变量的打印,将有很大帮助

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

这篇关于尝试调试.仍然不知道为什么代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆