简体   繁体   English

Windows上的cocos2d-x 3.x:如何更改游戏窗口的大小?

[英]cocos2d-x 3.x on Windows: How do I change the size of my game window?

When developing using cocos2d-x 3.x for a device, it automatically sets the GL view to fit the device. 使用cocos2d-x 3.x为设备开发时,它会自动设置GL视图以适合设备。 In VS2012 on windows, it creates a seemingly-arbitrarily sized window. 在VS2012的Windows上,它创建了一个看似任意大小的窗口。 How do I set the size of that window? 如何设置该窗口的大小?

My solution was as follows. 我的解决方案如下。

In AppDelegate.cpp: 在AppDelegate.cpp中:

bool AppDelegate::applicationDidFinishLaunching() {
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) { 
        glview = GLView::create("My Game");
        glview->setFrameSize(800, 600);     // or whatever
        director->setOpenGLView(glview);
    }
    ...
}

In my particular use case, I'm setting the window sizes to various resolutions and aspect ratios to test my layouts. 在我的特定用例中,我将窗口大小设置为各种分辨率和宽高比以测试我的布局。 I'm sharing Q&A format because I couldn't find a straight answer to this anywhere. 我正在分享Q&A格式,因为我无法在任何地方找到答案。

+1 to Tom. 给汤姆+1。 Also, I would just like to share my answer and experience to everyone. 另外,我想向大家分享我的答案和经验。

Remember, do not set the screen size "AFTER" the director set the GL view. 请记住,不要将导演设置GL视图的屏幕尺寸设置为“AFTER”。 This causes some problem with bounds detection on some nodes most notably cocos2d::ui::Button and other ways on how create buttons. 这会导致某些节点上的边界检测出现一些问题,尤其是cocos2d :: ui :: Button以及其他创建按钮的方法。 I am using cocos2d-x v3.6 我使用的是cocos2d-x v3.6

Check my answer here: cocos2d::Menu click detection is a bit off to bottom left whenever using a custom window size 在这里查看我的答案: cocos2d ::使用自定义窗口大小时,菜单点击检测有点偏向左下角

Doing this way will cause some problem: 这样做会导致一些问题:

auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
    glview = GLViewImpl::create("My Game");
    director->setOpenGLView(glview);
}

director->getOpenGLView()->setFrameSize(width , height);

Clicking the button, doesn't kick in the callback. 单击该按钮不会启动回调。 Which is frustrating. 这令人沮丧。 Probably out of your frustration as well you click all through out the screen and notice there are some part of the screen (most likely bottom left part) which triggers the callback as if the bounding box of the node lies there. 也许你的沮丧也可以点击整个屏幕,并注意屏幕的某些部分(很可能是左下角部分)触发回调,好像节点的边界框位于那里一样。 It doesn't even match the position and the size of the node in question. 它甚至不匹配所讨论节点的位置和大小。 I can reproduce the problem whenever I change the screen size like the one I describe above. 每当我像上面描述的那样改变屏幕尺寸时,我都可以重现这个问题。 So never ever do it like that. 所以永远不要那样做。

Tom's answer is the correct way of changing screen size. 汤姆的答案是改变屏幕尺寸的正确方法。

I hope this will deter anyone doing it this way. 我希望这会阻止任何人这样做。 I swear to God, this post could have save me heck of time solving this. 我向上帝发誓,这篇文章本可以省时间解决这个问题。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM