custom font for iOS and Android using cocos2d-x v3.0 C++

  • abhishek
    Likes 0

    Problem Description

    hi..
    i want to use custom font in one of my games..
    i am using cocos2d-x 3.0 C++ and i want the code to work on iOS as well as android
    so can you please help me out with this?
    do i have to implement the custom font code separately for iOS and android?

    thanks 

  • abhishek
    Likes 1

    hi..
    i got it to work..
    i dont know why code from the internet was not working..
    i simply created a new project and used the default code from HelloWorld.cpp

    auto myLabel = LabelTTF::create("label text", "fonts/Marker Felt.ttf", 32);
    myLabel->setPosition(100, 100);
    this->addChild(myLabel);


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Great to hear :D

  • abhishek
    Likes 0

    hii..
    i have got the code running, but now i have another problem.
    i am able to display text in MARKER FELT font that is by default a part of the project..
    but i have another custom font named VOGUEBOLD (VogueBold.ttf) and i have tried my best but am not able to get it to work

    i even tried using the PostScript name of the font which happens to be VogueBold
    but it still does not work

    can you help me out with this?
    thanks  

  • Sonar Systems admin
    Likes 0

    Could you show us all of the code you are using to use the font.

  • abhishek
    Likes 0

    THIS WORKS
    auto label3 = LabelTTF::create("3", "fonts/Marker Felt.ttf", (visibleSize.width/1920)*80);
    label3->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_3->addChild(label3);


    THIS DOES NOT WORK
    auto label4 = LabelTTF::create("4", "fonts/VogueBold.ttf", (visibleSize.width/1920)*80);
    label4->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_4->addChild(label4);


    THIS DOES NOT WORK
    auto label5 = LabelTTF::create("5", "fonts/Vogue Bold.ttf", (visibleSize.width/1920)*80);
    label5->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_5->addChild(label5);


    i found VogueBold font on the internet with different names:
    VogueBold.ttf and Vogue Bold.ttf, so i tried both of them, but only the Marker Felt option works
    i even tried writing the font name without the font/ part and also tried adding the fonts to the resources folder directly
    but nothing works 


  • Sonar Systems admin
    Likes 0

    Try some other fonts from the net, just random ones off dafont.com as it may just be a problem with the font you are trying to use,

     

     

  • abhishek
    Likes 0

    ok i will do that..
    thanks a lot for your suggestion

    can you please tell me 2 more things..

    1. LABELS
    every where i checked, people are creating a label with this code:
    auto label1 = LabelTTF::create("1", "fonts/Marker Felt.ttf", (visibleSize.width/1920)*80);
    label1->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_1->addChild(label1);


    the problem is i do not want to use auto label1 because i want to maintain a reference for this label
    because i want to change its value as and when required
    so what do i use here: Label *label1.. or LabelTTF *label1.. or what?


    2. MANY BUTTONS ONE FUNCTION
    i know how to do this in iOS objective-C, but have not been able to implement this in cocos2d-x yet
    i have 9 buttons:
    bn_1
    bn_2
    ..
    bn_8
    bn_9

    i want all these buttons to be linked to 1 single function say: bn_X_tapped
    and in this function, i want to use a switch case to check the tag of the button that was tapped and then perform particular action..
    how can i do this?

    thanks
     


  • Sonar Systems admin
    Likes 0

    auto label1 = LabelTTF::create("1", "fonts/Marker Felt.ttf", (visibleSize.width/1920)*80);
    is the same as

    LabelTTF *label1 = LabelTTF::create("1", "fonts/Marker Felt.ttf", (visibleSize.width/1920)*80);

     

    and yes you would using LabelTTF *

     

     

     

     

    Check this out http://cocos.sonarlearning.co.uk/v1.0/docs/ui-elements-such-as-uibutton


     

  • abhishek
    Likes 0

    hi..
    i tried that code already..
    it shows that LabelTTF is deprecated
    thats why i thought that there must be some other solution
    so this is the only way? 

  • abhishek
    Likes 0

    // this is called when the user interacts with the button
    void ClassName::touchEvent( Ref *sender, Widget::TouchEventType type )
    {
        switch (type)
        {
            case Widget::TouchEventType::BEGAN:
                // code to handle when the button is first clicked
                break;
                
            case Widget::TouchEventType::MOVED:
                // code to handle when the user is moving their finger/cursor whilst clicking the button
                break;
                
            case Widget::TouchEventType::ENDED:
                // code to handle when the button click has ended (e.g. finger is lifted off the screen)
                break;
                
            case Widget::TouchEventType::CANCELED:
                // code to handle when the button click has been cancelled,  this is usually handled the same way as ENDED in most applications (e.g. another application takes control of the device)
                break;
                
            default:
                break;
        }
    }
    
    this is the code mentioned in the sonar-learning web page link you shared
    but i need to check for the tags because i want to know which button has been tapped
    
    so if my .h file, the fn is:
    void bn_x_tapped(Ref *sender);
    
    and i have set fn calls and button tags like this in .cpp file:
    bn_1 = MenuItemImage::create("lock/bn_digit_0.png", "lock/bn_digit_1.png", CC_CALLBACK_0(MainMenuScene::bn_x_tapped, this));
    bn_2 = MenuItemImage::create("lock/bn_digit_0.png", "lock/bn_digit_1.png", CC_CALLBACK_0(MainMenuScene::bn_x_tapped, this));
    and so on..
    
    bn_1->setTag(1);
    bn_2->setTag(2);
    and so on..
    
    what should i do in my .cpp file:
    

    void MainMenuScene::bn_x_tapped(Ref *sender){
        int temp = ??; (i want the tag value of the button that was tapped.. how do i do this?)
        switch(temp){

        }
    }


  • Sonar Systems admin
    Likes 0

    void ClassName::touchEvent( Ref *sender, Widget::TouchEventType type )
    {
        Node *node = (Node *)sender;
    
        switch (type)
        {
            case Widget::TouchEventType::BEGAN:
                // code to handle when the button is first clicked
                break;
                
            case Widget::TouchEventType::MOVED:
                // code to handle when the user is moving their finger/cursor whilst clicking the button
                break;
                
            case Widget::TouchEventType::ENDED:
                // code to handle when the button click has ended (e.g. finger is lifted off the screen)
                switch ( node->getTag( ) )
                {
                    case 1:
                        
                        break;
    
                    case 2:
                        
                        break;
                }
    
                break;
                
            case Widget::TouchEventType::CANCELED:
                // code to handle when the button click has been cancelled,  this is usually handled the same way as ENDED in most applications (e.g. another application takes control of the device)
                break;
                
            default:
                break;
        }
    }

     


  • abhishek
    Likes 0

    thanks a lot..
    this worked perfect !!
    i suggest you use the above code in your web page also..
    this way people who have the same doubt as me will get their answers directly
    :)


  • abhishek
    Likes 0

    hii..
    about this particular issue:

    THIS WORKS
    auto label3 = LabelTTF::create("3", "fonts/Marker Felt.ttf", (visibleSize.width/1920)*80);
    label3->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_3->addChild(label3);


    THIS DOES NOT WORK
    auto label4 = LabelTTF::create("4", "fonts/VogueBold.ttf", (visibleSize.width/1920)*80);
    label4->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_4->addChild(label4);


    THIS DOES NOT WORK
    auto label5 = LabelTTF::create("5", "fonts/Vogue Bold.ttf", (visibleSize.width/1920)*80);
    label5->setPosition(bn_1->getContentSize().width/2, bn_1->getContentSize().height/2);
    bn_5->addChild(label5);


    i found VogueBold font on the internet with different names:
    VogueBold.ttf and Vogue Bold.ttf, so i tried both of them, but only the Marker Felt option works
    i even tried writing the font name without the font/ part and also tried adding the fonts to the resources folder directly
    but nothing works 

    i tried using other fonts as well
    nothing else works, only Marker Felt is working properly
    :(

  • Sonar Systems admin
    Likes 0

    Try using a newer version of Cocos2d-x

  • abhishek
    Likes 0

    ok i will try to do that 2moro..
    i need help with array as well
    but will add that as a separate qn
    thanks
    :)

  • Sonar Systems admin
    Likes 0

    ok      

  • abhishek
    Likes 0

    hi..
    i updated to the latest version of cocos2d-x (3.5) and now i am able to use custom font.
    thank you for your suggestion.. it helped a lot.
    :)

  • Sonar Systems admin
    Likes 0

    Great to hear 

Login to reply