Set background images to fit any screen.

I am developing a game using cocos2d-x. To set the background image that matches my screen, I used the following code, but stretching the image, please give advice.

bool AppDelegate::applicationDidFinishLaunching() {
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector ->setOpenGLView(CCEGLView::sharedOpenGLView());
    CCEGLView::sharedOpenGLView() ->setDesignResolutionSize(480, 320, kResolutionExactFit);
    pDirector ->setDisplayStats(false);
    pDirector ->setAnimationInterval(1.0 / 60);
    CCScene *pScene = SplashScene::scene();
    pDirector ->runWithScene(pScene);
    return true;
}
+4
source share
1 answer

What I usually do is that I scale the ratio of image size to screen. Below is the code for reference.

CCSprite * sprite = CCSprite::createWithSpriteFrameName(spriteName);
sprite->setScaleX((winSize.width / sprite->getContentSize().width) * scaleXFactor);
sprite->setScaleY((winSize.height / sprite->getContentSize().height) * scaleYFactor);

scaleXFactor and scaleYFactor are the size you want. if you want it to be fullscreen, you can set it to 1.

+8
source

Source: https://habr.com/ru/post/1538375/


All Articles