2010-11-16 74 views
0

我正在創建一個通用應用程序,我的CCMenu在iPhone,iPhone 4和iPad上都顯得很好。但是,在iPad上觸摸時按鈕不會執行任何操作。CCMenu不能在iPad上工作

除修改contentScaling屬性外,我沒有特定的iPad代碼,因此iPad使用與iPhone 4相同的圖像。這意味着相同的圖像可以在iPhone 4上使用,但不能在iPad上使用。

我使用的是cocos2d 0.99.rc0和iOS 4.1 SDK。我甚至不知道從哪裏開始排除故障。

我最近注意到的唯一怪事就是,iPad似乎畫了一次菜單場景,然後由於某種原因而快速重畫它,將所有像素或其他東西都移動。我的菜單類非常簡單,沒有「清爽」的代碼或任何應該移動的東西。這不會發生在低或高分辨率iPhone上。

這是我的代碼,馬虎,但非常簡單。

MainMenu.m:

CCMenuItemImage * playItem = [self makeMenuButtonWithSprite:@"Play.png" withSelector:@selector(play:)]; 

    CCMenuItemImage * resumeItem = [self makeMenuButtonWithSprite:@"Resume.png" withSelector:@selector(resume:)]; 

    CCMenuItemImage * optionsItem = [self makeMenuButtonWithSprite:@"Options.png" withSelector:@selector(options:)]; 

    CCMenuItemImage * helpItem = [self makeMenuButtonWithSprite:@"Help.png" withSelector:@selector(help:)]; 

    CCMenu *myMenu; 

    // Check if there is a valid savegame by comparing versions. 
    if ([[uD stringForKey:@"CFBundleVersion"] isEqualToString:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]) { 
     myMenu = [CCMenu menuWithItems:playItem, resumeItem, optionsItem, helpItem, nil]; 
    } else { 
     myMenu = [CCMenu menuWithItems:playItem, optionsItem, helpItem, nil]; 
    } 

    // Arrange the menu items vertically 
    [myMenu alignItemsVerticallyWithPadding:0.0f]; 
    myMenu.position = ccp(dB.wWidth/2,dB.wHeight/2); 

    // add the menu to your scene 
    [self addChild:myMenu z:100]; 

而且CCMenuItemImage工廠:

- (CCMenuItemImage *)makeMenuButtonWithSprite:(NSString *)spriteFileName withSelector:(SEL)selector { 
    CCSprite *spriteForButton = [CCSprite spriteWithFile:spriteFileName]; 
    spriteForButton.anchorPoint = ccp(0.5f,0.5f); 
    CCMenuItemImage * buttonImage =[CCMenuItemImage itemFromNormalImage:@"button.png" 
                 selectedImage: @"button.png" 
                  target:self 
                  selector:selector]; 
    [buttonImage addChild:spriteForButton z:100]; 
    spriteForButton.position = ccp([buttonImage boundingBox].size.width/2,([buttonImage boundingBox].size.height/2)-5); 
    return buttonImage; 
} 

回答

1

發現問題。這與我的自定義入侵有關,使iPad加載視網膜圖形。問題出在我的appDelegate中,我設置了使得ccDirector縮放和UIScreen縮放不匹配的contentScaleFactor。

問題歸結爲圖形很大,但cocos2d認爲座標是低分辨率。

+0

您是否使高分辨率圖像有效?如果是 - 請發佈答案如何去做 – Andrew 2011-03-05 21:29:48

3

我不認爲任何已知的bug存在此問題。不知道如何在沒有看到任何代碼的情況下進行調試,但是,如果有幫助,這裏是我的一些代碼,它使用cocos2d 0.99.5在iOS 4.0,4.1和4.2上成功添加了一個菜單(我升級後沒有區別):

-(void) initBottomMenu { 
CCMenuItem *aboutButton = [self gameButtonWithName:@"about" selector:@selector(onAbout:)]; 
CCMenuItem *settingsButton = [self gameButtonWithName:@"settings" selector:@selector(onSettings:)]; 
CCMenuItem *tutButton = [self gameButtonWithName:@"tutorial" selector:@selector(onTutorial:)]; 

CCMenu *menu = [CCMenu menuWithItems:aboutButton, settingsButton, tutButton, nil]; 
menu.position = ccp(xPos, yPos); 

[menu alignItemsHorizontallyWithPadding:45.0]; 

[self addChild:menu]; 
} 

的gameButtonWithName:選擇:方法是這樣的:

-(CCMenuItem *) gameButtonWithName:(NSString *)name selector:(SEL)s { 

NSString *iPadSuffix = @"IPad"; 

NSString *normal = [[NSString alloc] initWithFormat:@"%@Btn%@.png", name, iPadSuffix, nil] ; 
NSString *selected = [[NSString alloc] initWithFormat:@"%@Btn%@.png", name, iPadSuffix, nil]; 

CCMenuItem *retButton = [CCMenuItemImage itemFromNormalImage:normal 
               selectedImage:selected 
               disabledImage:selected 
                 target:self 
                selector:s]; 

[selected release]; 
[normal release]; 

return retButton; 
} 

有點草率,但它工作進行的順利添加菜單到我的主場景。

+0

感謝您的分享。我現在已將自己的代碼添加到原始文章中 - 外觀與您的文章幾乎完全相同,但無法使用。 – 2010-11-18 11:59:21

+0

請注意,無法使用[NSString stringWithFormat:「%@ Btn%@。png」,name,iPadSuffix,nil]創建您的字符串;然後跳過整個發佈的事情?我認爲用便利方法創建的所有對象(即不是[[obj alloc] init])都是自動釋放的,不是這種情況嗎? – 2010-11-18 12:00:53

+0

是的,這是絕對正確的,但最好的做法是儘可能多地照顧儘可能多的分配和釋放。不知道iPhone什麼時候會決定翻轉並釋放使用便捷方法創建的對象 - 這樣我就知道它何時發生。一旦iPhone得到適當的垃圾收集,這些都不會成爲問題! – 2010-11-19 14:37:34

相關問題