Pico 8 代码中的编码错误(Lua)。 (新手)

我最近开始编写代码,想尝试 Pico-8。这是一个使用 Lua 的游戏开发平台。我观看了如何创建平台游戏的教程,但遇到了代码障碍。代码中的 spid 是我的主要精灵名称,并且我将某些代码组织成以下部分:init、update、draw、collisions 和 player。如果有人可以帮助我解决错误,请记得我几乎没有编码经验。

错误消息:

Runtime error line 26 tab 4
If spid.dy>0 then
attempt to index global 'spid' (a nil value)
at line 26 tab 4

Cart code

pico-8 车带 // http://www.pico-8.com
version 18
__lua__
--init

function _init()

spid={
 sp=1,
 x=59,
 y=59,
 h=8,
 w=8,
 dx=0,
 dy=0,
 maxdx=2,
 maxdy=3,
 acc=0.4,
 boost=4,
 anim=0,
 running=false,
 jumping=false,
 falling=false,
 crouching=false,
 flp=false,
 }

 grav=1.2
 friction=0.85

end

-->8
--update

function _update()

 spid.x+=spid.dx
 spid.y+=spid.dy

 spid_update()
 spid_animation()

end
-->8
--draw

function _draw()

cls()
spr(spid.sp,spid.x,spid.y,1,1,spid.flp)
map(0,0)
end
-->8
--collisions

function obj_collision(obj,aim,flag)
 --obj = table and needs x,y,w,h

 local x=obj.x local y=obj.y
 local w=obj.w local h=obj.h

 local x1=0 local y1=0
 local x2=0 local y2=0

 if aim=="left" then
  x1=x-1   y1=y
  x2=x     y2=y+h-1

 elseif aim=="right" then
  x1=x+w   y1=y
  x2=x+w+1 y2=y+h-1

 elseif aim=="up" then
  x1=x+1   y1=y-1
  x2=x+w-1 y2=y

 elseif aim=="down" then
  x1=x     y1=y+h
  x2=x+w   y2=y+h
 end

 -- 以像素为单位转换为瓷砖

 x/=8 y1/=8
 x/=8 y2/=8

 if fget(mget(x1,y1),flag)
 or fget(mget(x1,y2),flag)
 or fget(mget(x2,y1),flag)
 or fget(mget(x2,y2),flag) then
    return true
   else
    return false
  end

end
-->8
--player

function spid_update()

 spid.dy+=grav
 spid.dx*=friction
 end

 if btn(2) then
  spid.dx-=spid.acc
  spid.running=true
  spid.flp=true
  end
 if btn(1) then
  spid.dx+=spid.acc
  spid.running=true
  spid.flp=false
  end

 if btnp(❎)
 and spid.landed then
  spid.dy-=spid.boost
  spid.landed=false
 end

 if spid.dy>0 then
  spid.falling=true
  spid.landed=false
  spid.jumping=false
  end
  if obj_collision(spid,"down",0) then
    spid.landed=true
    spid.falling=false
    spid.dy=0
    spid.y-=(spid.y+spid.h)%8

elseif spid.dy<0 then
  spid.jumping=true
  if obj_collision(spid,up,1) then
   spid.dy=0
 end
end

if spid.dx<0 then
 if obj_collision(spid,"left",1) then
  spid.dx=0
 end

elseif spid.dx>0 then
 if obj_collion(spid,"right",1) then
  spid.dx=0
  end

end
点赞
用户5836
用户5836

你的函数在第四个选项卡里有一个多余的 end

我认为在 spid.dx*=friction 后面的那个 end 不应该存在。在更正了这个问题之后,pico-8 抱怨 spid_animation() 不存在,但在提供的代码中确实不存在。

function spid_update()

 spid.dy+=grav
 spid.dx*=friction
end

 if btn(2) then
  spid.dx-=spid.acc
  spid.running=true
  spid.flp=true
 end

我可以建议你将 if/end 对齐到同一列以使它们保持一致。这样就更容易沿代码的边缘向下查看,知道哪些 endif 语句、函数等相对应。

例如,不要使用:

 if btn(2) then
  spid.dx-=spid.acc
  spid.running=true
  spid.flp=true
  end

而是应该使用:

 if btn(2) then
  spid.dx-=spid.acc
  spid.running=true
  spid.flp=true
 end

这看起来是一件小事,但当你有很多嵌套的代码时,这会让阅读变得更容易。

Pico-8 文件也可以使用外部编辑器进行编辑。当遇到像这种(不明显的)问题时,我发现这特别有用。首先,你可以一次看到更多的代码,其次它们可以突出显示缺少/多余的 end

在可执行文件中包含的手册中提到了使用外部编辑器的信息:https://www.lexaloffle.com/pico-8.php?page=manual

2020-05-12 22:37:48