--------------------------------------------------------------------
--
-- TextureAsync
--
--------------------------------------------------------------------
local function TextureAsync()
local ret = createTestLayer("Texture Async Load",
"Textures should load while an animation is being run")
local m_nImageOffset = 0
local size =cc.Director:getInstance():getWinSize()
local label = cc.Label:createWithTTF("Loading...", s_markerFeltFontPath, 32)
label:setAnchorPoint(cc.p(0.5, 0.5))
label:setPosition(cc.p( size.width/2, size.height/2))
ret:addChild(label, 10)
local scale = cc.ScaleBy:create(0.3, 2)
local scale_back = scale:reverse()
local seq = cc.Sequence:create(scale, scale_back)
label:runAction(cc.RepeatForever:create(seq))
local function imageLoaded(texture)
local director = cc.Director:getInstance()
local sprite = cc.Sprite:createWithTexture(texture)
sprite:setAnchorPoint(cc.p(0,0))
ret:addChild(sprite, -1)
local size = director:getWinSize()
local i = m_nImageOffset * 32
sprite:setPosition(cc.p( i % size.width, math.floor((i / size.width)) * 32 ))
m_nImageOffset = m_nImageOffset + 1
cclog("Image loaded:...")-- %p", tex)
end
local function loadImages(dt)
local i = 0
local j = 0
for i=0, 7 do
for j=0, 7 do
local szSpriteName = string.format(
"Images/sprites_test/sprite-%d-%d.png", i, j)
cc.Director:getInstance():getTextureCache():addImageAsync(
szSpriteName, imageLoaded)
end
end
cc.Director:getInstance():getTextureCache():addImageAsync("Images/background1.jpg", imageLoaded)
cc.Director:getInstance():getTextureCache():addImageAsync("Images/background2.jpg", imageLoaded)
cc.Director:getInstance():getTextureCache():addImageAsync("Images/background.png", imageLoaded)
cc.Director:getInstance():getTextureCache():addImageAsync("Images/atlastest.png", imageLoaded)
cc.Director:getInstance():getTextureCache():addImageAsync("Images/grossini_dance_atlas.png",imageLoaded)
ret:unscheduleUpdate()
end
local function onNodeEvent(event)
if event == "enter" then
ret:scheduleUpdateWithPriorityLua(loadImages,0)
elseif event == "exit" then
ret:unscheduleUpdate()
cc.Director:getInstance():getTextureCache():removeAllTextures()
end
end
ret:registerScriptHandler(onNodeEvent)
return ret
end
待学习 异步加载 纹理 addImageAsync
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一.什么是异步加载? 在之前的学习笔记中,爬取的网页是需要手动翻页的网址,但是一些网站是通过自动加载翻页的,如kn...