2012-03-27 23 views
0

我想知道:我怎樣才能初始化類參數的對象?如何初始化帶有參數的類對象?

我有一個類來初始化精靈,我想使它更可用。讓類通過傳遞參數來初始化所有的精靈。

Somethong像

RolyPoly* new = [RolyPoly initWithSpriteName: [NSString stringWithFormat:@"rolypoly"]]; 

,並在課堂上PolyPoly用戶這個參數

NSDictionary *rolyPolyDict = [levelDict valueForKeyPath:@"rolypoly"]; 

我班

@interface RolyPoly : CCLayer { 

    CCAction *_walkAction; 
    CCSprite *_rolypoly; 
    double offsetx; 
    double offsety; 
} 

@property (nonatomic, retain) CCSprite *rolypoly; 
@property (nonatomic, retain) CCAction *walkAction; 
@property (nonatomic, assign) double offsetx; 
@property (nonatomic, assign) double offsety; 

+(id) scene; 
-(void) setOffx; 
@end 

//************************************************************** 


@implementation RolyPoly 

@synthesize rolypoly = _rolypoly; 
@synthesize walkAction = _walkAction; 
@synthesize offsetx = _offsetx; 
@synthesize offsety = _offsety; 

+(id) scene 
{ 
    CCScene *scene = [CCScene node]; 

    return scene; 
} 

-(id) init 
{ 
    if ((self = [super init])) 
    { 
     int currentLevel = [[SettingsManager sharedSettingsManager] getCurrentLevel]; 

     CGSize screenSize = [[CCDirector sharedDirector] winSize]; 

     NSData *xmlData = [NSData dataWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"gameScene" ofType:@"xml"]]; 

     NSError *error = nil; 

     NSDictionary *dictionary = [XMLReader dictionaryForXMLData:xmlData error:&error]; 
     NSDictionary *levelsDict = [dictionary valueForKeyPath:@"levels.level"]; 
     NSDictionary *levelDict; 

     for (levelDict in levelsDict) 
     { 

      int idLevel = [[levelDict valueForKeyPath:@"id"] intValue]; 
      if(idLevel==currentLevel) 
      { 
       NSDictionary *rolyPolyDict = [levelDict valueForKeyPath:@"rolypoly"]; 

       int count=[[rolyPolyDict valueForKeyPath:@"count"] intValue]; 

       [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: [NSString stringWithFormat:@"rolypoly_%d.plist", currentLevel]]; 
       CCSpriteBatchNode *rolypolySpriteSheet = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"rolypoly_%d.png", currentLevel]]; 
       [self addChild:rolypolySpriteSheet]; 
       NSMutableArray *rolypolyAnimFrames = [NSMutableArray array]; 
       for(int i = 1; i <= count; ++i) { 

        [rolypolyAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"rolypoly_%d_%d.png", currentLevel, i]]]; 
       } 
       CCAnimation *rolypolyAnim = [CCAnimation animationWithFrames:rolypolyAnimFrames delay:0.1f]; 
       self.rolypoly = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"rolypoly_%d_1.png", currentLevel]]; 

       self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:rolypolyAnim restoreOriginalFrame:NO]]; 
       [self.rolypoly runAction:self.walkAction]; 
       [rolypolySpriteSheet addChild:self.rolypoly z:1]; 

       self.offsetx=[[rolyPolyDict valueForKeyPath:@"offsetx"] doubleValue]; 
       self.offsety=[[rolyPolyDict valueForKeyPath:@"offsety"] doubleValue]; 
       self.position = ccp(screenSize.width*self.offsetx, screenSize.height*self.offsety); 

      }  
     } 

     [self scheduleUpdate]; 
    } 
    return self; 
} 
-(void) setOffx 
{ 
    // self.offsetx=x; 
    NSLog(@"dsfafdaf"); 
} 

-(void) update:(ccTime)delta 
{ 

} 
- (void) dealloc 
{ 
    self.rolypoly = nil; 
    self.walkAction = nil; 
    [super dealloc]; 
} 
    @end 

回答

0

我希望我理解正確的話,你想達到什麼樣的:

在你的類只要定義了一種新方法:

-(id) initWithSpriteName:(NSString *)spriteName 
{ 
    if self = [self init]; 
    { 
     NSDictionary *rolyPolyDict = [levelDict valueForKeyPath:@"rolypoly"]; 
    } 
} 

然後,只需調用它就像你通常會做的事:

RolyPoly *myRolyPoly = [[RolyPoly alloc] initWithSpriteName: [NSString stringWithFormat:@"rolypoly"]]; 
+0

後初始化RolyPoly * myRolyPoly = [[RolyPoly頁頭] initWithSpriteName:[的NSString stringWithFormat:@ 「rolypoly」]];在dealloc我應該[myRolyPoly發佈];不是嗎? – 2012-03-28 04:17:17

+0

非常感謝,它的工作原理。解決問題很簡單! – 2012-03-28 04:20:40