#include "cocos2d.h" using namespace cocos2d; // ... auto view = Director::getInstance()->getOpenGLView(); Size frameSize = view->getFrameSize(); // returns a Size object representing the current frame size int width = frameSize.width; // the width of the game view in pixels int height = frameSize.height; // the height of the game view in pixels
#include "cocos2d.h" using namespace cocos2d; // ... auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); auto view = Director::getInstance()->getOpenGLView(); Size frameSize = view->getFrameSize(); // returns a Size object representing the current frame size // create a sprite that fills the screen auto sprite = Sprite::create("background.png"); float scaleX = frameSize.width / sprite->getContentSize().width; float scaleY = frameSize.height / sprite->getContentSize().height; sprite->setScaleX(scaleX); sprite->setScaleY(scaleY); sprite->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2)); addChild(sprite);This code gets the visible size and origin of the game view using the Director class, as well as the current frame size using CCEGLView's getFrameSize() method. It then creates a sprite that scales itself to fill the width and height of the game view, using the ratio of the frame size to the sprite's original size. Finally, it adds the sprite to the scene's node hierarchy. Package/library: Cocos2d-x