#import
#import "cocos2d.h"
@interface BGSpriteActionCreatManger : NSObject {
}
+(BGSpriteActionCreatManger *)sharedAction;
//创建一个一直播放的动画
-(CCAction*)initActionWithSpriteName:(NSString*)name FrameCount:(int)frameCount DelayTime:(float)delayTime;
//创建一个只播放一次的动画
-(CCAction*)initActionRunOnceWithSpriteName:(NSString*)name FrameCount:(int)frameCount DelayTime:(float)delaytime;
@end
Objective-C的实现文件 “BGSpriteActionCreatManger.m”或者 “BGSpriteActionCreatManger.mm”当你的游戏中要用物理引擎的时候,就应该是.mm,其实xcode开发环境是我用过的最好的开发工具了。可能我用别的东西用的少吧,大家不要喷。你如果想用纯 C++,那就的实现文件必须用.cpp。只要用了这个文件后缀, Objective-C的语法就不能用了。好了,不扯了,我们就正事。#import "BGSpriteActionCreatManger.h"
@interface BGSpriteActionCreatManger() //这个是Objective-C私有方法定方式
-(CCAnimation*)initActionBaseWithSpriteName:(NSString*)name FrameCount:(int)frameCount DelayTime:(float)delayTime;
@end
@implementation BGSpriteActionCreatManger
static BGSpriteActionCreatManger *_sharedAction = nil;
+ (BGSpriteActionCreatManger *)sharedAction
{
@synchronized(self){
if (!_sharedAction) {
_sharedAction = [[self alloc] init];
}
}
return _sharedAction;
}
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(CCAnimation*)initActionBaseWithSpriteName:(NSString*)name FrameCount:(int)frameCount DelayTime:(float)delayTime
{
NSMutableArray* frma = [[NSMutableArray alloc]initWithCapacity:frameCount];
NSString* file;
for (int i = 0;i < frameCount ;i++)
{
file = nil;
file = [[NSString alloc]initWithFormat:@"%@%i.png",name,i];
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame* theFrame = [frameCache spriteFrameByName:file];
[frma addObject:theFrame];
}
return [[CCAnimation alloc]initWithSpriteFrames:frma];
}
-(CCAction*)initActionWithSpriteName:(NSString *)name FrameCount:(int)frameCount DelayTime:(float)delayTime
{
CCAnimation* animationMove = [self initActionBaseWithSpriteName:name FrameCount:frameCount DelayTime:delayTime];
CCAnimate* Sup = [[CCAnimate alloc]initWithAnimation:animationMove];//CCAnimate::create(animationMove);
CCRepeatForever* repeat = [[CCRepeatForever alloc]initWithAction:Sup];//CCRepeatForever::create(Sup);
return repeat;
}
-(CCAction*)initActionRunOnceWithSpriteName:(NSString*)name FrameCount:(int)frameCount DelayTime:(float)delaytime
{
CCAnimation* animationMove = [self initActionBaseWithSpriteName:name FrameCount:frameCount DelayTime:delaytime];
CCAnimate* Sup = [[CCAnimate alloc]initWithAnimation:animationMove];
CCActionInterval* timeAction = (CCActionInterval*)Sup;
return timeAction;
}
@end
#ifndef __SPRITEACTIONCREATMANGER_H__
#define __SPRITEACTIONCREATMANGER_H__
#include "cocos2d.h"
class SpriteActionCreatManger: public cocos2d::CCObject
{
public:
virtual bool init();
//单例访问入口
static SpriteActionFactoryMangerClass* sharedAction(void);
//创建一个一直播放的动画
cocos2d::CCAction* initActionWithSpriteNameAndCountAndDelayTime(cocos2d::CCString* name,int frameCount,float delayTime);
//创建一个只播放一次的动画cocos2d::CCAction* initActionWithSpriteNameAndCountAndDelayTimeRunOnce(cocos2d::CCString* name,int frameCount,float delayTime);
//下边这个是cocos2d-x独有的creat宏CREATE_FUNC(SpriteActionFactoryMangerClass);
private:
cocos2d::CCAnimation* initActionWithSpriteNameAndFrameCountAndDelayTime(cocos2d::CCString* name,int frameCount,float delayTime);//C++中的私有方法
};
#endif
好了,我们来接着写实现文件.cpp#include "SpriteActionFactoryMangerClass.h"
//C++特有语法名称空间
using namespace cocos2d;
//单例访问入口实现,这里不像Objective-C的实现有多线程保护
static SpriteActionFactoryMangerClass *s_SharedAction = NULL;
SpriteActionFactoryMangerClass* SpriteActionFactoryMangerClass::sharedAction(void)
{
if (!s_SharedAction)
{
s_SharedAction = new SpriteActionFactoryMangerClass();
s_SharedAction->init();
}
return s_SharedAction;
}
//创建动画中的私有方法实现
CCAnimation* SpriteActionFactoryMangerClass::initActionWithSpriteNameAndFrameCountAndDelayTime(CCString* name,int frameCount,float delayTime)
{
CCArray* frma = CCArray::createWithCapacity(frameCount);
CCString* file;
for (int i = 0;i < frameCount ;i++)
{
file = NULL;
file = CCString::createWithFormat("%s%d.png",name->getCString(),i);
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCSpriteFrame* theFrame = frameCache->spriteFrameByName(file->getCString());
frma->addObject(theFrame);
}
return CCAnimation::createWithSpriteFrames(frma,delayTime);
}
//创建一个可以一直循环播放的动画CCAction* SpriteActionFactoryMangerClass::initActionWithSpriteNameAndCountAndDelayTime(cocos2d::CCString* name,int frameCount,float delayTime)
{
CCAnimation* animationMove = this->initActionWithSpriteNameAndFrameCountAndDelayTime(name,frameCount,delayTime);
CCAnimate* Sup = CCAnimate::create(animationMove);
CCRepeatForever* repeat = CCRepeatForever::create(Sup);
return repeat;
}
//创建一个只播放一次的动画CCAction* SpriteActionFactoryMangerClass::initActionWithSpriteNameAndCountAndDelayTimeRunOnce(cocos2d::CCString* name,int frameCount,float delayTime)
{
CCAnimation* animationMove = this->initActionWithSpriteNameAndFrameCountAndDelayTime(name,frameCount,delayTime);
CCAnimate* Sup = CCAnimate::create(animationMove);
CCActionInterval* timeAction = (CCActionInterval*)Sup;
return timeAction;
}