//GameLayer.h

#ifndef __GAME_LAYER_H__
#define __GAME_LAYER_H__
 
#include "cocos2d.h"
using namespace cocos2d;

class GameLayer : public cocos2d :: CCLayer
{

public :
    virtual bool init();
 
    CREATE_FUNC(GameLayer);

        virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pevent);
        virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pevent);
        virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pevent);
        virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pevent);

        void createBackgroundParallax();
        void createButtons();
        bool isTouchInside(CCSprite* sprite, CCTouch* touch);
        void startMovingBackground();
        void stopMovingBackground();
        void moveBackground(float t);

        CCSprite* leftidle;
        CCSprite* leftclicked;
        CCSprite* rightidle;
        CCSprite* rightclicked;
        CCSprite* okidle;
        CCSprite* okclicked;

        CCSize winSize;
        bool isLeftPressed;
        bool isRightPressed;
        bool isOkPressed;

};
 
#endif //  __GAME_LAYER_H__

//GameLayer.cpp
#include "GameLayer.h"

#define IMG_WIDTH 2560

bool GameLayer::init()
{
        winSize = CCDirector::sharedDirector()->getWinSize();
        this->setTouchEnabled(true);
        this->createBackgroundParallax();
        this->createButtons();

        return true;
}

void GameLayer::createBackgroundParallax()
{
        ////캐릭터
        CCSprite* hero = CCSprite::create("ch.jpg");
        hero->setAnchorPoint(ccp(500,300));

        //배경
        CCSprite* background1 = CCSprite::create("scene_stage1_1.png");
        background1->setAnchorPoint(ccp(0,0));
        CCSprite* background2 = CCSprite::create("scene_stage1_2.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(1280,0));                

        voidNode->setTag(1);

        this->addChild(voidNode,0);
}

void GameLayer::createButtons()
{
        //left button idle
        leftidle = CCSprite::create("btn_left_idle.png");
        leftidle->setPosition(ccp(50,70));
        this->addChild(leftidle,2);

        //left button clicked
        leftclicked = CCSprite::create("btn_left_hover.png");
        leftclicked->setPosition(leftidle->getPosition());
        this->addChild(leftclicked,1);


        //right button idle
        rightidle = CCSprite::create("btn_right_idle.png");
        rightidle->setPosition(ccp(150,70));
        this->addChild(rightidle,2);

        //right button clicked
        rightclicked = CCSprite::create("btn_right_hover.png");
        rightclicked->setPosition(rightidle->getPosition());
        this->addChild(rightclicked,1);


        //right button idle
        okidle = CCSprite::create("btn_ok_idle.png");
        okidle->setPosition(ccp(1230,70));
        this->addChild(okidle,2);

        //right button clicked
        okclicked = CCSprite::create("btn_ok_hover.png");
        okclicked->setPosition(okidle->getPosition());
        this->addChild(okclicked,1);
}

void GameLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pevent)
{
        CCSetIterator it = pTouches->begin();
        CCTouch* touch = (CCTouch*)(*it);

        isLeftPressed = 0;
        isRightPressed = 0;

        if(this->isTouchInside(leftidle, touch) == true)
        {
                leftidle->setVisible(0);
                isLeftPressed = 1;
        }
        else if(this->isTouchInside(rightidle,touch)==true)
        {
                leftidle->setVisible(0);
                isRightPressed = 1;
        }
        if(isLeftPressed == true || isRightPressed == true)
        {
                this->startMovingBackground();
        }
}
void GameLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pevent)
{
        CCSetIterator it = pTouches->begin();
        CCTouch* touch = (CCTouch*)(*it);

        CCPoint touchPoint = touch->getLocation();

        if(isLeftPressed == 1 && this->isTouchInside(leftidle,touch)==1)
        {
                leftidle->setVisible(1);
                this->startMovingBackground();
        }
        else if(isRightPressed == 1 && this->isTouchInside(rightidle,touch)==1)
        {
                rightidle->setVisible(1);
                this->startMovingBackground();
        }
}
void GameLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pevent)
{
        if(isLeftPressed == 1 || isRightPressed == 1)
                this->startMovingBackground();
        if(isLeftPressed == 1)
                leftidle->setVisible(1);
        if(isRightPressed == 1)
                rightidle->setVisible(1);
}

//만약 전화가 오는경우 호출된다
void GameLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pevent)
{

}

bool isTouchInside(CCSprite* sprite, CCTouch* touch)
{
        CCPoint touchPoint = touch->getLocation();
        CCRect rect = sprite->boundingBox();

        if(rect.containsPoint(touchPoint))
        {
                return 1;
        }
        return 0;
}

void GameLayer::startMovingBackground()
{
        if(isLeftPressed == 1 && isRightPressed == 1)
        {
                return;
        }
        this->schedule(schedule_selector(GameLayer::moveBackground));
}

void GameLayer::stopMovingBackground()
{
        this->unschedule(schedule_selector(GameLayer::moveBackground));
}

void GameLayer::moveBackground(float t)
{
        //GameLayer에 있는 패럴렉스 노드를 받는다
        CCNode *voidNode = this->getChildByTag(1);

        //매 프레임마다 움직일 거리
        CCPoint moveStep = ccp(3,0);

        if(isRightPressed)
        {
                moveStep.x = -moveStep.x;
        }

        float bgParallaxRatio = 1.0f;

        CCPoint newPos = ccp(voidNode->getPosition().x + moveStep.x, voidNode->getPosition().y);

        //배경이 끝에 닿으면 움직이지 않는다
        if(isLeftPressed == 1 && newPos.x > 0)
        {
                newPos.x = 0;
        }
        else if(isRightPressed == 1 && newPos.x < -(IMG_WIDTH - winSize.width)/bgParallaxRatio)
        {
                newPos.x = -(IMG_WIDTH - winSize.width) / bgParallaxRatio;
        }
        voidNode->setPosition(newPos);
}



1>GameLayer.obj : error LNK2019: "public: bool __thiscall GameLayer::isTouchInside(class cocos2d::CCSprite *,class cocos2d::CCTouch *)" (?isTouchInside@GameLayer@@QAE_NPAVCCSprite@cocos2d@@PAVCCTouch@3@@Z) 외부 기호(참조 위치: "public: virtual void __thiscall GameLayer::ccTouchesBegan(class cocos2d::CCSet *,class cocos2d::CCEvent *)" (?ccTouchesBegan@GameLayer@@UAEXPAVCCSet@cocos2d@@PAVCCEvent@3@@Z) 함수)에서 확인하지 못했습니다.
1>A:\Cocos2d-x\cocos2d-2.0-x-2.0.4\Debug.win32\HelloCpp.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다.


라이브러리랑 헤더랑 다 확인했는데 모르겠음 존나 예제 보고 치는것도 힘들다