CCSpriteBatchNode

时间:2011-05-03 09:59:22

标签: cocos2d-iphone

我在init方法中创建一个名为Player的类,我想使用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;
}

然后在HelloWorldScene中我想将这个类用于动画

Player *pl = [Player node];

[self addChild:pl.player];

但没有任何作用。我究竟做错了什么? 感谢。

1 个答案:

答案 0 :(得分:2)

这里你的代码有一些修改

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

要使用此代码,请致电

Player *pl = [Player node];

[self addChild:pl];

您是否试图像[self addChild:pl];而不是[self addChild:pl.player];那样打电话给您?