2013-03-21 56 views
0

在水平選擇屏幕上具有菜單項並取決於選定的項目級別開始...這裏是代碼..其中selectedLevel是menuItem的索引。問題與選擇水平表單水平選擇屏幕(Cocos2d + box2d)

if(selectedLevel==0) 
{ 
activeScene = [level2_1Scene node]; 
[[CCDirector sharedDirector] pushScene:activeScene]; 
    } 
else if(selectedLevel==1) 
{ 
activeScene = [level2_2Scene node]; 
[[CCDirector sharedDirector] pushScene:activeScene]; 
    } 
else if(selectedLevel==2) 
{  
activeScene = [level2_3Scene node]; 
[[CCDirector sharedDirector] pushScene:activeScene]; 

...等所有級別

如何與水平場景名稱像這樣簡單的格式做這工作不

activeScene = [[NSString stringWithFormat:@"level2_%d",selectedLevel+1] node]; 
[[CCDirector sharedDirector] pushScene:activeScene]; 

回答

0

這對我有用,,,

NSString *level=[NSString stringWithFormat:@"level2_%dScene",selectedLevel+1]; 
    Class sceneClass = NSClassFromString(level); 
    activeScene = (bunnyScene*)[sceneClass node] ; 
    [[CCDirector sharedDirector] replaceScene:activeScene]; 
2

這個片段從一個我的工廠大概可以在適應後做你的伎倆:

Skill *skkk = (Skill *) NSClassFromString(className); 

if (skkk) { 
    return (Skill *) [[skkk class] skillWithSpec:ss]; 
} else { 
    MPLOGERROR(@"*****************************************************************"); 
    MPLOGERROR(@"    no class with name %@", className); 
    MPLOGERROR(@"*****************************************************************"); 
    return nil; 
} 
0

我想這可能是你在找什麼:

switch(selectedLevel) 
{ 
    case 0: 
    activeScene = [level2_1Scene node]; 
    break; 

    case 1: 
    activeScene = [level2_2Scene node]; 
    break; 

    // Etc.... 
} 

if(activeScene) { 
    [[CCDirector sharedDirector] replaceScene:activeScene]; 
}