これは、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
}