2010-10-28 7 views
1

J'essaye de créer une animation dans mon application écrite sur cocos2d. Je le fais sur ce tutoriel http://getsetgames.com/tag/ccanimation/ Tout fonctionne bien. Mais j'écris tout le code dans ma classe de moteur. J'ai aussi classe pour objet que je voudrais animer. Et j'ai une classe spéciale pour créer un objet. Maintenantcocos2d animate

J'essaie maintenant Fichier qui doit avoir une feuille de sprites d'animation. // GiftSprite.h

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 
#import "LevelScene.h"; 

@interface GiftSprite : CCSprite { 
... 
    CCSpriteSheet *giftSpriteSheet; 
} 
... 
@property (assign, readwrite) CCSpriteSheet *giftSpriteSheet; 
... 


@end 

sorcière fichier créer GiftSprite et méthode addGift

-(void) addGift: (ccTime) time { 
gift.giftSpriteSheet = [CCSpriteSheet spriteSheetWithFile:@"redGift.png"];// In this place I try retain. 
gift = [GiftSprite spriteWithTexture:gift.giftSpriteSheet.texture rect:CGRectMake(0,0,30,30)]; 
} 

et le fichier qui crée l'animation si quelques captures d'événements.

NSLog(@"%@", gift.giftSpriteSheet);// in this place I see NULL 
      CCAnimation *fallingShowAnimation = [CCAnimation animationWithName:@"fallingInSnow" delay:0.5f]; 
      for(int x=0;x<6;x++){ 
       CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:gift.giftSpriteSheet.texture rect:CGRectMake(x*30,0,30,30) offset:ccp(0,0)]; 
       [fallingShowAnimation addFrame:frame]; 
      } 
      CCAnimate *giftAnimate = [CCAnimate actionWithAnimation:fallingShowAnimation]; 
      [gift runAction:giftAnimate]; 

Et quand je le fais. sur mon animation, je vois juste un carré blanc. Comment je peux résoudre ce problème

Répondre

0

J'ai eu le même problème jusqu'à ce que je lise que lorsque vous sous-classe CCSprite vous devez configurer initWithTexture et ensuite l'utiliser dans votre méthode init personnalisée. Le mien est un peu différent parce que je ne suis pas en utilisant une feuille de sprite, voici comment je l'ai fait avec ma sous-classe CCSprite:

tête:

#import "cocos2d.h" 

typedef enum tagButtonState { 
    kButtonStatePressed, 
    kButtonStateNotPressed 
} ButtonState; 

typedef enum tagButtonStatus { 
    kButtonStatusEnabled, 
    kButtonStatusDisabled 
} ButtonStatus; 

@interface spuButton : CCSprite <CCTargetedTouchDelegate> { 
@private 
    ButtonState state; 
    CCTexture2D *buttonNormal; 
    CCTexture2D *buttonLit; 
    ButtonStatus buttonStatus; 

} 

@property(nonatomic, readonly) CGRect rect; 

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture; 

- (void)setNormalTexture:(CCTexture2D *)normalTexture; 
- (void)setLitTexture:(CCTexture2D *)litTexture; 
- (BOOL)isPressed; 
- (BOOL)isNotPressed; 

@end 

fichier .m:

#import "spuButton.h" 
#import "cocos2d.h" 

@implementation spuButton 

- (CGRect)rect 
{ 
    CGSize s = [self.texture contentSize]; 
    return CGRectMake(-s.width/2, -s.height/2, s.width, s.height); 
} 

+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture 
{ 
    return [[[self alloc] initWithTexture:normalTexture] autorelease]; 
} 

- (void)setNormalTexture:(CCTexture2D *)normalTexture { 
    buttonNormal = normalTexture; 
} 
- (void)setLitTexture:(CCTexture2D *)litTexture { 
    buttonLit = litTexture; 
} 

- (BOOL)isPressed { 
    if (state == kButtonStateNotPressed) return NO; 
    if (state == kButtonStatePressed) return YES; 
    return NO; 
} 

- (BOOL)isNotPressed { 
    if (state == kButtonStateNotPressed) return YES; 
    if (state == kButtonStatePressed) return NO; 
    return YES; 
} 

- (id)initWithTexture:(CCTexture2D *)aTexture 
{ 
    if ((self = [super initWithTexture:aTexture])) { 

     state = kButtonStateNotPressed; 
    } 

    return self; 
} 

- (void)onEnter 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
    [super onEnter]; 
} 

- (void)onExit 
{ 
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; 
    [super onExit]; 
} 

- (BOOL)containsTouchLocation:(UITouch *)touch 
{ 
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]); 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    if (state == kButtonStatePressed) return NO; 
    if (![self containsTouchLocation:touch]) return NO; 
    if (buttonStatus == kButtonStatusDisabled) return NO; 

    state = kButtonStatePressed; 
    [self setTexture:buttonLit]; 

    return YES; 
} 

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    // If it weren't for the TouchDispatcher, you would need to keep a reference 
    // to the touch from touchBegan and check that the current touch is the same 
    // as that one. 
    // Actually, it would be even more complicated since in the Cocos dispatcher 
    // you get NSSets instead of 1 UITouch, so you'd need to loop through the set 
    // in each touchXXX method. 

    if ([self containsTouchLocation:touch]) return; 

    state = kButtonStateNotPressed; 
    [self setTexture:buttonNormal]; 

} 

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
state = kButtonStateNotPressed; 
    [self setTexture:buttonNormal]; 


} 
@end 

Ensuite, dans mon programme principal init:

CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:@"RedButtonNormal.png"]; 

spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal]; 

[self addChild:redButton]; 

Vous feriez la même chose mais l'intégrer dans vos animations aussi. est-ce que cela aide? J'espere. Codage heureux!