gpt4 book ai didi

ios - 如何将 Cocos2d Sprite Frames 封装在一个类中?

转载 作者:行者123 更新时间:2023-11-29 13:11:46 24 4
gpt4 key购买 nike

所以在我的 cocos2d-iphone 游戏中,我有一个 Action 层 - 所有的魔法都在这里发生,然后我有我的敌人 Sprite 和我的主角 Sprite 的类。

在我的 Action 层中,我在 init 上添加了 Sprite 帧,但这感觉应该封装到它们所属的类而不是它们所在的层中,但我不想添加它们超过一次.

我怎样才能做到只有在使用该类时才将帧添加到 spriteFrameCache 中?如果我将以下代码放入外星 Sprite 的 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 ]];

现在这感觉就像是语义自杀,有什么方法可以封装这种行为吗?

最佳答案

你绝对应该继承 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];

注意 此代码未经测试,只是将我想到的内容写入浏览器,所以预计它不会工作,但它应该让您对我正在尝试的内容有一个简单的了解进行交流 :),即使它有效,也将其视为伪代码;)

干杯!

关于ios - 如何将 Cocos2d Sprite Frames 封装在一个类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17126046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com