cocos2d::Menu click detection is a bit off to bottom left whenever using a custom window size

China☆狼群 提交于 2019-12-01 13:00:23

I fixed it!

I tried numerous ways on how to create a button. All of them exhibited similar behavior.

There must be something wrong with the changing of the screenSize. I committed a terrible mistake.

On AppDelegate::applicationDidFinishLaunching when it first initialize the glview, I was once told that in order to change the screen size I need to call this: cocos2d::Director::getInstance()->getOpenGLView()->setFrameSize(width , height). I set this after this particular line such that it will look like this:

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

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

This is so wrong. Doing this after the director set the OpenGL view registers the original 960 , 640 screen size that reverberates all through out the scenes, layers, nodes you have. Every time you check on the debugger the content size of the node in question (The button I have has a size of (140 , 40)), it defaults to the original screen size. This is not right. There is something wrong there. AND this will not be fixed if you set the content size manually yourself. I think by incorrectly setting the screensize some nodes' content size become misaligned or not properly computed by the time the director set the GL view. The proper way of changing screen is this:

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

Now, the click detection bounds, position and size, exactly fits onto the sprite image and can now receive callbacks! For *(#$*ing two weeks. I got it.

I hope this helps someone in the future!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!