본문 바로가기
프로그래밍/cocos2d-x

cocos2d-x 패럴렉스노드(ParallaxNode)

by -현's- 2013. 6. 2.
반응형


●두개이상의 배경 이미지 패럴랙스노드에 추가


- HelloWorldScene.cpp


#include "HelloWorldScene.h"


using namespace cocos2d;


CCScene* HelloWorld::scene()

{

    CCScene *scene = CCScene::create();

    

    HelloWorld *layer = HelloWorld::create();


    scene->addChild(layer);


    return scene;

}


bool HelloWorld::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )

    {

        return false;

    }


    /////////////////////////////

//배경 스프라이트1

CCSprite* background1 = CCSprite::create("Images/background1.png");

background1->setAnchorPoint(ccp(0,0));

//배경 스프라이트2

CCSprite* background2 = CCSprite::create("Images/background2.png");

background2->setAnchorPoint(ccp(0,0));


//패럴랙스노드를 만든다.

CCParallaxNode* voidNode = CCParallaxNode::create();


//배경 스트라이트를 패럴랙스노드에 넣는다.

voidNode->addChild(background1, 1, ccp(1.0f, 0.0f), ccp(0,0));

voidNode->addChild(background2, 1, ccp(1.0f, 0.0f), ccp(512,0));



CCActionInterval* go = CCMoveBy::create(4, ccp(-512, 0));

CCActionInterval* goBack = go->reverse();

CCFiniteTimeAction* seq = CCSequence::create(go, goBack, NULL);

CCAction* act = CCRepeatForever::create((CCActionInterval*)seq);


voidNode->runAction(act);

this->addChild(voidNode);

    return true;

}









●배경과 스프라이트 이미지 동시에 움직이기


- HelloWorldScene.cpp


#include "HelloWorldScene.h"


using namespace cocos2d;


CCScene* HelloWorld::scene()

{

    CCScene *scene = CCScene::create();

    

    HelloWorld *layer = HelloWorld::create();


    scene->addChild(layer);


    return scene;

}


bool HelloWorld::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )

    {

        return false;

    }


    /////////////////////////////

//배경레이어1

CCSprite* background1 = CCSprite::create("Images/background1.png");

background1->setAnchorPoint(ccp(0,0));

//배경레이어2

CCSprite* background2 = CCSprite::create("Images/background2.png");

background2->setAnchorPoint(ccp(0,0));


//패럴랙스노드를 만든다.

CCParallaxNode* voidNode = CCParallaxNode::create();


//배경 스트라이트를 패럴랙스노드에 넣는다.

voidNode->addChild(background1, 1, ccp(1.0f, 0.0f), ccp(0,0));

voidNode->addChild(background2, 1, ccp(1.0f, 0.0f), ccp(512,0));



CCActionInterval* go = CCMoveBy::create(4, ccp(-512, 0));

CCActionInterval* goBack = go->reverse();

CCFiniteTimeAction* seq = CCSequence::create(go, goBack, NULL);

CCAction* act = CCRepeatForever::create((CCActionInterval*)seq);


voidNode->runAction(act);

this->addChild(voidNode);


//움직이는 용 추가

CCTexture2D *texture=CCTextureCache::sharedTextureCache()

->addImage("Images/dragon_animation.png");

CCAnimation *animation=CCAnimation::create();

animation->setDelayPerUnit(0.1);


for(int i=0; i<6; i++)

{

int index = i%4;

int rowIndex=i/4;


animation->addSpriteFrameWithTexture(texture, CCRectMake(index*130, rowIndex*140, 130, 140));

}

//스프라이트 생성

CCSprite* dragon=CCSprite::createWithTexture(texture, CCRectMake(0,0,130,140));

dragon->setPosition(ccp(240,260));

this->addChild(dragon);


CCAnimate *animate=CCAnimate::create(animation);

CCAction* rep=CCRepeatForever::create(animate);

dragon->runAction(rep);

    return true;

}









반응형

댓글