Attempt to index global 'self' (a nil value)

Hi I am newbie to cocos2d. I just tried to add stripe animation by below code. When I Execute the below code [LUA ERROR] [string “src/main.lua”]:43: [string “HeroScene.lua”]:24: attempt to index global ‘self’ (a nil value) error occurs


main.lua

require “Cocos2d”

– cclog
local cclog = function(…)
print(string.format(…))
end

– for CCLuaEngine traceback
function G__TRACKBACK(msg)
cclog("----------------------------------------")
cclog(“LUA ERROR: " … tostring(msg) … “\n”)
cclog(debug.traceback())
cclog(”----------------------------------------")
return msg
end

local function main()
collectgarbage(“collect”)
– avoid memory leak
collectgarbage(“setpause”, 100)
collectgarbage(“setstepmul”, 5000)

cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")
cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(480, 320, 0)

--create scene 
local scene = require("HeroScene")
local HeroScene = scene.create()

if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(HeroScene)
else
    cc.Director:getInstance():runWithScene(HeroScene)
end

end

local status, msg = xpcall(main, G__TRACKBACK)
if not status then
error(msg)
end


HeroScene.lua

require “Cocos2d”
require “Cocos2dConstants”

local HeroScene = class(“HeroScene”,function()
return cc.Scene:create()
end)

function HeroScene.create()
local scene = HeroScene.new()

-- runAction
local cache = cc.SpriteFrameCache:getInstance()
-- cache:addSpriteFrames("hero.plist")

local animFrames = {}
for i = 0, 4 do 
    local frame = cache:getSpriteFrame(string.format("hero_%02d.png", i) )
    animFrames[i] = frame
end

local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.1)
local animate = cc.Animate:create(animation);
self:runAction(cc.RepeatForever:create(animate))

return scene

end

return HeroScene

The difference between . and : lead to this error.The function HeroScene.create() use the .,so it didn’t passed the self as default parameter.If you want to use self, you should declare the function as follows: HeroScene:create().

Hi guys. How can I create a new topic on this forum? I dont see any button.

Thanks samuele3hu Error cleared :smile:

@Ksamp checkout the below notification bar http://discuss.cocos2d-x.org/c/cocos2d-x.