Reputation: 71
I'm creating a class named Player ... in the init method I want to use a CCSpriteBatchNode:
@interface Player : CCNode {
CCSprite *player;
CCSpriteBatchNode *spriteSheet;
CCAction *walkAction;
int playerSpeed;
int xPos;
int yPos;
}
@property (nonatomic, retain) CCSprite *player;
@property (nonatomic, retain) CCSpriteBatchNode *spriteSheet;
@property (nonatomic, retain) CCAction *walkAction;
@property int playerSpeed;
@property int xPos;
@property int yPos;
-(id)init {
if( (self=[super init] )) {
playerSpeed = 70;
xPos = 160;
yPos = 10;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bugA.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"bugA.png"];
[player useBatchNode:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 8 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bug%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
player = [CCSprite spriteWithSpriteFrameName:@"bug1.png"];
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[player runAction:walkAction];
[spriteSheet addChild:player];
}
return self;
}
then in HelloWorldScene I want to use this class with animation
Player *pl = [Player node];
[self addChild:pl.player];
but nothing works. What am I doing wrong? Thanks.
Upvotes: 0
Views: 3443
Reputation: 51
Here your code with some modification
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bugA.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"bugA.png"];
//add :
[self addChild:spriteSheet];
//instead of :
[player useBatchNode:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i = 1; i <= 8 ; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bug%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
player = [CCSprite spriteWithSpriteFrameName:@"bug1.png"];
//add to show the player in the middle of the screen
CGSize winSize = [CCDirector sharedDirector].winSize;
player.position = ccp(winSize.width/2, winSize.height/2);
walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[player runAction:walkAction];
[spriteSheet addChild:player];
To use this code just call
Player *pl = [Player node];
[self addChild:pl];
Did you try to call you class like that [self addChild:pl];
instead of [self addChild:pl.player];
?
Upvotes: 2