一切正常,直到我试图将[发送者持续时间]传递给int dur。它适用于TAG,但不适用于我自己的var。我为durationSpell尝试了INT和NSinteger。 我想要做的是: 我有10个不同的按钮触发spawnShoot,所有按钮都有不同的持续时间。我想从点击的按钮中获取持续时间。
@interface ClassUI : NSObject {
CCMenuItemImage *button;
CCSprite *shot;
int durationSpell;
}
-----------------
ClassUI *spellshealP1 = [[ClassUI alloc]init];
spellshealP1.button = [CCMenuItemImage itemFromNormalImage:@"smallHeal.png" selectedImage:@"healempty.png" target:self selector:@selector(spawnShoot:)];
spellshealP1.button.tag = 101;
spellshealP1.durationSpell = 10;
CCMenu *player1menu = [CCMenu menuWithItems:spellshealP1.button, spellbighealP1.button, spellcureP1.button, spellfocusP1.button, spellpoisonP1.button, spellbfBallP1.button, spellsfBallP1.button, nil];
player1menu.position = ccp(MoveMenuInXP1, (768/2) - (numberOfButtons*buttonSize/2) + buttonSize/2);
[self addChild:player1menu];
-----------------
-(IBAction)spawnShoot:(id)sender{
int tag = [sender tag];
int dur = [sender durationSpell];
}
答案 0 :(得分:1)
好的,问题在于:发件人不是spellSheal
,而是spellSheal.button
所以你试图获得spellSheal.button.durationSpell
,但事实上它是spellSheal.durationSpell
...
因此,最好的方法是让ClassUI继承CCMenuItemImage。 我就是这样做的:
@interface ClassUI : CCMenuItemImage {
CCSprite *shot;
int durationSpell;
}
-----------------
ClassUI* spellshealP1 = [ClassUI itemFromNormalImage:@"smallHeal.png" selectedImage:@"healempty.png" target:self selector:@selector(spawnShoot:)];
spellshealP1.tag = 101;
spellshealP1.durationSpell = 10;
-----------------
-(IBAction)spawnShoot:(id)sender{
int tag = [sender tag];
int dur = [sender durationSpell];
}