2

I want to set the class tile value by click on the button

  • if click onPlus it will have a string value = @"+"
  • if click onMinus it will have a string value = @"-"

here are the code

//Class Tile

@interface Tile : TouchableNode {
     NSString *val;
}

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

-(NSString *) getVal
{
    return val;
}

And the in another class

I have a code like this

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];
        }
    }
}

and I have onPlus & onMinus method that I want to send object tile to that method

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

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

How to pass the object tile to the method?? Or it have another way to make it??

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005

3 Answers3

0

If you want to send a Tile object to your onPlus handler, just subclass CCMenuItemImage and make a Tile property:

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

Here's a rough example of what your code might look like:

- (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;
}
Jeremy
  • 8,902
  • 2
  • 36
  • 44
0

CCMenuItemImage inherits from CCNode, which has a void* userData property. If you assign tile to userData, you could fetch it back from the (id)sender in your onPlus/onMinus methods.

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
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Ah... by the way when the pause layer is up with those two button .The screen is pause but the program still running... In other words I want to wait for the button action so that after I touch the button... the program can run continuously... So I want to know a wait for action funtion with the button too.. – Bancha Rojkittisakul Jan 06 '12 at 02:55
  • If the program is still running, wouldn't it just work the way that you coded it? Did you get a chance to try the `userData` trick? – Sergey Kalinichenko Jan 06 '12 at 04:27
  • Your code is work but it set to @"+" or @"-" after my function have finish.. so I get the value @"P" that I don't want it to be.So I want to wait until I press the button. – Bancha Rojkittisakul Jan 06 '12 at 06:20
  • The lastest 2 comment that I ask is a new question... The question is "How to wait until the CCmenuItemImage was press???" – Bancha Rojkittisakul Jan 06 '12 at 06:30
  • http://stackoverflow.com/questions/8754362/how-to-wait-until-the-ccmenuitemimage-is-press-in-objective-c – Bancha Rojkittisakul Jan 06 '12 at 06:48
0

For string comparisons, you're better off using

[tile.getVal isEqualToString:@"P"]

Since the method you use only works if it's the exact same string (in the same memory location), as opposed to an equivalent string elsewhere .

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
ObjectiveC-oder
  • 342
  • 1
  • 5