第一点、CCMoveTo,
简单的执行元素从A移动到B。
CCMoveTo * move_ten =CCMoveTo::create(1, CCPointMake(300,400));
元素addScore->runAction(move_ten);
第二点、CCSequence
如果执行连续的几个动作则可以使用CCSequence将Action打包。记得以NULL结束。
addScore->runAction(CCSequence::create(Action1,Action2,Action3,CCCallFunc::create(this,callfunc_selector(R2GameScene::add_score)),NULL));
第三点、利用plist生成逐帧动画。
先安装好TexturePackerGUI,排好图片序列,利用其生成plist和pvr.ccz文件。
然后自己封装一个读取plist的类
.h头文件
//
// BYAnimationTool.h
// COG
//
// Created by Yanghui Liu on 12-7-26.
// Copyright (c) 2012年 BoyoJoy. All rights reserved.
//
#ifndef COG_BYAnimationTool_h
#define COG_BYAnimationTool_h
#define kDelayPerUnit 0.08f
#include "cocos2d.h"
USING_NS_CC;
class BYAnimationTool {
public:
static CCActionInterval* createRFAnimFormPng(const char* pngName,const char* frameName,int beginFrameIndex,float DelayPerUnit=kDelayPerUnit);
//isReturn 是否返回第一帧
static CCActionInterval* createAnimFormPng( const char* pngName,const char* frameName,int beginFrameIndex=1,float DelayPerUnit=kDelayPerUnit,int endFrameIndex=1,bool isReturn=false );
static CCSpriteFrame* createSpriteFrame(const char* pngName,const char* frameName,int beginFrameIndex=1);
};
#endif
.cpp文件
//
// BYAnimationTool.cpp
// COG
//
// Created by Yanghui Liu on 12-7-26.
// Copyright (c) 2012年 BoyoJoy. All rights reserved.
//
#include "BYAnimationTool.h"
//将PNG序列图转成永久性动画
CCActionInterval* BYAnimationTool::createRFAnimFormPng(const char* pngName,const char* frameName,int beginFrameIndex,float DelayPerUnit)
{
return CCRepeatForever::create(createAnimFormPng(pngName,frameName,beginFrameIndex,DelayPerUnit));
}
CCActionInterval* BYAnimationTool::createAnimFormPng( const char* pngName,const char* frameName,int beginFrameIndex/*=1*/,float DelayPerUnit/*=kDelayPerUnit*/,int endFrameIndex/*=1*/,bool isReturn/*=false */ )
{
char str1[100] = {0};
char str2[100] = {0};
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
sprintf(str1, "%s.plist", pngName);
sprintf(str2, "%s.pvr.ccz", pngName);
cache->addSpriteFramesWithFile(str1,str2);
CCArray* animFrames = CCArray::create();
int i = beginFrameIndex;
do {
if (endFrameIndex>1&&(i-1)==endFrameIndex)
{
break;
}
sprintf(str2, "%s%d.png", frameName,i);
CCSpriteFrame *frame = cache->spriteFrameByName(str2);
if (frame) {
animFrames->addObject(frame);
}else {
break;
}
} while (++i);
//CCAnimation *animation = CCAnimation::create(animFrames, DelayPerUnit);
CCAnimation *animation =CCAnimation::createWithSpriteFrames(animFrames,DelayPerUnit);
cache->removeSpriteFramesFromFile(str1);
animation->setRestoreOriginalFrame(isReturn);
return CCAnimate::create(animation);
}
CCSpriteFrame* BYAnimationTool::createSpriteFrame( const char* pngName,const char* frameName, int beginFrameIndex)
{
char str1[100] = {0};
char str2[100] = {0};
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
sprintf(str1, "%s.plist", pngName);
sprintf(str2, "%s.pvr.ccz", pngName);
cache->addSpriteFramesWithFile(str1,str2);
CCArray* animFrames = CCArray::create();
int i = beginFrameIndex;
sprintf(str2, "%s%d.png", frameName,i);
CCSpriteFrame *frame = cache->spriteFrameByName(str2);
if (frame) {
return frame;
}
else{
return NULL;
}
}
然后再自己的类里面包含此头文件就可以使用了
anima[i]=CCSprite::createWithSpriteFrame(BYAnimationTool::createSpriteFrame(path_list[i],"tb_",1));
anima[i]->setPosition(ccp(xy[i][0],xy[i][1]));
this->addChild(anima[i]);
pStart[i]->setVisible(false);
anima[i]->runAction(BYAnimationTool::createAnimFormPng(path_list[i],"tb_",1,0.05f,11,false));
第四点、动作合成使用CCSpawn