开发者

userdata of a CCNode

开发者 https://www.devze.com 2023-02-19 11:47 出处:网络
I have a CCMenuItemSprite that is instantiate to call a function: ` menuControl = [CCMenu menuWithItems: nil开发者_JAVA技巧];

I have a CCMenuItemSprite that is instantiate to call a function:

`

menuControl = [CCMenu menuWithItems: nil开发者_JAVA技巧];

//Create the buttons
for(int i=1;i<17;i++)
{
  Button *B_image = [Button buttonWithTexture:Sheet.texture type:i];

  CCMenuItemSprite *menuButton = [CCMenuItemSprite itemFromNormalSprite:B_image  selectedSprite:B_image target:self selector:@selector(generateEnemy:)];

NSNumber *Btype = [NSNumber numberWithInt:i];
menuButton.userData = Btype;

[menuControl addChild:menuButton];

}

[self addChild:menuControl];`

As you can see in the above, I'm using the menuButton.userData to pass in an NSNumber. This is to "pass a parameter" to the generateEnemy function.

`-(void) generateEnemy:(CCMenuItemSprite*)sender
{
    NSNumber *Btype = (NSNumber*)sender.userData;

        int Etype = [Btype intValue];

        enemy = [[Enemy alloc]init:ccp(300,0) Type:Etype];
}

It crashes at this: NSNumber *Btype = (NSNumber*)sender.userData; It works for number 1-12. But 13 and above, it crashes.


I've solve this problem quite a while back. It crashes due to this concept here.

How I pass data is:

for(int i=1;i<17;i++)
{
   Button *B_image = [Button buttonWithTexture:Sheet.texture type:i];

   CCMenuItemSprite *menuButton = [CCMenuItemSprite itemFromNormalSprite:B_image  selectedSprite:B_image target:self selector:@selector(generateEnemy:)];

   menuButton.tag = i;

   [menuControl addChild:menuButton];

}

In the generateEnemy function:

-(void) generateEnemy:(id) sender
{
      enemy = [[Enemy alloc]init:ccp(300,0) Type:[sender tag]];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消