使用switch case全局变量

时间:2014-05-14 12:31:00

标签: objective-c cocos2d-iphone spritebuilder

在SpriteBuilder中,我想使用全局变量来改变Sprite的特性。以下是代码的简化版本:

在Grid.h中

#import "CCNodeColor.h"

extern int a;

@interface Grid : CCNodeColor
@property (nonatomic, assign) NSInteger score;

@end

在Grid.m中

#import "Grid.h"

@implementation Grid{
    NSMutableArray *_gridArray;
    NSNull *_noTile;  
  }

- (void)mergeTileAtIndex:(NSInteger)x y:(NSInteger)y withTileAtIndex:(NSInteger)xOtherTile y:(NSInteger)yOtherTile {
Tile* mergedTile = _gridArray[x][y];
Tile* otherTile = _gridArray[xOtherTile][yOtherTile];
self.score += mergedTile.value + otherTile.value;
otherTile.value *= 2;   
_gridArray[x][y] = _noTile;
switch(otherTile.value)
  {
    case 2:
        a = 2;
        break;
    case 4:
        a = 4;
        break;
    case 8:
        a = 8;
        break;
  }
}

@end

在MainScene.m

#import "MainScene.h"
#import "Grid.h"

int a;

@implementation MainScene{
CCSprite *_red;
CCSprite *_green;
CCSprite *_blue;
}

- (void)didLoadFromCCB {
[self showTile];
}

-(void) showTile {
if (a == 2)
    _red.visible = TRUE;
else if (a == 4)
    _green.visible = TRUE;
else
    _blue.visible = TRUE;
}

@end

我可以在Grid.m中切换大小写,但我无法将全局变量传递给MainScene.m。为什么?我如何使代码工作?我是否需要在switch语句中添加return?

0 个答案:

没有答案