尝试索引UpValue "charfaceright"(一个nil值)
2014-1-7 23:37:32
收藏:0
阅读:120
评论:2
我是 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
点赞
用户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
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

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