cocos-2dx 随记- 打飞机(LoadingScene.lua) - 第4天

更新中.. 非终极版

require "SystemConst"


--定义一个层,并注册场景消息
local size = cc.Director:getInstance():getWinSize()
local frameCache = cc.SpriteFrameCache:getInstance()
local textureCache = cc.Director:getInstance():getTextureCache()


local LoadingScene = class("LoadingScene", function()
    return cc.Scene:create()
end)


function LoadingScene.create()
    local scene = LoadingScene.new()
    scene:addChild(scene:createLayer())
    return scene
end


function LoadingScene:ctor()
    --场景生命周期事件处理
    local function onNodeEvent(event)
        if event == "enter" then
            self:onEnter()
        elseif event == "enterTransitionFinish" then
            self:onEnterTransitionFinish()
        elseif event == "exitTransitionStart" then
            self:onExitTransitionStart()
        elseif event == "cleanup" then
            self:cleanup()
        end
    end
    self:registerSpriteHandler(onNodeEvent)
end


function LoadingScene:createLayer()
    cclog("LoadingScene init...")
    local layer = cc.Layer:create()


    --精灵帧缓存
    frameCache:addSpriteFrames(loading_texture_plist)
    local bg = cc.TMXTileMap:create("map/red_bg.tmx")
    layer:addChild(bg)


    local logo = cc.Sprite:createWithSpriteFrameName("logo.png")
    layer:addChild(logo)
    logo:setPosition(cc.p(size.width/2,size.height/2))


    local sprite = cc.Sprite:createWithSpriteFrameName("loading4.png")
    layer:addChild(sprite)
    local logoX,logoY = logo.getPosition()
    sprite:setPosition(cc.p(logoX,logoY-130))


    --动画开始
    local animation = cc.AnimationFrameData:create()
    for i = 1, 4 do
        local frameName = string.format("loading%d.png",i)
        local spriteFrame = frameCache:getSpriteFrameByName(frameName)
        animation:addSpriteFrame(spriteFrame)
    end


    --设置两个帧播放时间
    animation:setDelayPerUnit(0.5)
    --动画执行后还原初始状态
    animation:setRestoreOriginalFrame(true)


    local action = cc.Animate3DQuality:create(animation)
    sprite:runAction(cc.RepeatForever:create(action))
    --动画结束




    --异步加载纹理缓存
    local function loadingTextureCallBack(texture)
        frameCache:addSpriteFrames(texture_plist)
        --初始化音乐
        AudioEngine.preloadMusic(bg_music_1)
        AudioEngine.preloadMusic(bg_music_2)
        --初始化音效
        AudioEngine.preloadEffect(sound_1)
        AudioEngine.preloadEffect(sound_2)


        local HomeScene = require("HomeScene")
        local scene = HomeScene.create()
        cc.Director:getInstance():pushScene(scene)
    end
    textureCache:addImageAsync(texture_res, loadingTextureCallBack)
    --异步加载纹理缓存完成
    --... 后面加东西


    return layer
end


function LoadingScene:onEnter()
    cclog("onEnter...")
end


function LoadingScene:onEnterTransitionFinish()


end


function LoadingScene:onExitTransitionStart()


end


function LoadingScene:cleanup()


end


return LoadingScene

你可能感兴趣的:(lua篇-打飞机)