封装了一个动画精灵类。。。需要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})
继续完善中。。。