Uiview Cocos2d-应用加载时初始应用层显示不正确,重新加载后显示正确

Uiview Cocos2d-应用加载时初始应用层显示不正确,重新加载后显示正确,uiview,cocos2d-iphone,cclayer,Uiview,Cocos2d Iphone,Cclayer,我正在使用GameManager单例处理游戏所需的一些共享任务。其中一项任务是加载单个游戏场景。当游戏开始时,我显示的场景/层组合出现错误;看起来项目相对于的坐标可能是错误的 我选择哪一层并不重要,它们都不正确。如果单击某个按钮并加载另一个场景/层,然后返回相关场景/层,则所有内容都会正确显示。这只发生在视网膜显示设备上,所以我认为这可能与我在游戏管理器中为不同显示类型设置的缩放有关。但是,更改视网膜显示的缩放比例会使情况更加糟糕(图层太小) 这是我的AppDelegate.h #import

我正在使用GameManager单例处理游戏所需的一些共享任务。其中一项任务是加载单个游戏场景。当游戏开始时,我显示的场景/层组合出现错误;看起来项目相对于的坐标可能是错误的

我选择哪一层并不重要,它们都不正确。如果单击某个按钮并加载另一个场景/层,然后返回相关场景/层,则所有内容都会正确显示。这只发生在视网膜显示设备上,所以我认为这可能与我在游戏管理器中为不同显示类型设置的缩放有关。但是,更改视网膜显示的缩放比例会使情况更加糟糕(图层太小)

这是我的AppDelegate.h

#import <UIKit/UIKit.h>

@class RootViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow            *window;
    RootViewController  *viewController;
}

@property (nonatomic, retain) UIWindow *window;

@end
#导入
@类RootViewController;
@接口AppDelegate:NSObject{
UIWindow*窗口;
RootViewController*viewController;
}
@属性(非原子,保留)UIWindow*window;
@结束
AppDelegat.m

#import "cocos2d.h"

#import "AppDelegate.h"
#import "GameConfig.h"
#import "RootViewController.h"
#import "GameplayScene.h"
#import "GameManager.h"

@implementation AppDelegate

@synthesize window;

- (void) removeStartupFlicker
{
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
#endif
}


- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    // Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Try to use CADisplayLink director
    // if it fails (SDK < 3.1) use the default director
    if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
        [CCDirector setDirectorType:kCCDirectorTypeDefault];


    CCDirector *director = [CCDirector sharedDirector];

    // Init the View Controller
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;

    EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
                                   pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];

    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];


    // make the OpenGLView a child of the view controller
    [viewController setView:glView];

    // make the View Controller a child of the main window
    [window addSubview: viewController.view];

    [window makeKeyAndVisible];

    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];


    // Removes the startup flicker
    [self removeStartupFlicker];

    // Run the intro Scene
    //[[CCDirector sharedDirector] runWithScene:[GameplayScene node]];
    [[GameManager sharedGameManager] runSceneWithID:kMainMenuScene];
}

- (void)dealloc {
    [[CCDirector sharedDirector] end];
    [window release];
    [super dealloc];
}
#导入“cocos2d.h”
#导入“AppDelegate.h”
#导入“GameConfig.h”
#导入“RootViewController.h”
#导入“gameplaysecene.h”
#导入“GameManager.h”
@实现AppDelegate
@合成窗口;
-(无效)removeStartupFlicker
{
#如果游戏自动旋转==kGameAutorotationUIViewController
#恩迪夫
}
-(无效)应用程序IDFinishLaunching:(UIApplication*)应用程序
{
//打开窗户
窗口=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]边界]];
//尝试使用CADisplayLink控制器
//如果失败(SDK<3.1),则使用默认控制器
if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink])
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector*董事=[CCDirector sharedDirector];
//初始化视图控制器
viewController=[[RootViewController alloc]initWithNibName:nil bundle:nil];
viewController.WantFullScreenLayout=是;
EAGLView*glView=[EAGLView viewWithFrame:[窗口边界]
像素格式:kEAGLColorFormatRGB565//kEAGLColorFormatRGBA8
深度格式:0//GL\U深度\U组件16\U OES
];
//将openglView附加到控制器
[director setOpenGLView:glView];
[控制器设置设备定向:KCCD设备定向肖像];
[director setAnimationInterval:1.0/60];
[导演setDisplayFPS:是];
//使OpenGLView成为视图控制器的子级
[视图控制器设置视图:glView];
//使视图控制器成为主窗口的子窗口
[窗口添加子视图:viewController.view];
[WindowMakeKeyandVisible];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
//移除启动闪烁
[自移除启动闪烁];
//运行介绍场景
//[[CCDirector sharedDirector]运行场景:[游戏场景节点]];
[[GameManager sharedGameManager]runSceneWithID:kMainMenuScene];
}
-(无效)解除锁定{
[[CCDirector sharedDirector]end];
[窗口释放];
[super dealoc];
}
游戏经理

#import <Foundation/Foundation.h>
#import "Constants.h"
#import "CommonProtocols.h"

@interface GameManager : NSObject {
    BOOL isMusicON;
    BOOL isSoundEffectsON;
    BOOL hasPlayerDied;
    BOOL newHighScore;
    BOOL newBestTime;
    BOOL isUiTextLeft;
    int currentScore;
    int highScore;
    int lengthPlayed;
    int bestTime;
    int randomPurple;
    int randomGreen;
    int timeBonus;
    int timeTillDeath;
    int uiBackgroundHeight;
    CharacterStates previousPurpleState;
    CharacterStates previousGreenState;
    SceneTypes currentScene;
}

@property (readwrite) BOOL isMusicON;
@property (readwrite) BOOL isSoundEffectsON;
@property (readwrite) BOOL hasPlayerDied;
@property (readwrite) BOOL newHighScore;
@property (readwrite) BOOL newBestTime;
@property (readwrite) BOOL isUiTextLeft;
@property (readwrite) int currentScore;
@property (readwrite) int highScore;
@property (readwrite) int lengthPlayed;
@property (readwrite) int bestTime;
@property (readwrite) int randomPurple;
@property (readwrite) int randomGreen;
@property (readwrite) int uiBackgroundHeight;
@property (readwrite) CharacterStates previousPurpleState;
@property (readwrite) CharacterStates previousGreenState;
@property (readwrite) int timeBonus;
@property (readwrite) int timeTillDeath;

+(GameManager*)sharedGameManager;
-(void)runSceneWithID:(SceneTypes)sceneID;
-(void)openSiteWithLinkType:(LinkTypes)linkTypeToOpen ;

@end
#导入
#导入“Constants.h”
#导入“CommonProtocols.h”
@界面游戏管理器:NSObject{
布尔·伊斯穆西翁;
布尔·伊森;
布尔玩得很开心;
布尔-纽高分;
布尔纽贝斯特时间;
布尔isUiTextLeft;
int-currentScore;
int高分;
国际长度;
最佳时间;
紫色;
绿色;
积分奖金;
死亡时间;
国际背景高度;
特征状态先前紫红色;
特征状态先前的绿色状态;
SceneTypes当前场景;
}
@属性(读写)BOOL isMusicON;
@属性(读写)BOOL-issoundson;
@属性(读写)BOOL hasplayerded;
@属性(读写)BOOL newHighScore;
@属性(读写)BOOL newBestTime;
@属性(读写)BOOL isUiTextLeft;
@属性(读写)int currentScore;
@属性(读写)整数高分;
@属性(读写)int lengthPlayed;
@属性(读写)int最佳时间;
@属性(读写)int;
@属性(读写)为绿色;
@属性(读写)int uiBackgroundHeight;
@属性(读写)字符状态previousPurpleState;
@属性(读写)字符状态previousGreenState;
@属性(读写)int timeBonus;
@财产(读写)int timeTillDeath;
+(GameManager*)sharedGameManager;
-(void)runSceneWithID:(SceneTypes)sceneID;
-(void)openSiteWithLinkType:(LinkTypes)linkTypeToOpen;
@结束
游戏经理

#import "GameManager.h"
#import "GameplayScene.h"
#import "MainMenuScene.h"
#import "OptionsScene.h"
#import "CreditsScene.h"
#import "IntroScene.h"
#import "LevelCompleteScene.h"

@implementation GameManager

static GameManager* _sharedGameManager = nil; 
@synthesize isMusicON;
@synthesize isSoundEffectsON;
@synthesize hasPlayerDied;
@synthesize newHighScore;
@synthesize newBestTime;
@synthesize isUiTextLeft;
@synthesize currentScore;
@synthesize highScore;
@synthesize lengthPlayed;
@synthesize bestTime;
@synthesize previousPurpleState;
@synthesize previousGreenState;
@synthesize randomPurple;
@synthesize randomGreen;
@synthesize timeBonus;
@synthesize timeTillDeath;
@synthesize uiBackgroundHeight;

+(GameManager*)sharedGameManager {
    @synchronized([GameManager class])
    {
        if(!_sharedGameManager)
            [[self alloc] init]; 
        return _sharedGameManager;
    }
    return nil; 
}

+(id)alloc {
    @synchronized([GameManager class]){
        NSAssert(_sharedGameManager == nil, @"Attempted to allocate a second instance of the Game Manager singleton");
        _sharedGameManager = [super alloc];
        return _sharedGameManager;
    }
    return nil;
}

-(id) init {
    self = [super init];
    if (self != nil) {
        // Game manager initialized
        CCLOG(@"Game manager singleton, init");
        hasPlayerDied = NO;
        // DECODING INFO FROM DEFAULTS
        NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [dirPaths objectAtIndex:0];

        NSMutableData *gameData;
        NSKeyedUnarchiver *decoder;

        NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"gameState.dat"];
        gameData = [NSData dataWithContentsOfFile:documentPath];

        if (gameData) {
            decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

            highScore = [decoder decodeIntForKey:@"highScore"];
            bestTime = [decoder decodeIntForKey:@"bestTime"];
            isMusicON = [decoder decodeBoolForKey:@"isMusicON"];
            isSoundEffectsON = [decoder decodeBoolForKey:@"isSoundEffectsON"];
            isUiTextLeft = [decoder decodeBoolForKey:@"isUiTextLeft"];
            //currentScore = [decoder decodeIntForKey:@"currentScore"];

            [decoder release];
        } else {
            highScore = 0;
            bestTime = 0;
            isMusicON = TRUE;
            isSoundEffectsON = TRUE;
            isUiTextLeft = TRUE;
            //currentScore = 0;
        }

        CCLOG(@"Music - %s", isMusicON ? "true" : "false");
        CCLOG(@"Sound - %s", isSoundEffectsON ? "true" : "false");
        //****************************
        currentScore = 0;
        timeBonus = 0;
        timeTillDeath = 0;
        uiBackgroundHeight = 0;
        currentScene = kNoSceneUninitialized;
    }

    return self;
}

-(void)runSceneWithID:(SceneTypes)sceneID {

    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;

    switch (sceneID) {
        case kMainMenuScene:
            sceneToRun = [MainMenuScene node];
            break;
        case kOptionsScene:
            sceneToRun = [OptionsScene node];
            break;
        case kCreditsScene:
            sceneToRun = [CreditsScene node];
            break;
        case kIntroScene:
            sceneToRun = [IntroScene node];
            break;
        case kLevelCompleteScene:
            sceneToRun = [LevelCompleteScene node];
            break;
        case kGameplayScene:
            sceneToRun = [GameplayScene node];
            break;
        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return;
            break;
    }

    if (sceneToRun == nil) {
        // Revert back, since no new scene was found
        currentScene = oldScene;
        return;
    }

    // Menu Scenes have a value of < 100
    if (sceneID < 100) {

        if ([[CCDirector sharedDirector] enableRetinaDisplay:YES]) {
            // iPhone 4 Retina
            [sceneToRun setScaleX:1.0f];
            [sceneToRun setScaleY:1.0f];
            CCLOG(@"GM:Scaling for iPhone 4 (retina)");
        } else {
            [sceneToRun setScaleX:1.0f];
            [sceneToRun setScaleY:1.0f];
            CCLOG(@"GM:Scaling for iPhone 3G(non-retina)");
        }
    }

    if ([[CCDirector sharedDirector] runningScene] == nil) {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
    } else {
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }
}
#导入“GameManager.h”
#导入“gameplaysecene.h”
#导入“MainMenuScene.h”
#导入“optionscene.h”
#导入“CreditsScene.h”
#导入“IntroScene.h”
#导入“LevelCompleteScene.h”
@实现游戏管理器
静态GameManager*_sharedGameManager=nil;
@合成isMusicON;
@合成Issoundson;
@他玩得很开心;
@综合得分高;
@合成新潮时间;
@合成isUiTextLeft;
@综合当前评分;
@综合得分高;
@综合长度;
@综合最佳时间;
@合成紫杉酯;
@合成前绿酸盐;
@合成随机紫;
@合成无规绿色;
@综合计时奖金;
@死亡时间;
@综合背景高度;
+(GameManager*)sharedGameManager{
@已同步([GameManager类])
{
如果(!\u sharedGameManager)
[[self alloc]init];
返回_sharedGameManager;
}
返回零;
}
+(id)alloc{
@已同步([GameManager类]){
NSAssert(_sharedGameManager==nil,@“试图分配游戏管理器单例的第二个实例”);
_sharedGameManager=[super alloc];
返回_sharedGameManager;
}
返回零;
}
-(id)init{
self=[super init];
if(self!=nil){
//游戏管理器已初始化
[[CCDirector sharedDirector]runWithScene:[obj runSceneWithID:1]];
-(CCScene*)runSceneWithID:(SceneTypes)sceneID {

    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;

    switch (sceneID) {
        case kMainMenuScene:
            sceneToRun = [MainMenuScene node];
            break;
        case kOptionsScene:
            sceneToRun = [OptionsScene node];
            break;
        case kCreditsScene:
            sceneToRun = [CreditsScene node];
            break;
        case kIntroScene:
            sceneToRun = [IntroScene node];
            break;
        case kLevelCompleteScene:
            sceneToRun = [LevelCompleteScene node];
            break;
        case kGameplayScene:
            sceneToRun = [GameplayScene node];
            break;
        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return nil;
            break;
    }

    if (sceneToRun == nil) {
        // Revert back, since no new scene was found
        currentScene = oldScene;

    }
    return sceneToRun;
}