여러이미지로 배경 구현하기



h++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "cocos2d.h"
 
USING_NS_CC;
 
class Test254 : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    // implement the "static create()" method manually
    CREATE_FUNC(Test254);
    
    
    void initBG();   //배경
};
 
#endif
cs



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
81
82
83
84
85
#include "test254.hpp"
 
USING_NS_CC;
 
Scene* Test254::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test254::create();
    scene->addChild(layer);
    
    
    return scene;
}
 
 
bool Test254::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    initBG();
    
    return true;
}
 
 
//배경그리기
void Test254::initBG(){
    /** 첫번째 배경레이어 **/
    //배경레이어를 추가한다
    auto bgLayer1 = Layer::create();
    this->addChild(bgLayer1);
    
    //배경이미지를 배경레이어에 추가한다
    auto sprite = Sprite::create("background2.png");
    sprite->setAnchorPoint(Point::ZERO);
    sprite->setPosition(Point::ZERO);
    bgLayer1->addChild(sprite);
    
    //가로크기만큼 추가 스프라이트 이미지 객체를 만들어서 배경레이어 오른쪽 끝에 추가한다
    auto sprite1 = Sprite::create("background2.png", Rect(00480320));
    sprite1->setAnchorPoint(Point::ZERO);
    sprite1->setPosition(Point(2000,0));
    bgLayer1->addChild(sprite1);
    
    //이미지의 가로크기인 2500만큼 왼쪽으로 이동한다
    auto action0 = MoveBy::create(10.0, Point(-2000,0));
    //이미지의 오른쪽 끝에 위치하면 원래 위치로 이동한다
    auto action1 = Place::create(Point::ZERO);
    //2개의 액션을 묶는다
    auto action2 = Sequence::create(action0, action1, NULL);
    //묶은 액션을 반복한다
    auto action3 = RepeatForever::create(action2);
    //배경레이어에 애니메이션 적용한다
    bgLayer1->runAction(action3);
    
    
    
    /** 두번째 배경레이어 **/
    auto bgLayer2 = Layer::create();
    this->addChild(bgLayer2);
    
    //배경이미지 추가
    auto sprite2 = Sprite::create("background3.png");
    sprite2->setAnchorPoint(Point::ZERO);
    sprite2->setPosition(Point::ZERO);
    bgLayer2->addChild(sprite2);
    
    //가로크기만큼 추가 스프라이트 이미지 객체를 만들어서 배경레이어 오른쪽 끝에 추가한다
    auto sprite3 = Sprite::create("background3.png", Rect(0,0,480,114));
    sprite3->setAnchorPoint(Point::ZERO);
    sprite3->setPosition(Point(2000,0));
    bgLayer2->addChild(sprite3);
    
    //배경레이어2에 애니메이션 적용한다
    auto action4 = MoveBy::create(5.0, Point(-2000,0));
    auto action5 = Place::create(Point::ZERO);
    auto action6 = Sequence::create(action4, action5, NULL);
    auto action7 = RepeatForever::create(action6);
    bgLayer2->runAction(action7);
}
cs



결과

이미지 2개로 배경을 구현하였다



+ Recent posts