Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:46:15+00:00 2026-06-01T02:46:15+00:00

I would like to know: how can i initialize class object with parameters? I’ve

  • 0

I would like to know: how can i initialize class object with parameters?

I’ve got a class to initialize sprite and i would like to make it more usable. Make class to initialize all sprites by passing parameter.

Somethong like

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

and in class PolyPoly user this parameter in

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

My class

@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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:46:16+00:00Added an answer on June 1, 2026 at 2:46 am

    I hope I am understanding correctly, what you are trying to achieve:

    Just define a new method in your class:

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

    Then just call it like you would usually do:

    RolyPoly *myRolyPoly = [[RolyPoly alloc] initWithSpriteName: [NSString stringWithFormat:@"rolypoly"]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like know some reference. I know i can googling it. but prefer
I would like to know how can i read a value of input text
I would like to know how can I use tinyint in SQL Server 2005
I would like to know how can I use jQuery to select multiple items
I would like to know how can we validate the Credit Card. We have
I would like to know how can i write to a file within Application.
I would like to know how can I pass a ruby variable inside an
I am new here. I would like to know how can I display HTML
I would like to know if I can use the following JQuery function on
I would like to know how can I force a model loading in development

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.