1

不知道为什么菜单与左上角对齐,以前的版本菜单与中心完美对齐。在 iPhone 4 中进行测试。

我知道解决方案[menu setPosition:ccp( size.width/2, size.height/2)];,但想了解为什么它与左上对齐。

要获得相同的结果,请在 AppDelegate 中尝试此操作

[director_ pushScene: [HelloWorldLayer scene]]; 

代替

[director_ pushScene: [IntroLayer scene]];

我的结果和我的代码

在此处输入图像描述

更新源

菜单层.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface MenuLayer : CCLayer {

}

+(CCScene *) scene;

@end

菜单层.m

#import "MenuLayer.h"


@implementation MenuLayer

        +(CCScene *) scene
        {
            CCScene *scene = [CCScene node];

            MenuLayer *layer = [MenuLayer node];

            [scene addChild: layer];

            return scene;
        }

    -(id) init
    {
        if( (self=[super init]) ) {
            CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Play" block:^(id sender) {}];
            CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Settings" block:^(id sender) {}];

            CCMenu *menu = [CCMenu menuWithItems:itemAchievement, itemLeaderboard, nil];

            [menu alignItemsVertically];

            [self addChild:menu];
        }
        return self;
    }
    @end

AppDelegate.m

    [director_ pushScene: [MenuLayer scene]];
4

3 回答 3

1

好的,我先告诉你答案:

你出了点问题:) ..你一定改变了父母的位置,或者它的祖父母,......等等。

编辑:

您的错误CCDirector在此方法中运行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ...
    // Remove this code ! 
    [director_ runWithScene:blah];
} 

相反,您必须CCDirector像这样运行:

// This method is part of the CCDirectorDelegate methods
// Add it below the - (BOOL)application: didFinishLaunchingWithOptions:
-(void) directorDidReshapeProjection:(CCDirector*)director
{
    if(director.runningScene == nil) {
        // Add the first scene to the stack. The director will draw it immediately into the framebuffer. (Animation is started automatically when the view is displayed.)
        // and add the scene to the stack. The director will run it when it automatically when the view is displayed.
        [director_ runWithScene: [HelloWorldLayer scene]];
    }
}

证据:

阅读 cocos2d 文档!他们说如果你在 iOS 6 之前的 iOS 版本上运行,你不能运行CCDirectorinsideapplication: didFinishLaunching:方法。

从文档:

这对于 iOS4 和 iOS5 是必需的,以确保第一个场景具有正确的尺寸 这在 iOS6 上不需要,可以添加到应用程序中:didFinish...

这是你的照片,解剖:

在此处输入图像描述

真是一团糟…… cocos2d 的默认行为不可能是那样的。创建一个新的 cocos2d v2.x,然后清理HelloWorldLayer.m,结果如下:

// Achievement Menu Item using blocks
CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Achievements" block:^(id sender) {}];
// Leaderboard Menu Item using blocks
CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Leaderboard" block:^(id sender) {}];

CCMenu *menu = [CCMenu menuWithItems:itemAchievement, itemLeaderboard, nil];

[menu alignItemsVertically];

[self addChild:menu];

在此处输入图像描述

于 2012-09-15T19:38:16.700 回答
1

我已通过-(void) makeTransition:(ccTime)dtIntroLayer.m中查找并添加替换以下代码来解决此问题:

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene] withColor:ccWHITE]];

经过

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[YourLayer scene] withColor:ccWHITE]];
于 2013-07-03T07:39:38.993 回答
0

它通过修改 MenuLayer.m 来工作 - 覆盖 onEnter 然后将孩子添加到该层

#import "MenuLayer.h"


@implementation MenuLayer

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];

    MenuLayer *layer = [MenuLayer node];

    [scene addChild: layer];

    return scene;
}
-(void) onEnter
{
    [super onEnter];
    CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"Play" block:^(id sender) {}];
    CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"Settings" block:^(id sender) {}];

    CCMenu *menu = [CCMenu menuWithItems:itemAchievement, itemLeaderboard, nil];

    [menu alignItemsVertically];

    [self addChild:menu];

}
-(id) init
{
    if( (self=[super init]) ) {

    }
    return self;
}
@end
于 2012-09-16T05:01:33.707 回答