전체 이미지중 일부 이미지만 보여주기 및 레이어 이동하기



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
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
 
USING_NS_CC;
 
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    
    auto layer = HelloWorld::create();
    scene->addChild(layer);
 
    return scene;
}
 
 
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
 
    /**
        Hello World 출력하기
     */
    //Hello World 라벨객체
    auto label = Label::createWithTTF("Hello World""fonts/Marker Felt.ttf"24);
    //위치지정
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
 
    //레이어에 추가
    this->addChild(label, 1);
 
    
    /**
        Image 추가하기
     */
    //create("추가할 이미지 파일명")
    //Rect(x,y,width,height) : 왼쪽 상단이 0,0 이된다 , 전체 이미지중 (x,y)에서 (x+width,y+height) 의 사각형
    auto spr = Sprite::create("testimage.png" , Rect(0,0,30,30));   //메모리 관리 Cocos에서 직접관리
    //Anchor Point 지정
    spr -> setAnchorPoint(Point(0.5,0.5));  //기본값 : 0.5 0.5
    //이미지 위치 지정 (Anchor Point의 위치)
    spr -> setPosition(Point(100,100));     //기본값 : 0 0
    //레이어에 추가
    this -> addChild(spr);
    //이미지의 가운데 위치가 100,100으로 지정
    
    
    //레이어의 위치 이동
    this->setPosition(Point(100,100));
    
    return true;
}
cs



결과

레이어가 이동하여 자식 객체인 이미지와 글자의 위치가 바뀌었다 (글자는 영역을 넘어가서 안보임)

+ Recent posts