【问题标题】:Removing Alert screen as display group删除警报屏幕作为显示组
【发布时间】:2015-03-20 13:19:11
【问题描述】:

我在那里,我再次为显示和删除警报而苦苦挣扎。我使用 Displaynew 组制作了一个警报屏幕。我有两个,一个带有按钮(ALert1)来选择动作,一个没有按钮(Alert2)来确认动作。问题是,当我调用 Alert1 并选择使 Alert2 出现的操作时,我无法同时删除这两个 Alert。我调用 Removealert 函数使两者都消失,但我只设法删除 Alert1。 Alert2 仍然存在。请帮忙,谢谢。

以下是代码:

function alertScreenScore(title, message) -- Alert1. Alert2 is similar with no buttons and is called alertScreenscore

alertBox=display.newImage("cornice.png")
alertBox.x=W
alertBox.y=H/1.3

transition.from(alertBox,{time=10,xScale=0.5,yScale=0.6,transition=easing.cutExpo})

titolomessaggio=display.newText(title,0,0,"Arial",100)
titolomessaggio:setTextColor(255,255,0,255)

titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5
titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-100

testomessaggio=display.newText(message,0,0,"Arial",100)
testomessaggio:setTextColor(255,255,0,255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5
testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10




buttonCancel = widget.newButton
{
  id = "buttonCancel",
  defaultFile = "buttonbn.png",
  label = "NO",
  labelColor = 
 { 
 default = { 0, 0, 0, 255 },
},
 font = native.systemFont,
 fontSize = 30,
 emboss = true,
 onEvent = decidiCancel,
 isEnabled=true
}


buttonOK = widget.newButton
{
 id = "buttonOK",
 defaultFile = "buttonbn.png",
 label = "YES",
 labelColor = 
 { 
   default = { 0, 0, 0, 255 },
 },
 font = native.systemFont,
 fontSize = 30,
 emboss = true,
 onEvent = decidiOK,
 isEnabled=true
}
buttonCancel.x=250
buttonCancel.y=620

buttonOK.x=520
buttonOK.y=620



scoreDisplayGroup=display.newGroup()
scoreDisplayGroup:insert(alertBox)
scoreDisplayGroup:insert(titolomessaggio)
scoreDisplayGroup:insert(testomessaggio)
scoreDisplayGroup:insert(buttonCancel)
scoreDisplayGroup:insert(buttonOK)

end

local decidiOK = function(event)

if event.phase=="began" then
score.add(punti)
score.save() 
end
alertScreen("","Score saved!") --Alert2


timer.performWithDelay(1000,removescore,1)
timeLimit=60
timeLeft.text = "1:00"


end

local decidiCancel = function( event )

 if event.phase == "ended" then
    timer.performWithDelay(100,removeAlertScreenScore,1)
 end
 timeLimit=60
 timeLeft.text = "1:00"
end


local function removescore(event)
  scoreDisplayGroup:removeSelf()
  alertDisplayGroup:removeSelf()
end

local function removeAlertScreenScore(event)
  scoreDisplayGroup:removeSelf()

end

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    代码本身是错误的,你已经在函数内部声明了按钮,但是 onRelease 函数被声明为本地并且更多的是它们在按钮下方。

    首先你应该知道lua是从上到下的方法,所以像这样更改代码并尝试

     local function removescore(event)
       scoreDisplayGroup:removeSelf()
       alertDisplayGroup:removeSelf()
    end
    
    local function removeAlertScreenScore(event)
       scoreDisplayGroup:removeSelf()
    
    end
    
    
    local decidiOK = function(event)
    
       if event.phase=="began" then
         score.add(punti)
         score.save() 
       end
      alertScreen("","Score saved!") --Alert2
    
    
     timer.performWithDelay(1000,removescore,1)
     timeLimit=60
     timeLeft.text = "1:00"
    
    
    end
    
    local decidiCancel = function( event )
    
      if event.phase == "ended" then
        timer.performWithDelay(100,removeAlertScreenScore,1)
      end
      timeLimit=60
      timeLeft.text = "1:00"
    end
    
    
     function alertScreenScore(title, message) -- Alert1. Alert2 is similar with        no buttons and is called alertScreenscore
    
        alertBox=display.newImage("cornice.png")
        alertBox.x=W
        alertBox.y=H/1.3
    
        transition.from(alertBox,  {time=10,xScale=0.5,yScale=0.6,transition=easing.cutExpo})
    
       titolomessaggio=display.newText(title,0,0,"Arial",100)
       titolomessaggio:setTextColor(255,255,0,255)
    
       titolomessaggio.xScale=0.5
       titolomessaggio.yScale=0.5
       titolomessaggio.x=display.contentCenterX
       titolomessaggio.y=display.contentCenterY-100
    
       testomessaggio=display.newText(message,0,0,"Arial",100)
       testomessaggio:setTextColor(255,255,0,255)
       testomessaggio.xScale=0.5
       testomessaggio.yScale=0.5
       testomessaggio.x=display.contentCenterX
       testomessaggio.y=display.contentCenterY+10
    
    
    
    
       buttonCancel = widget.newButton
       {
       id = "buttonCancel",
       defaultFile = "buttonbn.png",
       label = "NO",
       labelColor = 
       { 
       default = { 0, 0, 0, 255 },
       },
       font = native.systemFont,
       fontSize = 30,
       emboss = true,
       onEvent = decidiCancel,
       isEnabled=true
      }
    
    
     buttonOK = widget.newButton
     {
     id = "buttonOK",
     defaultFile = "buttonbn.png",
     label = "YES",
     labelColor = 
     { 
      default = { 0, 0, 0, 255 },
     },
     font = native.systemFont,
     fontSize = 30,
     emboss = true,
     onEvent = decidiOK,
     isEnabled=true
     }
     buttonCancel.x=250
     buttonCancel.y=620
    
       buttonOK.x=520
       buttonOK.y=620
    
    
    
      scoreDisplayGroup=display.newGroup()
      scoreDisplayGroup:insert(alertBox)
      scoreDisplayGroup:insert(titolomessaggio)
      scoreDisplayGroup:insert(testomessaggio)
      scoreDisplayGroup:insert(buttonCancel)
      scoreDisplayGroup:insert(buttonOK)
    
     end
    

    现在应该可以了,在移除分数函数上方声明所有变量。

    【讨论】:

    • 您好 Kumar,非常感谢您的帮助。如您所知,我正在 Lua 上迈出第一步:)。顺便说一句,我按照您的建议输入正确的顺序函数并正确声明上面的变量。我只有一个问题:声明为“local something=function(event)”和“function something(event)”的函数有什么区别?
    • 如果您想了解这些功能,请查看此链接lua.org/pil/6.2.html和lua.org/pil/5.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2022-01-11
    相关资源
    最近更新 更多