尝试索引UpValue "charfaceright"(一个nil值)

我是 Corona SDK 和 LUA 编程的新手。我在尝试切换精灵方向时出现了以下错误:

尝试索引 upvalue“charfaceright”(空值)

module(...,package.seeall)

display.setStatusBar(display.HiddenStatusBar)
system.activate(“multitouch”)

function new()

local gameGroup = display.newGroup()

local physics = require(“physics”)
physics.start();

local sprite = require(“sprite”)

local speed = 2
local move = 0
local charfaceright
local charfacerunright
local Xposchar = 50
local Yposchar = 200
local sheetrunright = graphics.newImageSheet(“sprite/vhanrunright.gif”,{width=67,height=84,numFrames=4})

local background1 = display.newImage(“background/menu_background.png”,-50,0)

local line = display.newLine(0,240,0,0)
line.x = 2
line.y = 231
physics.addBody(line,“static”,{density = 1.0,friction = 0.3,bounce = 0.2})

local floorlvl1 = display.newImage(“floor/kinabanlvl1flor.gif”)
floorlvl1.x = 500
floorlvl1.y = 245
physics.addBody(floorlvl1,“static”,{density = 1.0,friction = 0.3,bounce = 0.2})

local buttonleft = display.newCircle(0,0,20)
buttonleft.x = 35
buttonleft.y = 285
buttonleft.alpha = 0.8

local buttonrth = display.newCircle(0,0,20)
buttonrth.x = 85
buttonrth.y = 285
buttonrth.alpha = 0.8

local buttonat = display.newCircle(0,0,20)
buttonat.x = 360
buttonat.y = 285
buttonat.alpha = 0.8

local buttonjump = display.newCircle(0,0,20)
buttonjump.x = 425
buttonjump.y = 285
buttonjump.alpha = 0.8

charfaceright = display.newImage(“sprite/vhanfaceright.gif”)
charfaceright.x = Xposchar
charfaceright.y = Yposchar
physics.addBody(charfaceright,“dynamic”,{bounce = 0.2})

gameGroup:insert(background1)

local function movement(event)

    Xposchar = Xposchar + move

    if Xposchar> = 50 then
    background1.x = background1.x - move
    floorlvl1.x = floorlvl1.x - move
    end

end

Runtime:addEventListener(“enterFrame”,movement)

local function stop(event)
    if event.phase ==“ended”then

        move = 0;

    end
end

Runtime:addEventListener(“touch”,stop)

当我删除 charfaceright 并将其更改为精灵表时,该错误会显示:

function moveright(event)

     move = speed;

     charfaceright:removeSelf()
     charfaceright = nil

     charfacerunright = display.newSprite(sheetrunright,{name =“runvhanright”,start = 1,count = 4,time = 1000})
     charfacerunright.x = Xposchar
     charfacerunright.y = Yposchar
     physics.addBody(charfacerunright,“dynamic”,{bounce = 0.2})
     charfacerunright:play()

end

buttonrth:addEventListener(“touch”,moveright)

function moveleft(event)

    move = - speed;

end

buttonleft:addEventListener(“touch”,moveleft)

function moveup(event)

if Yposchar> = 200 then

    Yposchar = Yposchar - 50

    end
end

buttonjump:addEventListener(“touch”,moveup)

return gameGroup

end
点赞
用户258523
用户258523

charfaceright 似乎是限于你的 new 函数中的本地变量(虽然我很难确定,因为你的缩进不够一致或整洁)。因此,除非你在 new 函数内创建你的 moveright 函数,否则它将无法访问 charfaceright 的本地变量。

2014-01-08 00:41:54
用户1979583
用户1979583

这是你 EventListener 的问题。如果你想在按钮点击时更改精灵图像,我建议你使用 "tap" 监听器,而不是 "touch"。因为你的图像删除操作可以在 event.phase=="began"event.phase=="moved"event.phase=="ended" 的任一时间发生(通常是第一个和最后一个)。

所以尝试将你的 move right 函数和 buttonrth EventListener 更改如下:

function moveright( event )
     move = speed;

     if(charfaceright~=nil)then  -- 检查精灵是否存在
        charfaceright:removeSelf()
        charfaceright = nil
     end

     charfacerunright = display.newSprite( sheetrunright, { name="runvhanright", start=1, count=4, time=1000 } )
     charfacerunright.x = Xposchar
     charfacerunright.y = Yposchar
     physics.addBody(charfacerunright, "dynamic", {bounce = 0.2})
     charfacerunright:play()
end
buttonrth:addEventListener("tap", move right)  -- 将 "touch" 更改为 ---> "tap"

继续编码.................... :)

2014-01-08 15:22:57