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

  • SEARCH
  • Home
  • 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 7401905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:36:11+00:00 2026-05-29T04:36:11+00:00

i have a class method in which i am creating and returning the class

  • 0

i have a class method in which i am creating and returning the class object.
But i want to access certain properties of that object in the same class. Being a class method i cannot declare the variable in .h file and later access it in other methods. Following is the code
How can i access the values of backsprite or hudlayer object in the instance method below??

    // class 1

+(id)HUDWithBackgroundSprite:(NSString *)spriteName withRect:(CGRect)rect atPoistion:(HUDPosition)pos
{

  HUDlayer *hud = [[HUDlayer alloc] init];

  CCSprite *backSprite = [CCSprite spriteWithFile:spriteName];
  [backSprite setContentSize:CGSizeMake(rect.size.width,rect.size.height)];
  [backSprite setPosition:ccp(rect.origin.x,rect.origin.y)];
  [backSprite setTag:100];
  [hud addChild:backSprite];
  [hud setTag:2];

  return [hud autorelease];
}


// access it here

-(void)AddButtonToHUDWithImage:(NSString *)imageName selector:(SEL)selector withDisabledImage:(NSString *)disbdImageName
{
  HUDlayer *hud = (HUDlayer *)[self getChildByTag:2];

  CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:imageName selectedImage:imageName disabledImage:disbdImageName target:self selector:@selector(selector)];

  CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil];

  CCSprite *sprite = (CCSprite *)[hud getChildByTag:100];

  CGPoint pos = sprite.position;
  //pos =[[CCDirector sharedDirector]convertToGL:pos];

  [menu setPosition:ccp(pos.x+160,pos.y-30)];

  [hud addChild:menu];
}

// class 2

+(CCScene *) scene
{
  // 'scene' is an autorelease object.
  CCScene *scene = [CCScene node];

    HUDlayer *hud = [HUDlayer node];
  //  [scene addChild:hud z:1];

  // 'layer' is an autorelease object.
  HelloWorldLayer *layer = [[[HelloWorldLayer alloc]initWithHUD:hud]autorelease];

  // add layer as a child to scene 
  [scene addChild: layer];

  id mainHuds = [HUDlayer HUDWithBackgroundSprite:HUDBackground withRect:CGRectMake(160,450, 300,60) atPoistion:pos_Top];

  SEL callOnClick;
  [mainHuds AddButtonToHUDWithImage:ButtonBackground selector:callOnClick withDisabledImage:disabledBackground];

  [scene addChild: mainHuds];

  // return the scene
  return scene;
}

i am not able to access it via tags either as HUDlayer object in + method is not added here but in the other class which calls the method.

  • 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-05-29T04:36:12+00:00Added an answer on May 29, 2026 at 4:36 am

    You can absolutely in a static method of a class access instance/member variable of that class IF you have a pointer/reference to an instance of that class. Here is a simple example… sorry if there are typos…

    // HUDLayer.h

    @interface HUDLayer : CCLayer
    {
        NSString* aString;
    }
    
    @property (nonatomic, retain) NSString* aString;
    
    +(void)modifyInstanceString:(HUDLayer*)hud;
    

    // HUDLayer.m

    @implementation HUDLayer
    
    @synthesize aString;
    
    -(id)init
    {
        if ((self=[super init]))
        {
            self.aString = @"initial value";
        }
        CCLOG(@"Initial value = %@", self.aString);
        return self;
    }
    
    +(void)modifyInstanceString:(HUDLayer*)hud
    {
        hud.aString = @"Something else";
        CCLOG(@"Modified aString via static method = %@", hud.aString);
    }
    
    @end
    

    // HelloWorldLayer.m (or wherever)

    HUDLayer* hud = [[HUDLayer alloc] init];
    [HUDLayer modifyInstanceString:hud];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has an output() method which returns a matplotlib Figure
I have a base class with a property which (the get method) I want
So I have a class foo that has a method which returns an array
I have an interface, IMessage and a class which have several methods for creating
I have a class with static method which has a local static variable. I
I have a VB6 class with a method which raises an error: Public Sub
The problem: I have a class which contains a template method execute which calls
I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
I have a class called EventConsumer which defines an event EventConsumed and a method
I have a class which inherits from Canvas. On the OnRender method I draw

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.