0

在我尝试将 [sender duration] 传递给 int dur 之前,一切正常。它适用于 TAG,但不适用于我自己的 var。我已经尝试过 INT 和 NSinteger 的 durationSpell。我想要做的是:我有 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];
        }
4

1 回答 1

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];
}
于 2012-04-29T22:26:54.737 回答