폰트를 이용한 메뉴 생성하기



c++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "test92.hpp"
 
USING_NS_CC;
 
Scene* Test92::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test92::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test92::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    //메뉴 아이템1
    //MenuItemFont::create(텍스트, 정의된 콜백함수(호출되는 메서드명, 타깃))
    //CC_CALLBACK_1 : 콜백함수 menuCallback의 매개변수가 1개이므로 CC_CALLBACK_1 사용
    auto item1 = MenuItemFont::create("Play", CC_CALLBACK_1(Test92::menuCallback, this));
    //메뉴 아이템2
    auto item2 = MenuItemFont::create("High Scores", CC_CALLBACK_1(Test92::menuCallback, this));
    //메뉴 아이템3
    auto item3 = MenuItemFont::create("About", CC_CALLBACK_1(Test92::menuCallback, this));
    
    
    auto menu = Menu::create(item1, item2, item3, NULL);
    menu -> alignItemsVertically();
    
    
    this->addChild(menu);
 
    return true;
}
 
 
//콜백함수
void Test92::menuCallback(Ref* sender){
    CCLOG("menu callback");
}
cs



결과



이미지를 이용한 메뉴 버튼 생성하기



c++)

Resources 폴더에 이미지파일 추가

메뉴아이템 생성 -> 메뉴에 추가 -> 화면레이어에 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "test91.hpp"
 
USING_NS_CC;
 
Scene* Test91::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test91::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test91::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    //메뉴1
    //MenuItemImage::create(일반 이미지, 클릭시 이미지, 클릭시 콜백함수)
    auto item1 = MenuItemImage::create("btn-play-normal.png""btn-play-selected.png", CC_CALLBACK_1(Test91::menuCallback, this));
    
    
    //메뉴2
    auto item2 = MenuItemImage::create("btn-highscores-normal.png""btn-highscores-selected.png", CC_CALLBACK_1(Test91::menuCallback, this));
    
    
    //메뉴3
    auto item3 = MenuItemImage::create("btn-about-normal.png""btn-about-selected.png", CC_CALLBACK_1(Test91::menuCallback, this));
    
    
    //메뉴아이템 추가
    auto menu = Menu::create(item1, item2, item3, NULL);
    menu -> alignItemsVertically();
    
    
    //화면레이어에 메뉴 추가
    this->addChild(menu);
    
    return true;
}
 
 
//콜백함수
void Test91::menuCallback(Ref* sender){
    CCLOG("menu callback");
}
cs



결과




CharMap 폰트 사용하기



Resources 폴더에 CharMap 폰트파일 추가 (.png 파일)



c++)

createWithCharMap() 함수 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "test84.hpp"
 
USING_NS_CC;
 
Scene* Test84::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test84::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test84::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    //createWithCharMap(CharMap폰트명, 글자너비, 글자높이, 이미지에서 아스크코드값)
    auto label = Label::createWithCharMap("labelatlas.png"1632'.');
    
    
    label->setString("012345"); //텍스트
    label->setAnchorPoint(Point(0.50.5)); //앵커포인트 (기본앵커포인트 : 0,0)
    label->setPosition(Point(150150));
 
    this->addChild(label);
    
    
    return true;
}
cs



결과




BMFont 사용하기



Resources 폴더에 BMFont 파일추가 (.fnt 파일, .png 파일)


c++)

createWithBMFont() 함수 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "test81.hpp"
 
USING_NS_CC;
 
Scene* Test81::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test81::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test81::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    //ps. .fnt파일명과 .png파일명이 같아야 한다
    //ps. 폰트크기 지정 못함
    //createWithBMFont(BMFont명, 텍스트, 가로정렬, 너비)
    auto label = Label::createWithBMFont("bitmapFontChinese.fnt""Hello World, Fonts : BMFont", TextHAlignment::CENTER, 100);
    
 
    label->setPosition(Point(150150));
    this->addChild(label);
    
    return true;
}
cs



결과




외부 TTF Fonts 사용하기



Resources > fonts 폴더에 ttf 파일 추가



ios > Icons > Info.plist

Fonts provided by application 에 fonts/폰트파일명 추가하기 


c++)

내장폰트 쓰는 방법과 똑같이 createWithSystemFont() 함수 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "test79.hpp"
 
USING_NS_CC;
 
Scene* Test79::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test79::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test79::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    //createWithSystemFont(글자,폰트,폰트크기,라벨크기,가로정렬,세로정렬)
    //내장 폰트 쓰는 법과 같다
    auto label = Label::createWithSystemFont("Hello World, Fonts : TTF""A Damn Mess"24, Size(200150), TextHAlignment::CENTER, TextVAlignment::CENTER);
    
 
    label->setPosition(Point(100100));
    this->addChild(label);
 
 
    return true;
}
cs



결과



이미지 여러개 선택하여 이미지뷰에 보이기



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.ghj.ex_003;
 
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
public class MainActivity extends AppCompatActivity {
 
    //UI
    ImageView image1, image2, image3;
 
    //constant
    final int PICTURE_REQUEST_CODE = 100;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //UI
        image1 = (ImageView)findViewById(R.id.img1);
        image2 = (ImageView)findViewById(R.id.img2);
        image3 = (ImageView)findViewById(R.id.img3);
 
        Button btnImage = (Button)findViewById(R.id.btnImage);
        btnImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
                //사진을 여러개 선택할수 있도록 한다
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),  PICTURE_REQUEST_CODE);
            }
        });
    }
 
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == PICTURE_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
 
                //기존 이미지 지우기
                image1.setImageResource(0);
                image2.setImageResource(0);
                image3.setImageResource(0);
 
                //ClipData 또는 Uri를 가져온다
                Uri uri = data.getData();
                ClipData clipData = data.getClipData();
 
                //이미지 URI 를 이용하여 이미지뷰에 순서대로 세팅한다.
                if(clipData!=null)
                {
 
                    for(int i = 0; i < 3; i++)
                    {
                        if(i<clipData.getItemCount()){
                            Uri urione =  clipData.getItemAt(i).getUri();
                            switch (i){
                                case 0:
                                    image1.setImageURI(urione);
                                    break;
                                case 1:
                                    image2.setImageURI(urione);
                                    break;
                                case 2:
                                    image3.setImageURI(urione);
                                    break;
                            }
                        }
                    }
                }
                else if(uri != null)
                {
                    image1.setImageURI(uri);
                }
            }
        }
    }
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/btnImage"
        android:text="이미지 3개 가져오기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <ImageView
        android:id="@+id/img1"
        android:scaleType="center"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
 
    <ImageView
        android:id="@+id/img2"
        android:scaleType="center"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
 
    <ImageView
        android:id="@+id/img3"
        android:scaleType="center"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
</LinearLayout>
 
cs



결과



Cocos Label 영역, 위치, 폰트, 폰트크기, 정렬 지정



c++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "test76.hpp"
 
USING_NS_CC;
 
Scene* Test76::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test76::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test76::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    //createWithSystemFont(글자,폰트,폰트크기,라벨크기,가로정렬,세로정렬)
    auto label = Label::createWithSystemFont("Hello World""Dotum"24, Size(100300), TextHAlignment::LEFT, TextVAlignment::CENTER);
    
    //label->setPosition() : 라벨의 위치지정
    label->setPosition(Point(100100));
 
    this->addChild(label);
    
    return true;
}
cs



결과




Error:The number of method references in a .dex file cannot exceed 64.


해결 사이트

https://developer.android.com/studio/build/multidex.html#mdex-gradle



build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
android {
    ...
 
    defaultConfig {
        ...
        multiDexEnabled true
    }
}
 
dependencies {
    ...
    compile 'com.android.support:multidex:1.0.0'
}
 
cs

추가



AndroidManifest.xml

1
2
3
4
5
6
<application
        ...
        android:name="android.support.multidex.MultiDexApplication">
 
    ...
</application>
cs

추가

텍스트 출력예제 (SystemFont 사용)



c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "test74.hpp"
#include "SimpleAudioEngine.h"
 
USING_NS_CC;
 
Scene* Test74::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test74::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test74::init()
{
    if ( !Layer::init())
    {
        return false;
    }
 
    //Label 사용 예제
    //SystemFont : 내장되어 있는 ttf 파일 사용
    //createWithSystemFont("텍스트" , "폰트이름" , "폰트크기")
    auto label = Label::createWithSystemFont("Hello World""arial"34);
    label->setPosition(Point(240,160));
    this->addChild(label);
    
    
    return true;
}
cs



결과




Cocos 기본 메서드 예제

(setOpacity , setScale , setRotation , setFlippedX)



c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "test71.h"
#include "SimpleAudioEngine.h"
 
USING_NS_CC;
 
Scene* Test71::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test71::create();
    scene->addChild(layer);
    
    return scene;
}
 
 
bool Test71::init()
{
    //레이어 색깔을 하얀색으로 한다
    //Color4B(r,g,b,a) : 색깔지정
    if ( !LayerColor::initWithColor(Color4B(255255255255)))
    {
        return false;
    }
    
    
    //화면의 크기 가져오기
    Size winSize = Director::getInstance()->getWinSize();
    
    //왼쪽 하단에 이미지
    auto spr1 = Sprite::create("testimage.png");
    spr1->setAnchorPoint(Point(0,0));
    spr1->setPosition(Point(0,0));
    this->addChild(spr1);
    
    //하단 가운데에 이미지
    auto spr2 = Sprite::create("testimage.png");
    spr2->setAnchorPoint(Point(0.5,0));
    spr2->setPosition(Point(winSize.width/2,0));
    /**
     setOpacity(0-255) : 0 - 투명 , 128 - 반투명 , 255 - 불투명
     */
    spr2->setOpacity(128);
    this->addChild(spr2);
 
    //하단 오른쪽에 2배하여 이미지
    auto spr3 = Sprite::create("testimage.png");
    spr3->setAnchorPoint(Point(1,0));
    spr3->setPosition(Point(winSize.width,0));
    /**
     setScale(비율) : 1> - 확대 , <1 - 축소
     */
    spr3->setScale(2.0);
    this->addChild(spr3);
 
    //가운데에 이미지
    auto spr4 = Sprite::create("testimage.png");
    spr4->setPosition(Point(winSize.width/2 , winSize.height/2));
    /**
     앵커포인트 기준으로 회전한다
     setRotation(각도) : z축 기준으로 회전
     setRotationX(각도) : x축 기준으로 회전
     setRotationY(각도) : y축 기준으로 회전
     */
    spr4->setRotation(90.0);
    this->addChild(spr4);
    
    //상단 가운데에 이미지
    auto spr5 = Sprite::create("testimage.png");
    spr5->setAnchorPoint(Point(0.5,1));
    spr5->setPosition(Point(winSize.width/2 , winSize.height));
    /**
     setFlippedX(true) : 좌우반전
     setFlippedY(true) : 상하반전
     */
    spr5->setFlippedY(true);
    this->addChild(spr5);
    
    return true;
}
cs



결과




+ Recent posts