Cocos2dx中的CCScrollLayer

时间:2013-12-27 08:20:06

标签: cocos2d-x

我必须为我的图层进行垂直滚动。我尝试过CCScrollLayer,但它不起作用。我怎么能这样做?

MissionLayer.h

#include "cocos2d.h"

using namespace cocos2d;    
class MissionLayer : public CCLayer
{    
public:
    MissionLayer();
    virtual ~MissionLayer();
    CREATE_FUNC(MissionLayer);
};    

MissionLayer.cpp

MissionLayer::MissionLayer()
{
    // TODO Auto-generated constructor stub 
    CCSprite* sprite = CCSprite::create("tab2_scene.jpg");
    addChild(sprite,1);    
}    
MissionLayer::~MissionLayer() {
    // TODO Auto-generated destructor stub
}

2 个答案:

答案 0 :(得分:2)

首先,启用Mission层中的触摸

setTouchEnabled(true); //in your constructor or init method

之后仅在Y轴上移动你的(ccTouchesMoved / ccTouchMoved)将如下所示

void MissionLayer::ccTouchesMoved(CCSet* pTouches, CCEvent* event)
{
 CCTouch *touch=(CCTouch*)pTouches->anyObject();
 CCPoint newTouchLocation = touch->getLocationInView();
 newTouchLocation = CCDirector::sharedDirector()->convertToGL(newTouchLocation);

 CCPoint oldTouchLocation = touch->getPreviousLocationInView();
 oldTouchLocation = CCDirector::sharedDirector()->convertToGL(oldTouchLocation);

 //get the difference in the finger touches when the player was dragging
 float differenceY = newTouchLocation.y- oldTouchLocation.y; //Only in Y axis  
 float newPosY=getPositionY()+differenceY;

 // Now we have to check new position comes in bounding box .
 // Let assume maxY,minY are upper or lower limit    

 if (newPosY>minY) {
    newPosY=minY;
 }   
 if (newPosY < maxY) {
    newPosY=maxY;
 }
 setPositionY(newPosY);
}    

答案 1 :(得分:0)

我希望这会对你有所帮助。根据您的要求,您可以修改垂直滚动。

在CCScrollLayer.h中

#pragma once
#include "cocos2d.h"


    class CCScrollLayer : public cocos2d::CCLayer 
    {

    public:

        static CCScrollLayer* create(cocos2d::CCArray* layers,int widthOffset);

        bool init(cocos2d::CCArray *layers,int widthOffset);
            // Holds the current page being displayed
        int currentScreen;
        void moveToPage(int page);

    private:

        // Holds the current height and width of the screen
        int scrollHeight;
        int scrollWidth;

        // Holds the height and width of the screen when the class was inited
        int startHeight;
        int startWidth;



        // A count of the total screens available
        int totalScreens;

        // The initial point the user starts their swipe
        int startSwipe; 

        void moveToNextPage();
        void moveToPreviousPage();


        virtual bool ccTouchBegan(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);
        virtual void ccTouchMoved(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);
        virtual void ccTouchEnded(cocos2d::CCTouch* pTouch, cocos2d::CCEvent* pEvent);  

        virtual void onEnter();
        virtual void onExit();

        void changePageIndicator();

    };

在CCScrollLayer.cpp

#include "CCScrollLayer.h"

USING_NS_CC;

CCScrollLayer* CCScrollLayer::create(CCArray *layers, int widthOffset)
{   
    CCScrollLayer *pRet = new CCScrollLayer();
    if (pRet && pRet->init(layers, widthOffset))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

bool CCScrollLayer::init(CCArray *layers, int widthOffset)
{   
    if (CCLayer::init())
    {       
        // Set up the starting variables
        if(!widthOffset)
        {
            widthOffset = 0;
        }   
        currentScreen = 1;

        // offset added to show preview of next/previous screens
        CCSize s=CCDirector::sharedDirector()->getWinSize();

        scrollWidth  = s.width - widthOffset;
        scrollHeight = s.height;
        startWidth = scrollWidth;
        startHeight = scrollHeight;

        // Loop through the array and add the screens
        unsigned int i;
        for (i=0; i<layers->count(); i++)
        {
            CCLayer* l = static_cast<CCLayer*>(layers->objectAtIndex(i));
            l->setAnchorPoint(ccp(0,0));
            l->setPosition(ccp((i*scrollWidth),0));
            addChild(l);            
        }

        // Setup a count of the available screens
        totalScreens = layers->count();
        return true;    
    }


    return false;

}

void CCScrollLayer::onEnter()
{

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
    CCLayer::onEnter();
}

void CCScrollLayer::onExit()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

    CCLayer::onExit();
}   

void CCScrollLayer::changePageIndicator() {
    LevelSelectionScene* scene = (LevelSelectionScene*) this -> getParent();
    if(scene != NULL) {
        scene -> changePageIndicator(currentScreen);
    }
}

void CCScrollLayer::moveToPage(int page)
{   
    CCEaseBounce* changePage = CCEaseBounce::create(CCMoveTo::create(0.3f, ccp(-((page-1)*scrollWidth),0)));
    this->runAction(changePage);
    currentScreen = page;

    changePageIndicator();
}

void CCScrollLayer::moveToNextPage()
{   
    CCEaseBounce* changePage = CCEaseBounce::create(CCMoveTo::create(0.3f, ccp(-(((currentScreen+1)-1)*scrollWidth),0)));

    this->runAction(changePage);
    currentScreen = currentScreen+1;

    changePageIndicator();
}

void CCScrollLayer::moveToPreviousPage()
{   
    CCEaseBounce* changePage =CCEaseBounce::create(CCMoveTo::create(0.3f, ccp(-(((currentScreen-1)-1)*scrollWidth),0)));
    this->runAction(changePage);
    currentScreen = currentScreen-1;

    changePageIndicator();
}


bool CCScrollLayer::ccTouchBegan(CCTouch *touch, CCEvent *withEvent)
{

    CCPoint touchPoint = touch->getLocation(); // Get the touch position
    touchPoint = this->getParent()->convertToNodeSpace(touchPoint);


    startSwipe = (int)touchPoint.x;
    return true;
}

void CCScrollLayer::ccTouchMoved(CCTouch *touch, CCEvent *withEvent)
{   

    CCPoint touchPoint = touch->getLocation(); // Get the touch position
    touchPoint = this->getParent()->convertToNodeSpace(touchPoint);

    this->setPosition(ccp((-(currentScreen-1)*scrollWidth)+(touchPoint.x-startSwipe),0));
}

void CCScrollLayer::ccTouchEnded(CCTouch *touch, CCEvent *withEvent)
{
    CCPoint touchPoint = touch->getLocation(); // Get the touch position
    touchPoint = this->getParent()->convertToNodeSpace(touchPoint);

    int newX = (int)touchPoint.x;

    if ( (newX - startSwipe) < -scrollWidth / 7 && (currentScreen+1) <= totalScreens )
    {
        this->moveToNextPage();
    }
    else if ( (newX - startSwipe) > scrollWidth / 7 && (currentScreen-1) > 0 )
    {
        this->moveToPreviousPage();
    }
    else
    {
        this->moveToPage(currentScreen);        
    }   
}