开发者

cocos2d animate

I try create animation in my application written on cocos2d. I make it on this tutorial http://getsetgames.com/tag/ccanimation/ All work fine. But I write all code in my Engine class. Also I have class for object which I would like animate. And I have special class for create object. Now

I try next File which must have animation spritesheet. // GiftSprite.h

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

开发者_Go百科@interface GiftSprite : CCSprite {
...
    CCSpriteSheet *giftSpriteSheet;
}
...
@property (assign, readwrite) CCSpriteSheet *giftSpriteSheet;
...


@end 

File witch create GiftSprite and method 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)];
}

And file which create Animation if some event catch.

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

And when I make it. on my animation I just see white square. How I can resolve this problem


I was having the same trouble until I read that when you subclass CCSprite you must setup initWithTexture and then use it in your custom init method. Mine is a little different because I am not using a sprite sheet, here is how I did it with my CCSprite subclass:

Header:

#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 file:

#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

Then in my main program init:

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

spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];

[self addChild:redButton];

You would do the same thing but incorporate it into your animations too. Does this help? I hope so. Happy coding!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜