简体   繁体   English

Cocos2d上的自定义字体异常

[英]Exception with custom font on Cocos2d

I'm trying to make menu with custom font, but it doesn't seem to work. 我正在尝试使用自定义字体制作菜单,但它似乎不起作用。 Here is the code: 这是代码:

- (id) init
{
    if (self = [super init])
    {
        CGSize size = [[CCDirector sharedDirector] winSize];
        [CCMenuItemFont setFontName:@"PC Senior Regular"];
        [CCMenuItemFont setFontSize:18];
        CCMenuItemFont *menu1 = [CCMenuItemFont itemFromString:@"Music ON" target:self selector:@selector(musicToggle)];
        CCMenuItemFont *menu2 = [CCMenuItemFont itemFromString:@"Back" target:self selector:@selector(back)];
        CCMenu *menu = [CCMenu menuWithItems:menu1, menu2, nil];
        [menu setPosition:ccp(size.width / 2 , size.height / 2)];
        [menu alignItemsVertically];
        [self addChild:menu];
    }

Here is the code in my info.plist: 这里是我的info.plist的代码:

<key>UIAppFonts</key>
    <array>
        <string>PC Senior Regular.ttf</string>
        <string>senior.ttf</string>
    </array>

Exception: 例外:

2012-07-27 05:42:35.369 Busterball[16089:10a03] In options
2012-07-27 05:42:35.371 Busterball[16089:10a03] -[__NSCFConstantString sizeWithZFont:]: unrecognized selector sent to instance 0xb0670
2012-07-27 05:42:35.372 Busterball[16089:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString sizeWithZFont:]: unrecognized selector sent to instance 0xb0670'

I have tried using senior.ttf, just senior, etc. Both fonts are added as targets for the project. 我试过使用senior.ttf,只是senior等。两种字体都添加为该项目的目标。 I double checked for correct type case. 我仔细检查了类型是否正确。 What is going wrong? 怎么了?

Have you tried something like this? 你尝试过这样的事情吗?

CCLabelTTF *label = [CCLabelTTF labelWithString:"Some Text" fontName:@"MyFont.TTF" fontSize:24.0];
CCMenuItem *item = [CCMenuItemLabel itemWithLabel:label target:self selector:@selector(myFunction)];
CCMenu *menu = [CCMenu menuWithItems:item];
[self addChild:menu];

Your info.plist looks right, and this code has worked for me. 您的info.plist看起来正确,并且此代码对我有用。 That said, I do recommend setting breakpoints and trying to find where your error is coming from. 也就是说,我确实建议设置断点并尝试查找错误的出处。

An invalid argument exception means that you are trying to use a method that is not recognized. 无效的参数异常意味着您正在尝试使用无法识别的方法。 The problem is your sizeWithZFont method. 问题是您的sizeWithZFont方法。 I have tried to search in the cocos2d documentation and that is not a method in there. 我试图在cocos2d文档中进行搜索,但这不是那里的一种方法。

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

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