Hi there,
I’m very new to cocos2dx, can some one point me some good example of drag to pan and pinch to zoom layer.
I tried many codes found in this forum and google, but none of theme are work for me, due to version of cocos2dx i’m using is the latest one.
I also tried to port codes writing in objective-c to cpp, it turns out that many functions do not work the same.
I decided to write my own, it nearly done but there is a jumping bug. I hope some one could help
maybe I should post my code here:
IsoController.h
#include "cocos2d.h"
class IsoController: public cocos2d::Layer {
cocos2d::Point _oneTouchStart;
cocos2d::Point _prevLayerPosition;
int _nTouches;
float _initialDistance;
bool _lock;
public:
virtual bool init();
virtual ~IsoController();
CREATE_FUNC(IsoController);
virtual void update(float delta);
void ccTouchesBegan(cocos2d::Set *touches, cocos2d::Event *event);
void ccTouchesMoved(cocos2d::Set *touches, cocos2d::Event *event);
void ccTouchesEnded(cocos2d::Set *touches, cocos2d::Event *event);
};
IsoController.cpp
#include "IsoController.h"
USING_NS_CC;
bool IsoController::init() {
if (!Layer::init()) {
return false;
}
Size winSize = Director::getInstance()->getWinSize();
this->setTouchEnabled(true);
this->setAnchorPoint(Point(0.0f, 0.0f));
this->setPosition(Point(winSize.width/2, winSize.height/2));
_nTouches = 0;
_oneTouchStart = Point(-1, -1);
_prevLayerPosition = Point(-1, -1);
_initialDistance = -1;
_lock = false;
Sprite *bg = Sprite::create("HelloWorld.png");
this->addChild(bg);
this->setContentSize(bg->getContentSize());
return true;
}
IsoController::~IsoController() {
}
void IsoController::update(float delta) {
}
void IsoController::ccTouchesBegan(Set *touches, Event *event) {
int n = touches->count();
_nTouches += n;
}
void IsoController::ccTouchesMoved(Set *touches, Event *event) {
if (_nTouches == 1 && !_lock) { //Pan
Touch *touch = (Touch*)touches->anyObject();
Point location = touch->getLocationInView();
location = Director::getInstance()->convertToGL(location);
if (_oneTouchStart.x < 0) {
_oneTouchStart = location;
_prevLayerPosition = this->getPosition();
}else {
this->setPosition(Point(_prevLayerPosition.x-(_oneTouchStart.x-location.x), _prevLayerPosition.y-(_oneTouchStart.y-location.y)));
}
}else if (_nTouches == 2) { //Zoom
SetIterator it = touches->begin();
for (int i=0; icount(); i++) {
Touch *touch = (Touch*)(*it);
Point location = touch->getLocationInView();
location = Director::getInstance()->convertToGL(location);
if (i == 0) {
_oneTouchStart = location;
}else {
float x = (_oneTouchStart.x - location.x);
float y = (_oneTouchStart.y - location.y);
float z = sqrtf(x*x + y*y);
if (_initialDistance < 0.0f) {
_initialDistance = z;
Point point = this->convertToNodeSpace(Point((_oneTouchStart.x+location.x)/2, (_oneTouchStart.y+location.y)/2));
Point t_newAnchor = Point(
(point.x/(this->getContentSize().width)),
(point.y/(this->getContentSize().height)
));
this->setAnchorPoint(t_newAnchor);
// I think, I should have some code to avoid camera jumping here
}
float zoom = this->getScale();
zoom += (z-_initialDistance)*0.005f;
if (zoom > 2.0f) {
zoom = 2.0f;
}
if (zoom < 0.5f) {
zoom = 0.5f;
}
this->setScale(zoom);
_initialDistance = z;
}
it++;
_lock = true;
}
}
}
void IsoController::ccTouchesEnded(Set *touches, Event *event) {
int n = touches->count();
_nTouches -= n;
if (_nTouches < 0) {
_nTouches = 0;
}
_oneTouchStart = Point(-1, -1);
_initialDistance = -1.0f;
if (_nTouches <= 0) {
_lock = false;
}
}