Cocos2D HD spriteWithFile以SD显示

时间:2015-01-02 21:31:02

标签: cocos2d-iphone sprite retina

我正在使用Cocos2D 2.1,这个应用程序计划只用于iPad视网膜和非视网膜。当我使用spriteWithFile并指定不带-hd后缀的文件名时,Cocos2D会找到带有-hd后缀的图像并使用它,但它会以标准清晰度显示,这意味着它不会缩小,因此它会显示为视网膜图像。

在我的委托 - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中,我有以下内容:

CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
                                   pixelFormat:kEAGLColorFormatRGBA8
                                   depthFormat:GL_DEPTH24_STENCIL8_OES
                            preserveBackbuffer:NO
                                    sharegroup:nil
                                 multiSampling:NO
                               numberOfSamples:0];

[glView setMultipleTouchEnabled:YES];

director_ = (CCDirectorIOS*) [CCDirector sharedDirector];

[director_ setDisplayStats:YES];

[director_ setAnimationInterval:1.0/60];


// attach the openglView to the director
[director_ setView:glView];

// 2D projection
[director_ setProjection:kCCDirectorProjection2D];

[director_ enableRetinaDisplay:YES];

//  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director_ enableRetinaDisplay:NO] )
    CCLOG(@"Retina Display Not supported");

// If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix.
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:NO]; // Default: NO. No fallback suffixes are going to be used

[sharedFileUtils setiPadRetinaDisplaySuffix:@"-hd"];

glView.opaque = NO;
glClearColor(0.0f,0.0f,0.0f,0.0f);
//glClear(GL_COLOR_BUFFER_BIT);

// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

当我使用spriteWithFile添加精灵(而不是使用精灵表,因为它只有一个图像)时,我有以下代码:

movingTileGroup.handleSprite1 = [CCSprite spriteWithFile:@"draggableTileGroupHandle.png"];

movingTileGroup.handleSprite1.position = ccp(x,y);

[self.theMap addChild:movingTileGroup.handleSprite1 z:13];

更新:我看到调用了 - (BOOL)enableRetinaDisplay:(BOOL),if语句都没有返回true。它可能与比例因子设置有关吗?

-(BOOL) enableRetinaDisplay:(BOOL)enabled
{
    NSLog(@"enableRetinaDisplay called");
    // Already enabled ?
    if( enabled && __ccContentScaleFactor == 2 ){
        NSLog(@"enabled && __ccContentScaleFactor == 2 ");
        return YES;
    }

    // Already disabled
    if( ! enabled && __ccContentScaleFactor == 1 ){
        NSLog(@"! enabled && __ccContentScaleFactor == 1 ");
        return YES;
    }

    // setContentScaleFactor is not supported
    if (! [__view respondsToSelector:@selector(setContentScaleFactor:)]){
        NSLog(@"setContentScaleFactor is not supported");
        return NO;
    }
    // SD device
    if ([[UIScreen mainScreen] scale] == 1.0){
        NSLog(@"SD device");
        return NO;
    }

    float newScale = enabled ? 2 : 1;
    [self setContentScaleFactor:newScale];

    // Load Hi-Res FPS label
    [[CCFileUtils sharedFileUtils] buildSearchResolutionsOrder];
    [self createStatsLabel];

    return YES;
}

1 个答案:

答案 0 :(得分:0)

我假设您正在使用Spritebuilder生成精灵,如果是这样您需要在spritebuilder软件中指定图像的默认大小,可以通过单击显示问题的图像然后再进行左侧边栏或面板选择图像尺寸为"默认"它会自动缩放到所需的大小......

如果这不能解决您的问题,请告诉我......问候......