您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 两点缩放

2016-06-13 19:08 423 查看
         在onTouchesBegan中,touches.size()一直是1。这对多点缩放图片,三维模型来说,挺郁闷的。本文把初始值放在onTouchesMoved 中,基本解决问题,但有一小点问题,就是第一次触摸时有小范围的缩放,因为只有移动才可以得出新的距离,但是此时的距离一定不等于之前的距离。对我的项目够了,不知道其它人能不能用。

void CocaineLayer::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event) {

if (touches.size() >= 2) {
if (mulTouchesFlag) {
auto t0 = touches[0];
auto t1 = touches[1];
auto location0 = t0->getLocation();
auto location1 = t1->getLocation();
_oldDis = (float) sqrt((location0.x - location1.x) * (location0.x - location1.x) + (location0.y - location1.y) * (location0.y - location1.y));

mulTouchesFlag = false;
} else {
auto t0 = touches[0];
auto t1 = touches[1];
auto location0 = t0->getLocation();
auto location1 = t1->getLocation();
float newDis = (float) sqrt((location0.x - location1.x) * (location0.x - location1.x) + (location0.y - location1.y) * (location0.y - location1.y));
_scale *= newDis / _oldDis;
_model->setScale(_scale * 0.01f);
if (_scale > 10.0f) {
_scale = 10.0f;
}
if (_scale < 0.7f) {
_scale = 0.7f;
}
_oldDis = newDis;
}

}
}
void CocaineLayer::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event) {

mulTouchesFlag = true;
}
仅供参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: