Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the background images fit to any screen

Tags:

cocos2d-x

I am developing a game using cocos2d-x. For setting the background image fit to my screen I used the following code, but it stretch 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;
}
like image 268
socrates Avatar asked Feb 14 '23 02:02

socrates


1 Answers

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

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

scaleXFactor and scaleYFactor is the size that you want. if you want it to be full screen you can set it to 1.

like image 66
Farooq Arshed Avatar answered May 16 '23 05:05

Farooq Arshed