我必须为我的图层进行垂直滚动。我尝试了 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/