After weeks of debugging, I struggle to figure out what was going on with cocos2d::Menu. I was surprise that my MenuItemImage was not receiving callbacks when I click on the
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!