所以在我的cocos2d-iphone游戏中,我有一个动作层 - 所有的魔法都发生了,然后我为我的敌人精灵和我的主要主角精灵上课。
在我的动作层中,我在init上添加了精灵帧,但这感觉它应该被封装到它们所属的类而不是它们所在的层中,但是我不想多次添加它们
如何在使用类时将帧添加到spriteFrameCache中?如果我将以下代码放在我的init
方法中用于外星精灵,它会让我的游戏陷入困境吗?
// Add Alien Hominid to sprite cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AlienHominid-ipadhd.plist"];
alienSheet = [CCSpriteBatchNode batchNodeWithFile:@"AlienHominid-ipadhd.png"];
[self addChild:alienSheet];
alienFrames = [NSMutableArray array];
for (int i=1; i <= 5; i++) {
[alienFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"AlienHominid %d.png", i]]];
if (i==5)
[alienFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"AlienHominid 1.png"]];
}
alienAnim = [CCAnimation animationWithSpriteFrames:alienFrames delay:0.1];
flip = [[CCRepeatForever alloc ] initWithAction:[CCAnimate actionWithAnimation:alienAnim ]];
现在感觉就像语义自杀一样,有没有办法来封装这种行为?
答案 0 :(得分:1)
你绝对应该将CCSprite子类化。
我们称之为alienHominid类,在您的头文件中,该类应该如下所示:
@interface alienHominid : CCSprite {
//helps you find which current frame is being displayed at any moment
int currentFrame;
CCAnimation *flip;
}
//Extra some extra params you might need ?
@property (readwrite) NSNumber *health;
//And your new init method
-(id)initWithSpriteSheet:(NSString*)aSpriteFrame andBatchNode:(NSString*)aBatchNode;
//Some other methods to animate and give it your required behaviours
-(void)startAnimations;
-(void)prepareAnimations;
-(void)stopAnimations;
-(void)removeFromLayer;
@end
现在让我们看一下您的实施文件
@implementation alienHominid
//Synth. properties you created here , if any.
@synthesize health = _health;
-(id)initWithSpriteSheet:(NSString*)aSpriteFrame andBatchNode:(NSString*)aBatchNode
{
self = [super init];
if(self)
{
//custom init code
[self setHealth:[NSNumber numberWithInt:100]];
//
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:aSpriteFrame];
alienSheet = [CCSpriteBatchNode batchNodeWithFile:aBatchNode];
}
return self;
}
//Some other methods to animate and give it your required behaviours
-(void)prepareAnimations{
alienFrames = [NSMutableArray array];
for (int i=1; i <= 5; i++) {
[alienFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"AlienHominid %d.png", i]]];
if (i==5)
[alienFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"AlienHominid 1.png"]];
}
CCAnimation *alienAnim = [CCAnimation animationWithSpriteFrames:alienFrames delay:0.1];
flip = [[CCRepeatForever alloc ] initWithAction:[CCAnimate actionWithAnimation:alienAnim]];
}
-(void)startAnimations{
[flip start];
}
-(void)stopAnimations{
//Stop animations here
[flip stop];
}
-(void)removeFromLayer{
//If alien dies, remove it here
}
@end
现在,您可以通过导入标题
轻松地在任何场景中使用它 #import "alienHominid.h"
并像这样添加:
alienHominid *alien = [[alienHominid alloc] initWithSpriteSheet:@"AlienHominid-ipadhd.plist" andBatchNode:@"AlienHominid-ipadhd.png"];
[alien prepareAnimations];
[alien startAnimations];
[self addChild:alien];
注意此代码未经过测试,只是将我想到的内容写入浏览器,所以期望它不起作用,但它应该让您简单了解我和# 39; m试图沟通:),即使它有效,也将其视为伪代码;)
干杯!