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

时间:2014-01-07 23:30:57

标签: lua corona

我是Corona SDK和LUA编程的新手。我正在尝试为他们的方向切换sprite这个错误出现 尝试索引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)

当我删除charface并将其更改为出现错误的精灵表

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

2 个答案:

答案 0 :(得分: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"

保持编码....................:)

答案 1 :(得分:0)

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