타일이미지를 이용한 배경스크롤 하기



h++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef test260_hpp
#define test260_hpp
 
#include "cocos2d.h"
 
USING_NS_CC;
 
class Test260 : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    // implement the "static create()" method manually
    CREATE_FUNC(Test260);
    
    
    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
#include "test260.hpp"
 
USING_NS_CC;
 
Scene* Test260::createScene()
{
    auto scene = Scene::create();
    
    auto layer = Test260::create();
    scene->addChild(layer);
    
    
    return scene;
}
 
 
bool Test260::init()
{
    if ( !Layer::init())
    {
        return false;
    }
    
    
    initBG();
    
    return true;
}
 
 
//배경그리기
void Test260::initBG(){
    //배경레이어
    auto bgLayer = Layer::create();
    this->addChild(bgLayer);
    
    //이미지를 반복하여 추가한다 (가로8개 세로6개)
    for(int i=0; i<8; i++){
        for(int j=0; j<6; j++){ //이미지 끊김 현상을 없애기 위해 타일이미지 1개를 더 추가한다
            auto sprite = Sprite::create("tile.png");
            sprite->setAnchorPoint(Point::ZERO);
            sprite->setPosition(Point(i*72 , j*72));
            bgLayer->addChild(sprite);
        }
    }
    
    //배경레이어에 애니메이션 적용
    //전체화면이 아닌 타일이미지 만큼만 스크롤한다
    auto action1 = MoveBy::create(2.0, Point(0,-72));
    auto action2 = Place::create(Point::ZERO);
    auto action3 = Sequence::create(action1, action2, NULL);
    auto action4 = RepeatForever::create(action3);
    bgLayer->runAction(action4);
}
 
 
cs



결과

아래방향으로 스크롤 된다



+ Recent posts