MenuItemFont字体设置bug的解决方法

1、问题

考虑如下代码:

	auto restart = MenuItemFont::create("Restart",
		CC_CALLBACK_0(GameScene::Restart, this));
	restart->setFontSize(40);

MenuItemFont字体大小并不会因为设置了而有所改变,反而会在第二次显示MenuItemFont字体的时候字体大小会有所体现。

2、解决方法

将上面代码修改为:

	MenuItemFont::setFontSize(40);
	auto restart = MenuItemFont::create("Restart",
		CC_CALLBACK_0(GameScene::Restart, this));

这时候MenuItemFont字体正常显示。

3、分析

MenuItemFont字体大小由全局变量_globalFontSize决定,但是显示效果由成员变量_fontSize决定。setFontSize方法只是修改了全局变量_globalFontSize,
并没有更改成员变量_fontSize,因此先将全局变量修改后再使用,就能使字体设置及时生效。

猜你喜欢

转载自blog.csdn.net/pigautumn/article/details/77977880