lua on cocos2dx 札记(二)


封装了一个动画精灵类。。。需要extern.lua支持


function debug(...)
    CCLuaLog("debug() start @"..os.date())
    --CCLuaLog("debug() msg: "..string.format(...))
    CCLuaLog("debug() msg: "..tostring(...))
    CCLuaLog("debug() end @"..os.date())
end


function errBox(...)
    CCMessageBox(tostring(...),"Error")
end


require "extern"

-- a_Sprite: animated sprite 
-- START
a_Sprite = class("a_Sprite",
    function(spriteFrame)
        return CCSprite:createWithSpriteFrame(spriteFrame)  
    end
)
a_Sprite.__index = a_Sprite


function a_Sprite:create(...)
    
    --local variable corresponds to params
    local fileName, frameWidth, frameHeight, framesAmt, defaultFrameIndex, frameDelay, spritePaused, frameDirection, scrollDirection, posX, posY


    for k,v in pairs({...}) do 
        
        --required params
        if v.fileName ~= nil then
            fileName = v.fileName
        else
            errBox("sprite file name is missing")            
        end


        if v.frameWidth ~= nil then
            frameWidth = v.frameWidth
        else
            errBox("sprite frame width is missing")
        end
        
        if v.frameHeight ~= nil then
            frameHeight = v.frameHeight
        else
            errBox("sprite frame height is missing")
        end
        
        if v.framesAmt ~= nil then
            framesAmt = v.framesAmt
        else
            errBox("sprite frames amount is missing")
        end
        
        --optional param
        if v.defaultFrameIndex ~= nil then
            defaultFrameIndex = v.defaultFrameIndex
        else
            defaultFrameIndex = 0
        end


        if v.frameDelay ~= nil then
            frameDelay = v.frameDelay
        else
            frameDelay = 0.5
        end


        if v.spritePaused ~= nil then
            spritePaused = v.spritePaused
        else
            spritePaused = false
        end
        
        if v.frameDirection ~= nil then
            frameDirection = v.frameDirection
        else
            frameDirection = "H"
        end
        
        if v.scrollDirection ~= nil then
            scrollDirection = v.scrollDirection
        else
            scrollDirection = "H"
        end


        if v.posX ~= nil then
            posX = v.posX
        else
            local visibleSize = CCDirector:sharedDirector():getVisibleSize()
            local origin = CCDirector:sharedDirector():getVisibleOrigin()
            if scrollDirection == "H" then
                posX = origin.x + frameWidth / 2
            else
                posX = origin.x + visibleSize.width / 2
            end
        end


        if v.posY ~= nil then
            posY = v.posY
        else
            local visibleSize = CCDirector:sharedDirector():getVisibleSize()
            local origin = CCDirector:sharedDirector():getVisibleOrigin()
            if scrollDirection == "H" then
                posY = origin.y + visibleSize.height / 2
            else
                posY = origin.y + frameHeight / 2
            end
        end


    end
    
    local texture = CCTextureCache:sharedTextureCache():addImage(fileName)
    
    local frames = CCArray:create()
        
    local rect 
    
    if frameDirection == "H" then
        for i = 1, framesAmt do
            rect = CCRectMake((i-1)*frameWidth, 0, frameWidth, frameHeight)
            frames:addObject(CCSpriteFrame:createWithTexture(texture, rect))
        end  
    elseif frameDirection == "V" then
        for i = 1, framesAmt do
            rect = CCRectMake(0, (i-1)*frameHeight, frameWidth, frameHeight)
            frames:addObject(CCSpriteFrame:createWithTexture(texture, rect))
        end 
    end
    
    local sprite = a_Sprite.new(frames:objectAtIndex(defaultFrameIndex))
    sprite.isPaused = spritePaused
    sprite:setPosition(posX, posY)
    
    local animation = CCAnimation:createWithSpriteFrames(frames, frameDelay)
    local animate = CCAnimate:create(animation);
    sprite:runAction(CCRepeatForever:create(animate))


    return sprite
end
-- END


调用:

local dog = a_Sprite:create({fileName="dog.png", frameWidth=105, frameHeight=95, framesAmt=2})


继续完善中。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值