• <tfoot id='skjG1'></tfoot>

      • <bdo id='skjG1'></bdo><ul id='skjG1'></ul>

        <legend id='skjG1'><style id='skjG1'><dir id='skjG1'><q id='skjG1'></q></dir></style></legend>
        <i id='skjG1'><tr id='skjG1'><dt id='skjG1'><q id='skjG1'><span id='skjG1'><b id='skjG1'><form id='skjG1'><ins id='skjG1'></ins><ul id='skjG1'></ul><sub id='skjG1'></sub></form><legend id='skjG1'></legend><bdo id='skjG1'><pre id='skjG1'><center id='skjG1'></center></pre></bdo></b><th id='skjG1'></th></span></q></dt></tr></i><div id='skjG1'><tfoot id='skjG1'></tfoot><dl id='skjG1'><fieldset id='skjG1'></fieldset></dl></div>

        <small id='skjG1'></small><noframes id='skjG1'>

        从 iPhone &amp; 中的类类型(+)方法访问对象Cocos2d?

        时间:2024-08-12

            <small id='wGu6I'></small><noframes id='wGu6I'>

          • <i id='wGu6I'><tr id='wGu6I'><dt id='wGu6I'><q id='wGu6I'><span id='wGu6I'><b id='wGu6I'><form id='wGu6I'><ins id='wGu6I'></ins><ul id='wGu6I'></ul><sub id='wGu6I'></sub></form><legend id='wGu6I'></legend><bdo id='wGu6I'><pre id='wGu6I'><center id='wGu6I'></center></pre></bdo></b><th id='wGu6I'></th></span></q></dt></tr></i><div id='wGu6I'><tfoot id='wGu6I'></tfoot><dl id='wGu6I'><fieldset id='wGu6I'></fieldset></dl></div>
              <legend id='wGu6I'><style id='wGu6I'><dir id='wGu6I'><q id='wGu6I'></q></dir></style></legend>

              • <bdo id='wGu6I'></bdo><ul id='wGu6I'></ul>
                <tfoot id='wGu6I'></tfoot>

                  <tbody id='wGu6I'></tbody>

                  本文介绍了从 iPhone &amp; 中的类类型(+)方法访问对象Cocos2d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类方法,我在其中创建和返回类对象.但我想在同一个类中访问该对象的某些属性.作为一个类方法,我不能在 .h 文件中声明变量,然后在其他方法中访问它.以下是代码如何在下面的实例方法中访问 backsprite 或 hudlayer 对象的值??

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

                  我无法通过标签访问它,因为这里没有添加 + 方法中的 HUDlayer 对象,而是在调用该方法的另一个类中.

                  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.

                  推荐答案

                  如果你有一个指向该类实例的指针/引用,你绝对可以在一个类的静态方法中访问该类的实例/成员变量.这是一个简单的例子...抱歉,如果有错别字...

                  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

                  // HUDLayer.h

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

                  //HUDLayer.m

                  // 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(或任何地方)

                  // HelloWorldLayer.m (or wherever)

                  HUDLayer* hud = [[HUDLayer alloc] init];
                  [HUDLayer modifyInstanceString:hud];
                  

                  这篇关于从 iPhone &amp; 中的类类型(+)方法访问对象Cocos2d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Cocos2d &amp; 中获取身体的接触点Box2d 下一篇:如何在 spritebuilder-Cocos2d 3.2 中添加单独的 iPad 和 iPhone 图像

                  相关文章

                  <small id='eLF5F'></small><noframes id='eLF5F'>

                  <legend id='eLF5F'><style id='eLF5F'><dir id='eLF5F'><q id='eLF5F'></q></dir></style></legend>

                  <tfoot id='eLF5F'></tfoot>

                  <i id='eLF5F'><tr id='eLF5F'><dt id='eLF5F'><q id='eLF5F'><span id='eLF5F'><b id='eLF5F'><form id='eLF5F'><ins id='eLF5F'></ins><ul id='eLF5F'></ul><sub id='eLF5F'></sub></form><legend id='eLF5F'></legend><bdo id='eLF5F'><pre id='eLF5F'><center id='eLF5F'></center></pre></bdo></b><th id='eLF5F'></th></span></q></dt></tr></i><div id='eLF5F'><tfoot id='eLF5F'></tfoot><dl id='eLF5F'><fieldset id='eLF5F'></fieldset></dl></div>
                      <bdo id='eLF5F'></bdo><ul id='eLF5F'></ul>