2011-05-03 39 views
0

我創建了一個名爲Player類......在init方法我想用一個CCSpriteBatchNode: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]; 

但沒有任何工作。我究竟做錯了什麼? 謝謝。

回答

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

相關問題