Objective c cocos2d动画
我尝试在cocos2d上编写的应用程序中创建动画。我在这篇教程中做到了 一切正常。但我在引擎类中编写所有代码。此外,我有一个类的对象,我想动画。我有一个创建对象的特殊类。现在 下一步我试试 必须具有动画精灵表的文件。 //吉普斯普林特Objective c cocos2d动画,objective-c,animation,cocos2d-iphone,Objective C,Animation,Cocos2d Iphone,我尝试在cocos2d上编写的应用程序中创建动画。我在这篇教程中做到了 一切正常。但我在引擎类中编写所有代码。此外,我有一个类的对象,我想动画。我有一个创建对象的特殊类。现在 下一步我试试 必须具有动画精灵表的文件。 //吉普斯普林特 #import <Foundation/Foundation.h> #import "cocos2d.h" #import "LevelScene.h"; @interface GiftSprite : CCSprite { ... CCSp
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "LevelScene.h";
@interface GiftSprite : CCSprite {
...
CCSpriteSheet *giftSpriteSheet;
}
...
@property (assign, readwrite) CCSpriteSheet *giftSpriteSheet;
...
@end
和文件,如果某个事件被捕获,则创建动画
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];
NSLog(@“%@”,gift.giftSpriteSheet);//在这个地方我看到了空
cAnimation*fallingShowAnimation=[cAnimation Animation with name:@“FallingSnow”延迟:0.5f];
对于(int x=0;x我也遇到了同样的问题,直到我读到当你将CCSprite子类化时,你必须设置initWithTexture,然后在你的自定义init方法中使用它。我的有点不同,因为我没有使用sprite表,下面是我如何使用我的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
然后在我的主程序init中:
CCTexture2D *redButtonNormal = [[CCTextureCache sharedTextureCache] addImage:@"RedButtonNormal.png"];
spuButton *redButton = [spuButton spuButtonWithTexture:redButtonNormal];
[self addChild:redButton];
你会做同样的事情,但也会把它融入到你的动画中。这有帮助吗?我希望如此。编码快乐
#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];