【问题标题】:cocos2d subclassing CCSpritecocos2d 子类化 CCSprite
【发布时间】:2012-08-30 16:44:33
【问题描述】:

我正在尝试继承 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

然后我像这样子类化 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) animation:HelloWorldLayer”实现中将“[self alloc]”替换为“[Animation alloc]”吗?另外,我看不到在 initAnimation 上传递 HelloWorldLayer 实例的原因
  • 我明白了,谢谢。我正在创建一个需要动画对象的 HelloWorldLayer 对象。

标签: cocos2d-iphone


【解决方案1】:

你只是忘了初始化你的动画精灵:

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

并且要小心您的 HelloWorldLayer,因为您没有将动画添加为子动画(并且变量输入不正确)。如果您想在场景中看到某些内容,请更改它。 :)

【讨论】:

    【解决方案2】:

    我明白了,谢谢。我正在初始化一个需要动画对象的 HelloWorldLayer 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多