我想要CCSprite的子類,但由於某種原因,我似乎無法得到任何東西在屏幕上繪製。我不是一個與cocos2d親,所以我打開任何建議。這是我的代碼。我有一個場景像這樣:cocos2d子類化CCSprite
#import "cocos2d.h"
@class Animation;
@interface HelloWorldLayer : CCLayer
{
}
+(CCScene *) scene;
@end
#import "HelloWorldLayer.h"
#import "Animation.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if((self=[super init])) {
CCLOG(@"super init");
HelloWorldLayer *animation;
animation = [Animation animation:self];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
@end
於是我繼承CCSprite像這樣:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@class HelloWorldLayer;
@interface Animation : CCSprite {
HelloWorldLayer *ivHelloWorld;
}
+ (id) animation:(HelloWorldLayer*) helloWorld;
- (id) initAnimation: (HelloWorldLayer*) helloWorld;
@end
#import "Animation.h"
#import "HelloWorldLayer.h"
@implementation Animation
+ (id) animation:(HelloWorldLayer *) helloWorld{
CCLOG(@"animation method");
return [[[self alloc] initAnimation:helloWorld] autorelease];
}
- (id) initAnimation:(HelloWorldLayer *)helloworld {
CCLOG(@"InitaAnimation method");
// cache
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:@"FootballAtlas.plist"];
// frame array
NSMutableArray *framesArray=[NSMutableArray array];
for (int i=1; i<60; i++) {
NSString *frameName=[NSString stringWithFormat:@"Football%d.png", i];
id frameObject=[cache spriteFrameByName:frameName];
[framesArray addObject:frameObject];
}
CCLOG(@" array count = %i", [framesArray count]);
// animation object
id animObject=[CCAnimation animationWithFrames:framesArray delay:0.01];
// animation action
id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animAction];
id animAction2=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
animAction2=[CCRepeatForever actionWithAction:animAction2];
// sprite
CCSprite *bird=[CCSprite spriteWithSpriteFrameName:@"Football0.png"];
bird.position=ccp(60,160);
CCSprite *bird2=[CCSprite spriteWithSpriteFrameName:@"Football0.png"];
bird2.position=ccp(80,160);
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"FootballAtlas.png"];
[self addChild:batchNode];
[batchNode addChild:bird];
[batchNode addChild:bird2];
[bird runAction:repeat];
[bird2 runAction:animAction2];
return self;
}
@end
這不會在屏幕上畫任何東西,但如果我的動畫代碼移到CClayer然後一切節目很好。我究竟做錯了什麼?
你可以嘗試在'+(id)動畫:HelloWorldLayer'實現中用'[動畫分配]'替換'[self alloc]'嗎?另外,我沒有看到在initAnimation上傳遞HelloWorldLayer的理由 – yannicuLar
我想明白了,謝謝。我正在創建一個HelloWorldLayer對象,我需要有一個動畫對象。 – Stephen