我的CCMenuItem在选择器中有两个方法
menuItem = [[CCMenuItemSprite
itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"menuItem.png"]
selectedSprite:[CCSprite spriteWithSpriteFrameName:@"menuItem.png"]
target:self
selector:@selector(methodName:anotherParam:)] retain];我想从CCMenuItem发送一个整数,这样我就可以根据CCMenuItem的整数来切换大小写
如下所示:
- (void)methodName:(id *)sender anotherParam:(int *)intNumber {
CCMenuItemSprite *menuItem = (CCMenuItemSprite *)sender;
switch (anotherParam) {
case 1:
//My case
break;
}如何从CCMenuItem发送int以切换我的案例?
发布于 2012-02-06 22:11:24
只需标记CCMenuItem:
menuItem.tag = 1;然后从发送者参数中获取:
- (void)methodName:(id *)sender {
switch ([sender tag]) {
//cases depending on tag
}
}发布于 2012-02-06 21:42:27
为此,您可以使用CCNode的userData属性,也可以设置一个关联的对象。
例如:
CCNode *myNode = [CCNode node];
myNode.userData = (void *) intVal;
// in callback
CCNode *myNode = (CCNode *) sender;
int anotherParam = (int) myNode.userData; 要设置关联的对象,您可以执行如下操作:
objc_setAssociatedObject(myObject, "anotherParam", (id) myInt, OBJC_ASSOCIATION_ASSIGN);
// in callback
int anotherParam = (int) objc_getAssociatedObject(myObject, "anotherParam");请注意,键的类型为void *,因此您可以在其中放置任何值(NSObject、NSString、C-string、int值等)。
https://stackoverflow.com/questions/9160903
复制相似问题