2010-10-28 88 views
1

我嘗試在寫在cocos2d上的應用程序中創建動畫。我在本教程中使用它 http://getsetgames.com/tag/ccanimation/ 一切正常。但是我在Engine類中編寫了所有代碼。另外我也有課堂,我想要動畫。我有創建對象的特殊類。現在cocos2d動畫

我嘗試它必須有動畫spritesheet未來 文件。 // GiftSprite.h

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

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


@end 

文件女巫創建GiftSprite和方法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)]; 
} 

和文件,如果一些事件漁獲物製作動畫。

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]; 

當我做到了。在我的動畫中,我只看到了白色方塊。 我該如何解決這個問題

回答

0

我有同樣的麻煩,直到我讀到你的子類CCSprite時,你必須設置initWithTexture,然後在你的自定義init方法中使用它。煤礦是一個有點不同,因爲我不使用精靈表,這裏是我如何用我的CCSprite子做的:

頁眉:

#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 

.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 

然後在我的主程序初始化:

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

spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal]; 

[self addChild:redButton]; 

你會做同樣的事情,但納入你的動畫也。這有幫助嗎?但願如此。快樂的編碼!