首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CCScrollLayer in Cocos2dx

CCScrollLayer in Cocos2dx
EN

Stack Overflow用户
提问于 2013-12-27 08:20:05
回答 2查看 561关注 0票数 2

我得为我的图层做垂直滚动。我试过CCScrollLayer,但不起作用。我该怎么做?

MissionLayer.h

代码语言:javascript
复制
#include "cocos2d.h"

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

MissionLayer.cpp

代码语言:javascript
复制
MissionLayer::MissionLayer()
{
    // TODO Auto-generated constructor stub 
    CCSprite* sprite = CCSprite::create("tab2_scene.jpg");
    addChild(sprite,1);    
}    
MissionLayer::~MissionLayer() {
    // TODO Auto-generated destructor stub
}
EN

回答 2

Stack Overflow用户

发布于 2013-12-27 12:38:53

首先,在任务层启用触摸

代码语言:javascript
复制
setTouchEnabled(true); //in your constructor or init method

之后,只在Y轴上运动,您的(ccTouchesMoved/ccTouchMoved)将如下所示

代码语言:javascript
复制
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);
}    
票数 2
EN

Stack Overflow用户

发布于 2014-12-05 14:08:28

我希望这能帮到你。根据您的要求,您可以修改垂直滚动。

在CCScrollLayer.h中

代码语言:javascript
复制
#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中

代码语言:javascript
复制
#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);        
    }   
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20797285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档