我正在尝试学习Objective-C,以及如何用cocos2d为iOS制作一些简单的动画。
我正在使用本教程,并让动画工作,但我想将代码从主类中取出,并将其放入一个单独的类中。(http://www.raywenderlich.com/32045/how-to-use-animations-and-sprite-sheets-in-cocos2d-2-x)
所以我创建了一个名为Pet的新类,它扩展了CCNode (这里是接口):
@interface Pet : CCNode {
BOOL moving;
int touchCount;
}
@property (nonatomic, assign) CCAction *walkAction;
@property (nonatomic, assign) CCAction *cuteAction;
@property (nonatomic, assign) CCAction *moveAction;
@property (nonatomic, assign) CCSprite *sprite;
- (id) initPetWithName:(NSString *)name xPosition:(int)x yPosition:(int)y;
- (BOOL) isTouchedByPoint:(CGPoint)point;
- (void) moveToPoint:(CGPoint)point;
@end它有一个CCSprite对象,该对象在初始化时初始化为:
self.sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@_%d.png", name, 4]];初始化过程中没有错误,但它不会显示在屏幕上。
CCLayer类在其初始化方法中包含以下内容:
-(id) init {
if((self = [super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.pet = [[Pet alloc] initPetWithName:@"Waffles"
xPosition:winSize.width/2 yPosition:winSize.height/2];
[self addChild:self.pet];
self.isTouchEnabled = YES;
}
return self;
}发布于 2013-04-17 05:36:46
我不明白你把精灵作为孩子加到哪里了。会是这样吗?
我曾经写过一个article that explains many potential causes for curiously absent nodes.
https://stackoverflow.com/questions/16046622
复制相似问题