删除警报屏幕作为显示组

时间:2015-03-19 07:52:12

标签: lua corona

我在那里,我再次挣扎着显示和删除警报。我使用Displaynew组编写了一个Alert屏幕。我有两个,一个用按钮(ALert1)来选择动作,一个没有按钮(Alert2)来确认动作。问题是,当我调用Alert1并选择使Alert2出现的操作时,我无法删除这两个警报。我调用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

1 个答案:

答案 0 :(得分:0)

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

首先你应该知道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

现在这应该有效,将所有变量声明为删除分数函数。