attempt to index global 'self' (a nil valu

2019-10-05 05:13发布

This is part of my game.lua I keep getting this error though.

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

标签: lua null
2条回答
Luminary・发光体
2楼-- · 2019-10-05 05:56

There are two ways to call functions in lua

1) call the function with ':' gives 'm' as first argument m:DoJob()

2) but if you call if with '.' you have to define the context where to look for the function, typically the module itself m.DoJob(m)

If you call a method defined in the module as module:Method() with module.Method() it expects as argument self and throws an error.

查看更多
我只想做你的唯一
3楼-- · 2019-10-05 06:07

It appears to me that you've misplaced the closing end in your createScene method. Try moving the lines below into the function body so the implicit self is used instead of the global self key:

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
查看更多
登录 后发表回答