zoukankan      html  css  js  c++  java
  • love2d教程32--碎图打包器texturepacker

    texturepacker是一个碎图打包器,可以把小图合并成一张大图,并对大图做优化。我用的是特别版(只好用xx版了,

    不然导出的图片会被变成红色),网盘下载,文件会提示有毒,我也是在网上找的,其实是补丁,放心用。把补丁放到

    安装后的bin目录,并添加到杀毒软件信任名单,或者先关闭杀毒软件,运行补丁。

    碎图打包只需把包含图片的文件夹拖到右边的Sprite就可以了。

    注意导出格式选择如下图,点击工具栏上的Publishe就可以了,其它的默认。

    这会导出一个lua文件和一个合成后的png图片,lua文件里记载了碎图在大图里位置等信息。

    我稍微封装为imgSheet类,可以载入多个lua和png图片,并支持动画,需要注意的是,没有

    做错误检查,代码如下:

    imgSheet={frames={},anims={}}
    
    --添加texturepacker导出的lua和大图,imgfile要完整路径和后缀
    function imgSheet.addSheet(luafile,imgfile)
    
        local t=dofile(luafile)
    
        local frameIndex=t.frameIndex
        local frames = t.sheet.frames
        
        for k,v in pairs(frameIndex) do
            frames[v]["sheetFile"]=imgfile  --保存碎图对应的大图名
            frames[v]["show"]=true --是否在屏幕上显示
            imgSheet.frames[k]=frames[v]
            
        end
    
    end
    
    --file碎图文件名,不要后缀,后面的参数与love.graphics.draw相同
    function imgSheet.drawFrame(file,x, y, r, sx, sy, ox, oy, kx, ky )
        local frame = imgSheet.frames[file]
        local image = love.graphics.newImage(frame.sheetFile)
        local quad = love.graphics.newQuad(frame.x, frame.y, frame.width, frame.height, image:getWidth(), image:getHeight())
        if frame.show then
            love.graphics.draw(image, quad,x, y, r, sx, sy, ox, oy, kx, ky )
        end
    end
    --获取碎图对应的数据,file不要后缀,以下函数也是如此
    function imgSheet.getFrame(file)
        return imgSheet.frames[file]
    end
    --删除一帧图片,只是不在屏幕上显示
    function imgSheet.removeFrame(file)
        imgSheet.frames[file].show=false
    end
    
    --绘制动画 st文件名的起始序号,ed终止序号,tm动画每帧绘制时间
    --之后的参数同love.graphics.draw
    function imgSheet.drawAnim(file,st,ed,tm,x, y, r, sx, sy, ox, oy, kx, ky)
        if not imgSheet.anims[file] then
             --idx当前索引,state状态:1正常,0暂停,-1停止 ,-2删除
            imgSheet.anims[file]={tm=tm,st=st,ed=ed,dt=0,idx=st,state=1}
        end
        local anim = imgSheet.anims[file]
       
        if anim.state==1 then
            anim.dt=anim.dt+love.timer.getDelta( )
            if anim.dt>=anim.tm then
                anim.dt=0
                anim.idx=anim.idx+1
                if anim.idx>anim.ed then
                    anim.idx=anim.st
                end
            end
        end
        if anim.state~=-2 then
            imgSheet.drawFrame(file..anim.idx,x, y, r, sx, sy, ox, oy, kx, ky )
        end
    end
    --暂停动画
    function imgSheet.pauseAnim(file)
        local anim = imgSheet.anims[file]
        if anim then
            anim.state=0
        end
    end
    --恢复动画
    function imgSheet.resumeAnim(file)
        local anim = imgSheet.anims[file]
        if anim then
            if anim.state==0 then
              anim.state=1
            end
        end
    end
    --停止动画
    function imgSheet.stopAnim(file)
        local anim = imgSheet.anims[file]
        if anim then
            anim.state=-1
        end
    end
    --删除动画
    function imgSheet.removeAnim(file)
        local anim = imgSheet.anims[file]
        if anim then
            anim.state=-2
        end
    end
    return imgSheet

    完整的工程在网盘下载,碎图在imgs目录下。

  • 相关阅读:
    BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
    poj3728 The merchant[倍增]
    poj2750 Potted Flower[线段树]
    poj2482 Stars in Your Window[扫描线]
    poj2182 Lost Cows[BIT二分]
    UVA12096 The SetStack Computer
    第05组(65) 需求分析报告
    团队介绍与选题报告
    数据采集技术第三次作业
    结对编程作业
  • 原文地址:https://www.cnblogs.com/xdao/p/love2d-tutor32.html
Copyright © 2011-2022 走看看