2013-04-13 75 views
0

在我的遊戲管理單獨的對象創建:爲什麼我的變量沒有在Cocos2d中設置?

// Load levels 
     // Create array property to store level objects 
     self.levels = [[[NSMutableArray alloc] init] autorelease]; 
     //Create 2 level objects with spawnRates 
     Level *level1 = [[[Level alloc] initWithLevelNum:1 spawnRate:10000 bgImageName:@"Icon.png"] autorelease]; 
     Level *level2 = [[[Level alloc] initWithLevelNum:2 spawnRate:10000 bgImageName:@"Icon.png"] autorelease]; 
     Level *level3 = [[[Level alloc] initWithLevelNum:3 spawnRate:80 bgImageName:@"Icon.png"] autorelease]; 
     Level *level4 = [[[Level alloc] initWithLevelNum:4 spawnRate:80 bgImageName:@"Icon.png"] autorelease]; 
     Level *level5 = [[[Level alloc] initWithLevelNum:5 spawnRate:2 bgImageName:@"Icon.png"] autorelease]; 
     Level *level6 = [[[Level alloc] initWithLevelNum:6 spawnRate:1 bgImageName:@"Icon.png"] autorelease]; 
     //Put level objects into array property 
     [_levels addObject:level1]; 
     [_levels addObject:level2]; 
     [_levels addObject:level3]; 
     [_levels addObject:level4]; 
     [_levels addObject:level5]; 
     [_levels addObject:level6]; 
     //Set current level index to 0 
     self.curLevelIndex = 0; 

並進一步向下行,我實現方法由層被稱爲像這樣:

- (Level *)curLevel { 
    NSLog(@"curLevelIndex is %d", _curLevelIndex); 
    //returns object at current level index where at start, first slot [0] = spawnrate = 1 
    return [_levels objectAtIndex:_curLevelIndex]; 
} 

- (void)restartGame { 
    _curLevelIndex = 0; 
    [self nextLevel]; 
} 

- (void)levelComplete { 
    NSLog(@"curLevel spawnRate is %d", [self curLevel].spawnRate); 

    //Increase currentlevelindex 
    _curLevelIndex++; 

    if (_curLevelIndex >= [_levels count]) { 
     _curLevelIndex = 0; 
     NSLog(@"resetting levels"); 
    } else { 
     //make it faster 
     //NSLog(@"current level %d", [[_levels objectAtIndex:_curLevelIndex].spawnRate ]; 
     NSLog(@"new level %d", _curLevelIndex); 
    } 

} 

請原諒所有NSLogs但我要去瘋狂試圖找出爲什麼我的spawnRate沒有被改變。在我的遊戲的更新方法中,我這樣做:

if (timeSinceLastRateBump > 3) { 
     NSLog(@"DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 
     [[GameManager sharedGameManager] levelComplete]; 
     timeSinceLastRateBump = 0; 
    } 

這就是每秒或多或少地調用。現在即時得到這個在我的NSLog:

2013-04-13 11:08:22:489 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:23:489 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:24:472 MZ[1408:3079] DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
2013-04-13 11:08:24:473 MZ[1408:3079] curLevelIndex is 1 
2013-04-13 11:08:24:474 MZ[1408:3079] curLevel spawnRate is 0 
2013-04-13 11:08:24:475 MZ[1408:3079] new level 2 
2013-04-13 11:08:24:509 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:25:506 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:26:522 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:27:490 MZ[1408:3079] DIFFICULTY INCREASES NOW>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
2013-04-13 11:08:27:491 MZ[1408:3079] curLevelIndex is 2 
2013-04-13 11:08:27:493 MZ[1408:3079] curLevel spawnRate is 0 
2013-04-13 11:08:27:494 MZ[1408:3079] new level 3 
2013-04-13 11:08:27:523 MZ[1408:3079] target added to _targets 
2013-04-13 11:08:27:856 MZ[1408:3079] Globals called. Points: 1 
2013-04-13 11:08:27:856 MZ[1408:3079] Globals leaving. Points: 2 
2013-04-13 11:08:28:539 MZ[1408:3079] target added to _targets 

這證明curLevelIndex從0到1增加至2但spawnRate保持爲0,爲什麼?

,而不是在你的日常levelComplete
+1

看不到任何改變spawnRate值的代碼 – Morion

+0

爲spawnRate顯示你的Level類(setters,getters) – YvesLeBorg

+0

爲每個Level實例創建不同的spawnRate。我創建了6個Level實例,並將它們放入self.levels數組中。當調用levelComplete方法時,curLevelIndex獲取++,因此返回[self.levels objectAtIndex:_curLevelIndex];方法 - (Level *)curLevel;應該在新的curLevelIndex處返回Level。 – marciokoko

回答

0

,:

NSLog(@"new level %d", _curLevelIndex); 

做的事:在日誌

Level* current = [self curLevel]; 
NSLog(@"current level no : %d , spw rate : %f",current.levelNumber,current.spawnRate]; 
// *** WAG for the getters. 

和斷點。如果電流爲零,請指出。如果沒有,請檢查var並查看該對象中的產卵率。

+0

我仍然得到0 spawnRate。目前是不是零,我得到我的水平與我分配spawnRate和一切:( – marciokoko

+0

杜赫...使用%f記錄你的產卵率,你記錄什麼不是你有什麼。 – YvesLeBorg

0

中的代碼:

NSLog(@"curLevel spawnRate is %d", [self curLevel].spawnRate); 

%d被用於雙精度值,而不是一個浮子。將其更改爲%f。

相關問題