尝试索引全局变量 'self'(它是一个空值)

这是我的game.lua文件的一部分,但我一直在得到以下的错误。

function scene:createScene(event)

end

screenGroup = self.view

background = display.newImage("Space-Background-Image.gif")
screenGroup:insert(background)

local background1 = display.newImage("Space-Background-Image.gif")
background.x = 90
screenGroup:insert(background1)

function scrollBackground(self,event)
if self.x < -480 then
    self.x = 480
else
    self.x = self.x - 3
end
end
点赞
用户234175
用户234175

看起来您在 createScene 方法中放错了结尾的 end。尝试将下面的行移动到函数体中,这样就可以使用隐式的 self 而不是全局的 self 关键字了:

function scene:createScene(event)
  screenGroup = self.view

  background = display.newImage("Space-Background-Image.gif")
  screenGroup:insert(background)

  local background1 = display.newImage("Space-Background-Image.gif")
  background.x = 90
  screenGroup:insert(background1)
end
2013-11-28 06:38:57
用户3049633
用户3049633

Lua中调用函数有两种方式:

1)使用“:”调用函数,将“m”作为第一个参数传递

m:DoJob()

2)但是,如果使用“.”调用函数,则必须定义查找函数的上下文,通常是模块本身

m.DoJob(m)

如果使用“module.Method()”调用在模块中定义的方法“module:Method()”,它会期望self作为参数并抛出错误。

2013-11-29 14:50:41