在 iPhone 游戏的开发过程中,这花费了我很多时间。任何帮助是极大的赞赏!
设置
- 弧
- Cocos2d 2.0
问题
- 当我在 CCLabelTTF 上设置字符串时出现 EXC_BAD_ACCESS
- 每 5 次尝试大约发生 1 次
代码
以下代码来自 LevelSelectionLayer 类。该类是另一个类的委托,该类处理关卡网格和触摸交互。
在 LevelSelectionLayer.h
@property (strong) NSArray *difficulties;
@property (strong) CCLabelTTF *difficultyLabel;
@property (strong) SlidingMenuGrid *slidingMenuGrid;
在 LevelSelectionLayer.m
-(id) init
{
if (self = [super init]) {
self.difficulties = [[NSArray alloc] initWithObjects:@"Easy", @"Intermediate", @"Hard", nil];
[self initDifficultyLabel];
[self initGrid];
}
return self;
}
-(void) initDifficultyLabel
{
CGSize size = [[CCDirector sharedDirector] winSize];
self.difficultyLabel = [[CCLabelTTF alloc] initWithString:[self.difficulties objectAtIndex:0] fontName:FONT_BOLD fontSize:18.0f];
self.difficultyLabel.position = ccp(size.width / 2 + 3, size.height / 2 + 63);
self.difficultyLabel.color = CCC3_DARK_COLOR;
[self addChild:self.difficultyLabel z:5];
}
在 LevelSelectionLayer.m 中(续)。这是从上述网格类调用的委托方法。
-(void) slidingGridDidChangePage
{
// Get curent page
int currentPage = self.slidingMenuGrid.iCurrentPage;
CCLOG(@"currentPage:%d", currentPage); // Always prints correct number
// Adjust pagination display
for (int i = 0; i < NUM_PAGES; i++) {
CCSprite *bub = [self.pageBubbles objectAtIndex:i];
if (i == currentPage) {
bub.displayFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"pageSelected.png"];
}
else {
bub.displayFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"page.png"];
}
}
// Adjust difficulty labeL
/******* BREAKS HERE *******/
self.difficultyLabel.string = (NSString *)[self.difficulties objectAtIndex:currentPage];
//self.difficultyLabel.string = @"Test"; // Breaks every once in a while as well
}