Cocos2d iphone cocos2d子类化CCSprite

Cocos2d iphone cocos2d子类化CCSprite,cocos2d-iphone,Cocos2d Iphone,我正在尝试子类CCSprite,但由于某些原因,我似乎无法在屏幕上绘制任何东西。我不是cocos2d的专家,所以我愿意接受任何建议。这是我的密码。我有这样一个场景: #import "cocos2d.h" @class Animation; @interface HelloWorldLayer : CCLayer { } +(CCScene *) scene; @end #import "HelloWorldLayer.h" #import "Animation.h" @implem

我正在尝试子类CCSprite,但由于某些原因,我似乎无法在屏幕上绘制任何东西。我不是cocos2d的专家,所以我愿意接受任何建议。这是我的密码。我有这样一个场景:

#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
#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
然后我将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
#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
#导入
#导入“cocos2d.h”
@类HelloWorldLayer;
@界面动画:CCSprite{
HelloWorldLayer*ivHelloWorld;
}
+(id)动画:(HelloWorldLayer*)helloWorld;
-(id)初始化动画:(HelloWorldLayer*)helloWorld;
@结束
#导入“Animation.h”
#导入“HelloWorldLayer.h”
@实现动画
+(id)动画:(HelloWorldLayer*)helloWorld{
CCLOG(“动画方法”);
返回[[[self alloc]initAnimation:helloWorld]autorelease];
}
-(id)初始化动画:(HelloWorldLayer*)helloworld{
CCLOG(“初始模拟方法”);
//缓存
CCSpriteFrameCache*cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:@“FootballAtlas.plist”];
//帧阵列
NSMutableArray*framesArray=[NSMutableArray];

对于(int i=1;i您刚刚忘记初始化动画精灵:

- (id) initAnimation:(HelloWorldLayer *)helloworld {
   if ((self = [super init]))
   {
      // PUT YOUR CODE HERE
   }
   return self;
}

在HelloWorldLayer中要小心,因为您没有将动画作为子对象添加(并且变量输入不正确)。如果您想在场景中看到一些东西,请更改此设置。

我找到了答案,谢谢。我正在初始化HelloWorldLayer对象,我需要一个动画对象。

您可以尝试替换“[self alloc]在“+(id)Animation:HelloWorldLayer”实现中使用“[Animation alloc]”?此外,我不认为在initanimation上传递HelloWorldLayer InAsance的原因。谢谢。我正在创建一个HelloWorldLayer对象,我需要一个Animation对象。