动画的性能问题

时间:2013-07-26 15:05:23

标签: performance animation lua corona

当我运行模拟器时,一切都运行顺畅,但是一旦我在实际设备上运行我的游戏,游戏会在加载/启动动画时冻结片刻。

我不知道我做错了什么。 这是加载动画的代码:

function showAnimation(event, type, index) 

    if type == "explosion" then
         sheetData = { width=100, height=100, numFrames=33, sheetContentWidth=1100, sheetContentHeight=300 }
         mySheet = graphics.newImageSheet( "media/animations/Explosion.png", sheetData )
         sequenceData = {
            { name = "fastRun", frames={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33 }, time=1050, loopCount = 1 }
        }
    elseif type == "smoke" then
        -- choose from 5 smokes animations
         sheetData = { width=100, height=100, numFrames=23, sheetContentWidth=500, sheetContentHeight=500 }
         mySheet = graphics.newImageSheet( "media/animations/smoke-" .. math.random(1,5)  .. ".png", sheetData )

         sequenceData = {
            { name = "fastRun", frames={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 }, time=math.random(555, 1110), loopCount = 1 }
        }   
    elseif type == "fire" then
         sheetData = { width=40, height=40, numFrames=89, sheetContentWidth=400, sheetContentHeight=360 }
         mySheet = graphics.newImageSheet( "media/animations/fire.png", sheetData )

         sequenceData = {
            { name = "fastRun", frames={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89 }, time=6000, loopCount = 1 }
        }   
    end

    animation[index] = display.newSprite( mySheet, sequenceData )
    animation[index].name = type
    animation[index].id = index
    animation[index].x = event.x
    animation[index].y = event.y
    animation[index]:rotate(math.random(-360, 360)) -- randomly rotate animation
    animation[index]:play()

    -- clean up function 
    local function mySpriteListener( event )
        if ( event.phase == "ended" ) then

            -- clean up invisibleTnt 
            if invisibleTnt ~= nil and animation[index] ~= nil then
                   ...
            end

            -- clean up fire flames
            if flames ~= nil and animation[index] ~= nil then
               ...
            end

            if animation[index] ~= nil then
                --animation[index]:removeSelf()
                animation[index]:getParent():remove( image )
                animation[index] = nil
            end
        end
    end
    -- run clean up function
    animation[index]:addEventListener( "sprite", mySpriteListener ) 
end

1 个答案:

答案 0 :(得分:0)

如果您要反复使用这些图像,我会提前加载它们,然后根据需要生成精灵。这将节省将图像加载到内存并解析它的时间。