【问题标题】:Tried debugging. Still dont know why the code is not working试过调试。仍然不知道为什么代码不起作用
【发布时间】:2015-07-09 05:18:59
【问题描述】:

我目前正在制作一款 Simon Says 游戏,它在运行我的算法时似乎并没有完全完成。

我已经尝试调试问题,并发现问题的根源在哪里,但我不确定要修复什么。

我使用 Corona sdk、Lua 作为我的语言。在这个函数之前,我已经声明了 light = 2。

它应该每次将 light 减 1,直到它

这是我的代码的一部分:

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

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

编辑:

这是我的 OnTouchListener 函数:

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)

【问题讨论】:

  • 该代码中没有循环。该代码调用start,您说每次event.phase == "ended"light 设置为2,然后有时从该值中减去一个。能否在starting 函数的末尾添加并澄清此处涉及的代码路径?我不知道目前应该如何使用此代码。
  • 这就是我的全部起始功能。此外,我有这个 OnTouch 函数,它设置 count = 1,以及设置 count = 1 或 2 或 3 ......取决于它点击的“框”。我将在我的帖子中添加该功能

标签: lua coronasdk


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 2014-01-17
    • 2021-03-11
    • 2015-07-06
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多