Cocos2d-x 中setFrameSize 和 setDesignResolutionSize的一个问题

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 中setFrameSize 和 setDesignResolutionSize的一个问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近开始搞cocos了,虽然开发起来看起来还挺简单的……但是感觉坑真是好多。。。

ok,现在讲一下我遇到的一个问题。在cocos2dx-3.x的版本中,增加了DesignResolutionSize的概念,这个东西可以使得自己设计的屏幕大小适应各种机器的屏幕。听起来还是很方便的。我开始想要模拟一个960*640的屏幕,但逻辑上的分辨率是480*320,于是我这么写了:


    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::create("Cpp Empty Test");
        director->setOpenGLView(glview);
    }
	// 设定分辨率
	glview->setDesignResolutionSize(480,320,ResolutionPolicy::EXACT_FIT);
	glview->setFrameSize(960,640);

结果出现了奇怪的问题,在我写touch事件的时候,坐标怎么搞都不对。搞了好长时间才弄好,晕了。实际上,问题就出在我刚才设定分辨率的部分,把代码改成这样就好了:


glview->setFrameSize(960,640);
glview->setDesignResolutionSize(480,ResolutionPolicy::EXACT_FIT);

为什么会出现这样的问题呢。。。我们来看一下源码,首先是setDesignResolutionSize: @H_502_16@void GLView::setDesignResolutionSize(float width,float height,ResolutionPolicy resolutionPolicy) { CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN,"should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } _designResolutionSize.setSize(width,height); _resolutionPolicy = resolutionPolicy; updateDesignResolutionSize(); }
看起来就是设定了一下我们传入的东西,然后update一下,点开updateDesignResolutionSize()看一下:
void GLView::updateDesignResolutionSize()
{
    if (_screenSize.width > 0 && _screenSize.height > 0
        && _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
    {
        _scaleX = (float)_screenSize.width / _designResolutionSize.width;
        _scaleY = (float)_screenSize.height / _designResolutionSize.height;
        
        if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
        {
            _scaleX = _scaleY = MAX(_scaleX,_scaleY);
        }
        
        else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
        {
            _scaleX = _scaleY = MIN(_scaleX,_scaleY);
        }
        
        else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
            _scaleX = _scaleY;
            _designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
        }
        
        else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
            _scaleY = _scaleX;
            _designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
        }
        
        // calculate the rect of viewport
        float viewPortW = _designResolutionSize.width * _scaleX;
        float viewPortH = _designResolutionSize.height * _scaleY;
        
        _viewPortRect.setRect((_screenSize.width - viewPortW) / 2,(_screenSize.height - viewPortH) / 2,viewPortW,viewPortH);
        
        // reset director's member variables to fit visible rect
        auto director = Director::getInstance();
        director->_winSizeInPoints = getDesignResolutionSize();
        director->createStatsLabel();
        director->setGLDefaultValues();
    }
}

看起来是根据设计分辨率和屏幕大小,然后根据所选策略,计算了缩放的因子,然后修改分辨率,那么这个问题大概也就能明白了,我们在设定屏幕大小之前就使用了它,自然会出现这个问题,因为之前的屏幕大小不是这样的,最后来看一下setFrameSize这个函数
void GLView::setFrameSize(float width,float height)
{
    _designResolutionSize = _screenSize = Size(width,height);
}
这个函数把屏幕分辨率和设计分辨率都进行了设定,因此像最开始那么写,设计分辨率也变得不对了,好坑。。。人生艰难啊

猜你在找的Cocos2d-x相关文章