【问题标题】:attemp to index upvalue "charfaceright"(a nil value)尝试索引 upvalue "charfaceright"(一个 nil 值)
【发布时间】:2014-01-07 23:30:57
【问题描述】:

我是 Corona SDK 和 LUA 编程的新手。我正在尝试切换精灵的方向,出现此错误 尝试索引 upvalue "charfaceright"(一个 nil 值)

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)

当我删除字符并将其更改为显示错误的精灵表时

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

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    这是您的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  -- Check whether the sprite exists
            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)  -- changed "touch" to ---> "tap"
    

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

    【讨论】:

      【解决方案2】:

      charfaceright 似乎是您的 new 函数的本地函数(尽管我很难说清楚,因为您的缩进不是很一致或干净)。因此,除非您在 new 函数中创建 moveright 函数,否则它将无法访问 charfaceright 局部变量。

      【讨论】:

        猜你喜欢
        • 2019-10-10
        • 1970-01-01
        • 2019-10-16
        • 2017-08-10
        • 2017-04-22
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多