SDK Corona 我想执行 --> movboton:setSequence( "apretado" ) <-- 用于更改 ImageSheet 的序列。如果我在函数之外执行它但是当我从函数中尝试时它会起作用。我收到此错误“99:尝试索引全局 'movboton'(零值)”。
知道为什么吗?谢谢。
botonnormal = graphics.newImageSheet( "buttonstart.png", { width=340, height=338, numFrames=2 } )
botonapretado = graphics.newImageSheet ( "buttonstart.png", { width=340, height=338, numFrames=2 } )
local movboton
local seqBoton = {
{ name="normal", sheet=botonnormal, start=1, count=1, time=1000},
{ name="apretado", sheet=botonapretado, start=2, count=2, time=1100}
}
movboton = display.newSprite(botonnormal, seqBoton)
movboton.x = display.contentWidth / 2
movboton.y = display.contentHeight / 2
movboton.xScale = .5
movboton.yScale = .5
如果我把代码放在这里,它可以工作,但是从我触摸屏时调用的函数中,它给了我 nil 错误。
movboton:setSequence("pretado")
movboton:play()
local function apretato(event)
--print("apretado")
if event.phase == "began" then
print("start")
--storyboard.gotoScene("game", "fade", 400)
movboton:setSequence( "apretado" )
movboton:play()
end
end
function scene:enterScene(event)
btninvisible:addEventListener( "touch", apretato )
end
这是所有代码:
-- requerimientos
local storyboard = require("storyboard")
local scene = storyboard.newScene()
--Background
function scene:createScene(event)
local screenGroup = self.view
local background = display.newImage("start.png")
screenGroup:insert(background)
city2 = display.newImage("city2.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 0
city2.y = 320
screenGroup:insert(city2)
--PERSONAJE
--Imagenes en forma de sheet
botonnormal = graphics.newImageSheet( "buttonstart.png", { width=340, height=338, numFrames=2 } )
botonapretado = graphics.newImageSheet ( "buttonstart.png", { width=340, height=338, numFrames=2 } )
--Simulacion de andar del personaje
local movboton
local seqBoton = {
{ name="normal", sheet=botonnormal, start=1, count=1, time=1000},
{ name="apretado", sheet=botonapretado, start=2, count=2, time=1100} --, loopCount =0
}
--Iniciamos
movboton = display.newSprite(botonnormal, seqBoton)
movboton.x = display.contentWidth / 2
movboton.y = display.contentHeight / 2
movboton.xScale = .5
movboton.yScale = .5
mybutton = display.newImage("button.png")
mybutton.x = display.contentWidth /2
mybutton.y = display.contentHeight -75
mybutton.xScale = .3
mybutton.yScale = .3
btninvisible = display.newImage("botonopacityzero.png")
btninvisible.x = display.contentWidth /2
btninvisible.y = display.contentHeight /2
btninvisible.xScale = .5
btninvisible.yScale = .5
btninvisible.alpha = 0.2 --opacidad
end
function start(event)
if event.phase == "began" then
--print("start")
storyboard.gotoScene("game", "fade", 400)
end
end
local function apretato(event)
--print("apretado")
if event.phase == "began" then
print("start")
--storyboard.gotoScene("game", "fade", 400)
movboton:setSequence( "apretado" )
movboton:play()
end
end
function scene:enterScene(event)
--background:addEventListener("touch", start)
mybutton:addEventListener( "touch", start )
btninvisible:addEventListener( "touch", apretato )
end
function scene:exitScene(event)
--background:removeEventListener("touch", start)
mybutton:removeEventListener( "touch", start )
--mybutton.destroy()
mybutton:removeSelf()
mybutton = nil
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene