我想知道:如何使用参数初始化类对象?
我有一个初始化精灵的类,我想让它更有用。使类通过传递参数来初始化所有精灵。
有些喜欢
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 :(得分:0)
我希望我正确理解你正在努力实现的目标:
只需在班级中定义一个新方法:
-(id) initWithSpriteName:(NSString *)spriteName
{
if self = [self init];
{
NSDictionary *rolyPolyDict = [levelDict valueForKeyPath:@"rolypoly"];
}
}
然后就像你通常那样打电话:
RolyPoly *myRolyPoly = [[RolyPoly alloc] initWithSpriteName: [NSString stringWithFormat:@"rolypoly"]];