个人学习:lua/cocos加载各种资源代码

个人学习:lua/cocos加载各种资源代码

  1. 加载spine/json(ExportJson)骨骼动画
--加载spine骨骼动画
local spineAnim = sp.SkeletonAnimation:create("base/res/spine/qiandao.json","base/res/spine/qiandao.atlas", 1.0)--预加载动画资源	
    spineAnim:setPosition(200,200)
	self:addChild(spineAnim)
	spineAnim:setAnimation(1, "effect", true)    --这里必须要设置动画才能显示

–加载ExportJson骨骼动画(很少用, 现在基本用spine)

	ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("game/yule/baccaratnew/res/animation/longhudou_vsdonghua.ExportJson")   --这里一定要确认好路径
	LHDPKadAnim = ccs.Armature:create("longhudou_vsdonghua")
	self._scene:addChild(LHDPKadAnim,10) --把动画加载进场景,然后就可以调用播放停止啥的函数了
	--播放
	LHDPKadAnim:getAnimation():playWithIndex(0) --注:这里的0代表播放资源里面的第一个动画, 如果ExportJson同时包含几个动画,那么就得用这个索引播放
	--停止
	--LHDPKadAnim:getAnimation():stop()
	--暂停
	--LHDPKadAnim:getAnimation():pause()
	--恢复
	--LHDPKadAnim:getAnimation():resume()
	armature->removeFromParent(); 
  
 	--资源释放  
 	ccs.ArmatureDataManager:getInstance():removeArmatureFileInfo("game/yule/baccaratnew/res/animation/longhudou_vsdonghua.ExportJson")
  1. 给精灵(Sprite)添加点击响应事件
--给精灵(Sprite)添加点击响应事件
function ClientScene:addSpriteTouchEvent(num,btn)
	local function onMyTouchBegan( touch,event )
		local target = event:getCurrentTarget()
        local size = target:getContentSize()
		local pos = touch:getLocation()
		local Rect = cc.rect(0, 0, size.width, size.height)
		local p = touch:getLocation()
		p = target:convertTouchToNodeSpace(touch)
		if cc.rectContainsPoint(Rect, p) then --判断点击位置
		    self:setGameCategoryVisible(false)
			self:onClickGameCategory(num)
		else
			print("==============超出点击范围=================")
		end
	end
	local touchListen = cc.EventListenerTouchOneByOne:create()
    touchListen:registerScriptHandler(onMyTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(touchListen,btn)
end
  1. 加载plist文件
--加载plist文件
cc.SpriteFrameCache:getInstance():addSpriteFrames("client/res/Button/fenleitubiaodonghua_dating0.plist")
  1. 读取plist的某一个属性
--读取plist的某一个属性(plist文件可拿vs查看, 里面的结构很类似xml)
local Path = cc.FileUtils:getInstance():fullPathForFilename("client/res/Button/fenleitubiaodonghua_dating0.plist")

local Dict = cc.FileUtils:getInstance():getValueMapFromFile(Path)

local frame = Dict["frames"]

local imageframe = frame["saoguang1.png"]

pos_x = imagefrmae["x"]

  1. 加载plist文件然后显示动画
--**加载plist文件然后显示动画**
--先加载plist和png文件
cc.SpriteFrameCache:getInstance():addSpriteFrames("game_res/animation.plist")
cc.Director:getInstance():getTextureCache():addImageAsync("game_res/animation.png")
local dengLongYuFrame = {}
	for i = 1,25,2 do
		if i < 10 then
			table.insert(dengLongYuFrame,cc.SpriteFrameCache:getInstance():
			getSpriteFrame(string.format("fish9_0%d.png",i)))
		else
			table.insert(dengLongYuFrame,cc.SpriteFrameCache:
			getInstance():getSpriteFrame(string.format("fish9_%d.png",i)))
		end
end
local dengLongYuAnimation = cc.Animation:createWithSpriteFrames(dengLongYuFrame,0.1)
local dengLongYuKey = string.format("animation_denglongyu")--创建名为灯笼鱼的动画
cc.AnimationCache:getInstance():addAnimation(dengLongYuAnimation,dengLongYuKey)
--使用
aniName = string.format("animation_denglongyu")
local animation = cc.AnimationCache:getInstance():getAnimation(aniName)
self:runAction(animation)


--用完过后还要释放资源
cc.SpriteFrameCache:getInstance():removeSpriteFramesFromFile("game_res/animation.plist")
cc.Director:getInstance():getTextureCache():removeTextureForKey("game_res/animation.png")
--同时动画也需要释放一遍
cc.AnimationCache:getInstance():removeAnimation("animation_denglongyu")
  1. 有时候,会碰到一个按钮是从csb资源里面直接加载的, 然后我们又没有csd源码, 要想改一个按钮的图片, 可以使用加载纹理的方式:
        local btn = clip_layout:getChildByName(str)     -- str是按钮在csb文件里面的节点名
		btn:loadTextures("res/newbet/ui_chip_bg_"..i.."_1.png",
		"res/newbet/ui_chip_bg_"..i.."_2.png",
		"res/newbet/ui_chip_bg_"..i.."_3.png")     -- 给按钮重新加载图片

7.加载个人制作的艺术字体(fnt字体)

    --fnt字体可以下载一个BMFont的软件制作, 先用ps把图片切割成一张图片一个字符,然后导出(24位, 8位BMFont识别不了),点击BMFont的图片管理, 一个个导入(注:一张图片代表什么字符要设置那个字符的ID,其他可以不动,ID就是打开软件时的列表, 鼠标移动过去右下角会有ID),设置好了过后, 就可以导出了, 存储为fnt格式)
    --加载fnt字体
	winTextArea = ccui.TextBMFont:create()
	winTextArea:setFntFile("fnt/win_count.fnt") --资源路径
	self.m_winTextArea = winTextArea
	self.m_winTextArea:addTo(self,100) --后面的100表示层级,越大越在上边显示
	self.m_winTextArea:setString(str)
    

猜你喜欢

转载自blog.csdn.net/qq_25563175/article/details/83068730