2

我想通过单击按钮来设置类图块值

  • 如果单击 onPlus 它将有一个字符串值 = @"+"
  • 如果单击 onMinus,它将有一个字符串值 = @"-"

这是代码

//Class Tile

@interface Tile : TouchableNode {
     NSString *val;
}

-(void) setVal:(NSString *)v
{
    val = v;
}

-(NSString *) getVal
{
    return val;
}

而在另一个班级

我有这样的代码

for(Tile *tile in player)
{
    if (tile.getVal == @"P") {
        if (pauseStatus == 0) {
            pauseStatus = 1;

            [[CCDirector sharedDirector] pause];
            CGSize size = [[CCDirector sharedDirector] winSize];
            pauseLayer=[[CCLayer alloc] init];
            pauseLayer.anchorPoint=ccp(0,0);
            pauseLayer = [CCLayerColor layerWithColor: ccc4(0, 0, 255, 125) width: 300 height: 150];
            pauseLayer.position = ccp(size.width/2, size.height/2);       
            pauseLayer.isRelativeAnchorPoint = YES;
            [self addChild: pauseLayer z:8];

            //Here are 2 two button that when user click it will have @"+" value or @"-"
            plusBtn = [CCMenuItemImage itemFromNormalImage:@"plus.png" selectedImage:@"plus.png"     target:self selector:@selector(onPlus:)];
            minusBTn = [CCMenuItemImage itemFromNormalImage:@"minus.png" selectedImage:@"minus.png"      target:self selector:@selector(onMinus:)];

            pauseMenu = [CCMenu menuWithItems:plusBtn, minusBTn, nil];
            [pauseMenu alignItemsHorizontally];
            [self addChild:pauseMenu z:10];
        }
    }
}

我有 onPlus 和 onMinus 方法,我想将对象图块发送到该方法

-(void)onPlus:(Tile *) set
{
    NSString *plus = @"+";
    [set setVal:plus];
}

-(void)onMinus:(Tile *) set
{
    NSString *minus = @"-";
    [set setVal:minus];
}

如何将对象图块传递给方法?或者它有其他方法可以做到吗?

4

3 回答 3

0

如果你想向Tile你的处理程序发送一个对象onPlus,只需子类 CCMenuItemImage 并创建一个 Tile 属性:

@interface MyCustomCCMenuItemImage : CustomCCMenuItemImage
@property (nonatomic, retain) Tile* tile;
@end

这是您的代码可能看起来的粗略示例:

- (void)someMethod
{

    for(Tile *tile in player)
    {
        plusBtn = [MyCustomCCMenuItemImage itemFromNormalImage:@"plus.png"   selectedImage:@"plus.png" target:self selector:@selector(onPlus:)];
        plusBtn.tile = tile ;
    }

}

//Your handler
-(void)onPlus:(id)sender
{
    Tile *myTile = sender.tile;
}
于 2012-01-05T15:47:10.340 回答
0

CCMenuItemImage继承自CCNode,它有一个void* userData属性。如果你分配tile给,你可以从你的/方法中userData取回它。(id)senderonPlusonMinus

plusBtn = [CCMenuItemImage itemFromNormalImage:@"plus.png" selectedImage:@"plus.png"     target:self selector:@selector(onPlus:)];
plusBtn.userData = (void*)tile; // You may need a bridge cast in ARC
minusBTn = [CCMenuItemImage itemFromNormalImage:@"minus.png" selectedImage:@"minus.png"      target:self selector:@selector(onMinus:)];
minusBTn.userData = (void*)tile;

-(void)onPlus:(id)senderObj {
    CCNode *sender = (CCNode*)senderObj;
    Tile *myTile = (Tile*)sender.userData; // Again you may need a bridge cast here
}
于 2012-01-05T15:50:24.083 回答
0

对于字符串比较,最好使用

[tile.getVal isEqualToString:@"P"]

因为您使用的方法仅适用于完全相同的字符串(在相同的内存位置),而不是其他地方的等效字符串。

于 2012-01-05T15:53:52.257 回答