CCNode的用户数据
我有一个 CCMenuItemSprite,它被实例化以调用一个函数:
`
menuControl = [CCMenu menuWithItems: nil];
//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];`
正如您在上面所看到的,我使用 menuButton.userData 来传入 NSNumber。 这是向generateEnemy函数“传递一个参数”。
`-(void) generateEnemy:(CCMenuItemSprite*)sender
{
NSNumber *Btype = (NSNumber*)sender.userData;
int Etype = [Btype intValue];
enemy = [[Enemy alloc]init:ccp(300,0) Type:Etype];
}
它崩溃于:NSNumber *Btype = (NSNumber*)sender.userData;
它适用于数字 1-12。但是13及以上就崩溃了。
I have a CCMenuItemSprite that is instantiate to call a function:
`
menuControl = [CCMenu menuWithItems: nil];
//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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很久以前就解决了这个问题。
由于这个概念此处,它崩溃了。
我传递数据的方式是:
在generateEnemy函数中:
I've solve this problem quite a while back.
It crashes due to this concept here.
How I pass data is:
In the generateEnemy function: