为啥 Corona sdk 中会出现此错误?

Posted

技术标签:

【中文标题】为啥 Corona sdk 中会出现此错误?【英文标题】:why is this error coming out in Corona sdk?为什么 Corona sdk 中会出现此错误? 【发布时间】:2021-09-03 00:13:36 【问题描述】:

12:39:31.002 错误:运行时错误 12:39:31.002 C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207:尝试调用方法“setFillColor”(零值) 12:39:31.002 堆栈回溯: 12:39:31.002 C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207: 在函数“监听器”中 12:39:31.002 D:\a\corona\corona\subrepos\timer\timer.lua:331: 在函数“方法”中 12:39:31.002 D:\a\corona\corona\platform\resources\init.lua:220: 在函数

这是我的代码

local composer = require("composer");
local scene = composer.newScene();
local widget = require( "widget" ) 
local json = require("json")


display.setDefault("background", 55/255, 50/255, 47/255);
------------------------------------------------------------------------------------

local tapText

local casi = display.newText( "a", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 42 )
casi:setFillColor(0/255,196/255,253/255,1)
local yes = display.newText( "c", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 46 )
yes:setFillColor(6/255,204/255,36/255,1)
local no = display.newText( "b", display.contentCenterX, 700, "Myfontilqar-Regular.ttf", 44 )
no:setFillColor(219/255,26/255,15/255,1)

local bal
local balpl
local vrem 

local scetcikBal = 0
local scetcikBalpl = 0
local scetcikLevel = 0

  
local settings = 
  yesJ = 0, noJ = 0, procent = 0, procent2 = 0
 

  

local function saveSettings()
  local path = system.pathForFile( "itoq.json", system.DocumentsDirectory )
  local file = io.open( path, "w" )
  if (file) then
    local contents = json.encode(settings)
    file:write( contents )
    io.close( file )
    return true
  end    
end



local secondL = 20

local function obnovVr( event )
     secondL = secondL - 1
     vrem.text = secondL
     if secondL == 0 then
        settings.yesJ = scetcikBal
        settings.noJ = scetcikBalpl
        settings.procent = (scetcikBal * 10)
        saveSettings()
        composer.gotoScene( "Level1Score")
     end
end   

  


local kor = 

      display.contentCenterX - 66, display.contentCenterY + 120,
      display.contentCenterX + 70, display.contentCenterY + 120,
      display.contentCenterX - 66, display.contentCenterY + 200,
      display.contentCenterX + 70, display.contentCenterY + 200


local tb =  
    "1 + 2 = ?","3","2","4","1","1 + 2 = 3",
    "2 + 3 = ?","5","4","6","3","2 + 3 = 5",
    "3 + 1 = ?","4","3","5","1","3 + 1 = 4",
    "2 + 2 = ?","4","2","3","1","2 + 2 = 4",
    "3 + 2 = ?","5","3","4","1","3 + 2 = 5",
    "1 + 1 = ?","2","5","4","1","1 + 1 = 2",
    "1 + 3 = ?","4","1","5","2","1 + 3 = 4",
    "2 + 1 = ?","3","2","1","4","2 + 1 = 3",
    "4 + 1 = ?","5","3","4","1","4 + 1 = 5",
    "1 + 4 = ?","5","4","2","3","1 + 4 = 5"




local j
for i = #tb, 2, -1 do
  j = math.random( i )
  tb[i], tb[j] = tb[j], tb[i]
end

local j1
for i = #kor, 2, -1 do
  j1 = math.random( i )
  kor[i], kor[j1] = kor[j1], kor[i]
end  


local button1
local button2
local button3
local button4

nVoprosa = 1

local t = 2

local function stop1()
  button1:setEnabled( false )
  button2:setEnabled( false )
  button3:setEnabled( false )
  button4:setEnabled( false )

  tapText.text = tb[nVoprosa][3]
  tapText:setFillColor(6/255, 204/255, 36/255, 1)  
end

local function stop2()
  button1:setEnabled( false )
  button2:setEnabled( false )
  button3:setEnabled( false )
  button4:setEnabled( false )

  tapText.text = tb[nVoprosa][3]
  tapText:setFillColor(219/255, 26/255, 15/255, 1)
  tapText.size = 50 
end
  
 
local function vb()
    
  t = t - 1
  if t == 0 then  
    
    scetcikBal = scetcikBal + 1
    bal.text = scetcikBal
    scetcikLevel = scetcikLevel + 1

    if ( scetcikLevel == 10) then
        settings.yesJ = scetcikBal
        settings.noJ = scetcikBalpl
        settings.procent = (scetcikBal * 10)
        saveSettings()
        composer.gotoScene( "Level1Score")
      
    else
       button1:setEnabled( true )
       button2:setEnabled( true )
       button3:setEnabled( true )
       button4:setEnabled( true )
       
       tapText:setFillColor(255/255, 255/255, 255/255, 1 )
       

         nVoprosa = nVoprosa + 1 
         tapText.text = tb[nVoprosa][1]
         button1:setLabel( tb[nVoprosa][2][1] )  
         button2:setLabel( tb[nVoprosa][2][2] )  
         button3:setLabel( tb[nVoprosa][2][3] )  
         button4:setLabel( tb[nVoprosa][2][4] )
     
         local j1
          for i = #kor, 2, -1 do
             j1 = math.random( i )
             kor[i], kor[j1] = kor[j1], kor[i]
          end 
     
       button1.x = kor[1][1]
       button1.y = kor[1][2]
       button2.x = kor[2][1]
       button2.y = kor[2][2]
       button3.x = kor[3][1]
       button3.y = kor[3][2]
       button4.x = kor[4][1]
       button4.y = kor[4][2]
    end
    t = 2
  end
end 

local function vbn()

t = t - 1 
if t == 0 then 
    scetcikBalpl = scetcikBalpl + 1
    balpl.text = scetcikBalpl
    scetcikLevel = scetcikLevel + 1

  if ( scetcikLevel == 10) then
        settings.yesJ = scetcikBal
        settings.noJ = scetcikBalpl
        settings.procent = (scetcikBal * 10)
        saveSettings()
        composer.gotoScene( "Level1Score")
  
    
  else
       button1:setEnabled( true )
       button2:setEnabled( true )
       button3:setEnabled( true )
       button4:setEnabled( true )
       
       tapText:setFillColor(255/255, 255/255, 255/255, 1)
       tapText.size = 40

       nVoprosa = nVoprosa + 1 
       tapText.text = tb[nVoprosa][1] 
       button1:setLabel( tb[nVoprosa][2][1] ) 
       button2:setLabel( tb[nVoprosa][2][2] )  
       button3:setLabel( tb[nVoprosa][2][3] )  
       button4:setLabel( tb[nVoprosa][2][4] )
 
      local j1
      for i = #kor, 2, -1 do
        j1 = math.random( i )
        kor[i], kor[j1] = kor[j1], kor[i]
      end 
 
      button1.x = kor[1][1]
      button1.y = kor[1][2]
      button2.x = kor[2][1]
      button2.y = kor[2][2]
      button3.x = kor[3][1]
      button3.y = kor[3][2]
      button4.x = kor[4][1]
      button4.y = kor[4][2]
   end
    t = 2
  end    
end 


local function handleButtonEvent( event )
 
    if ( "ended" == event.phase ) then
        stop1()
        timer.performWithDelay( 300, vb, t )
        --vb()
    end
end

local function handleButtonEventN( event )
 
    if ( "ended" == event.phase ) then
        stop2()
        timer.performWithDelay( 300, vbn, t )
        --vbn()
    end
end


---------------------------------------------------------------------------------------

function scene:create( event )

        local sceneGroup = self.view
       

      
       timer.performWithDelay( 1000, obnovVr, secondL )      
        
       
        tapText = display.newText("", display.contentCenterX, display.contentCenterY - 80, "Lato-Regular.ttf", 40 ) 
      tapText.text = tb[1][1]
        
      button1 = widget.newButton(
       
       
        id = "button1",
        label = tb[nVoprosa][2][1],
        onEvent = handleButtonEvent,
        labelColor =  default= 1, 1, 1 , over= 0, 0, 0, 0.5  ,
        fontSize = 36,
        font = "Lato-Regular.ttf",
        emboss = false,
       
        shape = "roundedRect",
        width = 120,
        height = 60,
        cornerRadius = 0,
        fillColor =  default=55/255,50/255,47/255,1, over=31/255,76/255,115/255,1 ,
        strokeColor =  default=0/255,196/255,253/255,1, over=0.8,0.8,1,1 ,
        strokeWidth = 3
      
  )

   button2 = widget.newButton(
      
        
        id = "button2",
        label = tb[nVoprosa][2][2],
        onEvent = handleButtonEventN,
        labelColor =  default= 1, 1, 1 , over= 0, 0, 0, 0.5  ,
        fontSize = 36,
        font = "Lato-Regular.ttf",
        emboss = false,
       
        shape = "roundedRect",
        width = 120,
        height = 60,
        cornerRadius = 0,
        fillColor =  default=55/255,50/255,47/255,1, over=31/255,76/255,115/255,1 ,
        strokeColor =  default=0/255,196/255,253/255,1, over=0.8,0.8,1,1 ,
        strokeWidth = 3
      
  )

   button3 = widget.newButton(
      
 
        id = "button3",
        label = tb[nVoprosa][2][3],
        onEvent = handleButtonEventN,
        labelColor =  default= 1, 1, 1 , over= 0, 0, 0, 0.5  ,
        fontSize = 36,
        font = "Lato-Regular.ttf",
        emboss = false,
       
        shape = "roundedRect",
        width = 120,
        height = 60,
        cornerRadius = 0,
        fillColor =  default=55/255,50/255,47/255,1, over=31/255,76/255,115/255,1 ,
        strokeColor =  default=0/255,196/255,253/255,1, over=0.8,0.8,1,1 ,
        strokeWidth = 3
      
  )

   button4 = widget.newButton(
    
        id = "button4",
        label = tb[nVoprosa][2][4],
        onEvent = handleButtonEventN,
        labelColor =  default= 1, 1, 1 , over= 0, 0, 0, 0.5  ,
        fontSize = 36,
        font = "Lato-Regular.ttf",
        emboss = false,
        
        shape = "roundedRect",
        width = 120,
        height = 60,
        cornerRadius = 0,
        fillColor =  default=55/255,50/255,47/255,1, over=31/255,76/255,115/255,1 ,
        strokeColor =  default=0/255,196/255,253/255,1, over=0.8,0.8,1,1 ,
        strokeWidth = 3
      
  )     
  --
  button1.x = kor[1][1]
  button1.y = kor[1][2]
  button2.x = kor[2][1]
  button2.y = kor[2][2]
  button3.x = kor[3][1]
  button3.y = kor[3][2]
  button4.x = kor[4][1]
  button4.y = kor[4][2]


  
  casi.x = 240
  casi.y = 2

  yes.x = 40
  yes.y = 2

  no.x = 140
  no.y = 2

  bal = display.newText(scetcikBal, 80, 3, "Lato-Regular.ttf", 30 )
  balpl = display.newText(scetcikBalpl, 180, 3, "Lato-Regular.ttf", 30 )
  vrem = display.newText("20", 290, 3, "Lato-Regular.ttf", 30 )
  
 
  sceneGroup:insert(bal)
  sceneGroup:insert(balpl)
  sceneGroup:insert(yes)
  sceneGroup:insert(no)
  sceneGroup:insert(vrem)
 
  sceneGroup:insert(casi)
  sceneGroup:insert(tapText)
  sceneGroup:insert(button1)
  sceneGroup:insert(button2)
  sceneGroup:insert(button3)
  sceneGroup:insert(button4)


  

end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    if (phase == "will") then
             
    elseif (phase == "did") then

    end  
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    if (phase == "will") then
        
    elseif (phase == "did") then
       composer.removeScene( "level1" ) 
    end  
end



function scene:destroy( event )
    local sceneGroup = self.view

end





-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return sceneenter code here

【问题讨论】:

你应该添加导致错误的代码 对不起。我添加了一些代码。请看 什么是tapText?你在哪里定义它? 我添加了所有代码 【参考方案1】:

C:\Users\User\Documents\Corona Projects\MATH FOR CHILDREN\level1.lua:207:尝试调用方法“setFillColor”(一个零值)

在 level1.lua 的第 207 行,你这样做 setFillColor() 是不允许的,因为 setFillColor 不是函数值,而是 nil 值。

setFillColorShapeObject 类的一个方法。确保您像这样使用它:myShapeObject:setFillColor(myColor)

【讨论】:

以上是关于为啥 Corona sdk 中会出现此错误?的主要内容,如果未能解决你的问题,请参考以下文章

Lua/Corona SDK:为啥我的倒计时停止了?

为啥这个代码在 w.show() 调用中会出现段错误?

为啥这样的 penAL 代码在 Visual Studio 2008 中会出现这样的错误?

Lua Corona SDK 中的捏缩放比例限制

为啥 YouTube 数据 API 中存在缺失部分错误

为啥警告框中会出现“阻止此页面创建其他对话框”?