2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCAnimation.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureCache.h"
|
|
|
|
#include "textures/CCTexture2D.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "ccMacros.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "sprite_nodes/CCSpriteFrame.h"
|
|
|
|
#include "cocoa/CCZone.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCAnimationFrame::CCAnimationFrame()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _spriteFrame(NULL)
|
|
|
|
, _delayUnits(0.0f)
|
|
|
|
, _userInfo(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimationFrame::initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo)
|
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimationFrame::~CCAnimationFrame()
|
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
CCLOGINFO( "cocos2d: deallocing %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_spriteFrame);
|
|
|
|
CC_SAFE_RELEASE(_userInfo);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* CCAnimationFrame::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCAnimationFrame* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy = (CCAnimationFrame*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pCopy = new CCAnimationFrame();
|
2012-08-21 05:33:34 +08:00
|
|
|
pNewZone = new CCZone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithSpriteFrame((CCSpriteFrame*)_spriteFrame->copy()->autorelease(),
|
|
|
|
_delayUnits, _userInfo != NULL ? (CCDictionary*)_userInfo->copy()->autorelease() : NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCAnimation
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAnimation* CCAnimation::create(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->init();
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->initWithSpriteFrames(frames, delay);
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
2012-06-27 14:21:29 +08:00
|
|
|
CCAnimation* CCAnimation::create(CCArray* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
2012-06-14 15:13:16 +08:00
|
|
|
pAnimation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
2012-04-19 14:35:52 +08:00
|
|
|
pAnimation->autorelease();
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::init()
|
|
|
|
{
|
|
|
|
return initWithSpriteFrames(NULL, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
bool CCAnimation::initWithSpriteFrames(CCArray *pFrames, float delay/* = 0.0f*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCARRAY_VERIFY_TYPE(pFrames, CCSpriteFrame*);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_loops = 1;
|
|
|
|
_delayPerUnit = delay;
|
2012-06-14 16:05:58 +08:00
|
|
|
CCArray* pTmpFrames = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
setFrames(pTmpFrames);
|
|
|
|
|
|
|
|
if (pFrames != NULL)
|
|
|
|
{
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(pFrames, pObj)
|
|
|
|
{
|
|
|
|
CCSpriteFrame* frame = (CCSpriteFrame*)pObj;
|
|
|
|
CCAnimationFrame *animFrame = new CCAnimationFrame();
|
|
|
|
animFrame->initWithSpriteFrame(frame, 1, NULL);
|
2013-06-15 14:03:30 +08:00
|
|
|
_frames->addObject(animFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
animFrame->release();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
|
|
|
|
{
|
|
|
|
CCARRAY_VERIFY_TYPE(arrayOfAnimationFrames, CCAnimationFrame*);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_delayPerUnit = delayPerUnit;
|
|
|
|
_loops = loops;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
setFrames(CCArray::createWithArray(arrayOfAnimationFrames));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCObject* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_frames, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimationFrame* animFrame = (CCAnimationFrame*)pObj;
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits += animFrame->getDelayUnits();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation::CCAnimation()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _totalDelayUnits(0.0f)
|
|
|
|
, _delayPerUnit(0.0f)
|
|
|
|
, _duration(0.0f)
|
|
|
|
, _frames(NULL)
|
|
|
|
, _restoreOriginalFrame(false)
|
|
|
|
, _loops(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation::~CCAnimation(void)
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d, deallocing %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_frames);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrame(CCSpriteFrame *pFrame)
|
|
|
|
{
|
|
|
|
CCAnimationFrame *animFrame = new CCAnimationFrame();
|
|
|
|
animFrame->initWithSpriteFrame(pFrame, 1.0f, NULL);
|
2013-06-15 14:03:30 +08:00
|
|
|
_frames->addObject(animFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
animFrame->release();
|
|
|
|
|
|
|
|
// update duration
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrameWithFileName(const char *pszFileName)
|
|
|
|
{
|
|
|
|
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);
|
|
|
|
CCRect rect = CCRectZero;
|
|
|
|
rect.size = pTexture->getContentSize();
|
2012-07-23 22:49:11 +08:00
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::createWithTexture(pTexture, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
addSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
|
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::createWithTexture(pobTexture, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
addSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
|
2013-06-14 08:25:14 +08:00
|
|
|
float CCAnimation::getDuration(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalDelayUnits * _delayPerUnit;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* CCAnimation::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCAnimation* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy = (CCAnimation*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pCopy = new CCAnimation();
|
2012-08-21 05:33:34 +08:00
|
|
|
pNewZone = new CCZone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
|
|
|
|
pCopy->setRestoreOriginalFrame(_restoreOriginalFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|