cocos2d-x lua之 lua函数中有内部函数的错误

欢迎各位童鞋转载,转载请注明出处:http://blog.csdn.net/song_hui_xiang

作者新浪微博:http://weibo.com/u/3168848533

作者腾讯微博:http://t.qq.com/song_huixiang


local menuLayer = CCLayer:create()
local cache = nil

--加载plist文件
local function addPvrCczRes(plistName)
    local temp_name  = plistName..".plist"
    if cache then
        cache:addSpriteFramesWithFile(temp_name)
    else
        cache = CCSpriteFrameCache:sharedSpriteFrameCache()
        cache:addSpriteFramesWithFile(temp_name)
    end
end


--此方法把第二个动作创建于内部函数外,执行到内部函数时动作2(animate2)已不存在 运行报错:Cocos2d: [LUA ERROR] ASSERT FAILED ON LUA EXECUTE: 
local function headFireAndForce()
    addPvrCczRes("xuli_force")
    addPvrCczRes("headFire1")
    addPvrCczRes("headFire2")
    --创建动画1
    local array1 = CCArray:create()
    for i=1,10 do
        local name = "xuli_force_"..i..".png"
        local frame = cache:spriteFrameByName(name)
        array1:addObject(frame)
    end
    local animation1 = CCAnimation:createWithSpriteFrames(array1,0.1)
    local animate1 = CCAnimate:create(animation1)
    
     --创建动画2
    local array2 = CCArray:create()
    for i=1,20 do
        local name = "headFire_"..i..".png"
        local frame = cache:spriteFrameByName(name)
        array2:addObject(frame)
    end
    local animation2 = CCAnimation:createWithSpriteFrames(array2,0.05)
    local animate2 = CCAnimate:create(animation2)

    local sprite = CCSprite:createWithSpriteFrameName("xuli_force_1.png")
    sprite:setPosition(ccp(300,300))
    menuLayer:addChild(sprite,5)

    --内部函数
    local function startHeadFire()
        --程序运行到这时就会报错
        sprite:runAction(CCRepeatForever:create(animate2))
    end
    
    sprite:runAction(CCSequence:createWithTwoActions(animate1,CCCallFunc:create(startHeadFire)))
end
--headFireAndForce()

--此方法则把第二个动作创建于内部函数中,可以正常运行
local function headFireAndForce()
    addPvrCczRes("xuli_force")
    addPvrCczRes("headFire1")
    addPvrCczRes("headFire2")
    --创建动画1
    local array1 = CCArray:create()
    for i=1,10 do
        local name = "xuli_force_"..i..".png"
        local frame = cache:spriteFrameByName(name)
        array1:addObject(frame)
    end
    local animation1 = CCAnimation:createWithSpriteFrames(array1,0.1)
    local animate1 = CCAnimate:create(animation1)

    local sprite = CCSprite:createWithSpriteFrameName("xuli_force_1.png")
    sprite:setPosition(ccp(300,300))
    menuLayer:addChild(sprite,5)

    --内部函数
    local function startHeadFire()
         --创建动画2
        local array2 = CCArray:create()
        for i=1,20 do
            local name = "headFire_"..i..".png"
            local frame = cache:spriteFrameByName(name)
            array2:addObject(frame)
        end
        local animation2 = CCAnimation:createWithSpriteFrames(array2,0.05)
        local animate2 = CCAnimate:create(animation2)
        sprite:runAction(CCRepeatForever:create(animate2))
    end
    
    sprite:runAction(CCSequence:createWithTwoActions(animate1,CCCallFunc:create(startHeadFire)))
end
headFireAndForce()


local scene = CCScene:create()
scene:addChild(menuLayer)
CCDirector:sharedDirector():runWithScene(scene)

你可能感兴趣的:(cocos2d-x lua之 lua函数中有内部函数的错误)