在 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吗?