cocos2d-x - Cocos2dx 中的 CCScrollLayer

标签 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
}

最佳答案

首先,在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);
}    

关于cocos2d-x - Cocos2dx 中的 CCScrollLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20797285/

相关文章:

cocos2d-x - cocos2d::CCmoveTO

android - CCSprite 不是 NULL。但无法访问属性。

module - 为什么Lua(cocos2d-x)中需要返回 bool 值

c++ - 如何使用 rapidjson 查看 .json 中的数组? (cocos2d-x)

c++ - 如何在 cocos2d-x 的另一个线程中运行自定义网络代码?

ios - 当主线程被阻塞时,MBProgressHUD 没有及时显示

android - cocos2dx如何在android上读取文件

cocos2d-x - 在cocos2d-x中动态更新颜色数据

javascript - 从实用程序文件访问 cc.Director 成员和函数

c++ - Cocos2dx中通过plist文件加载动画