Sairin 2014-03-18 09:45 采纳率: 0%
浏览 1958

cocos2dx 图片的多点触控缩放

#include "HelloWorld.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);

    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();
    CC_BREAK_IF(! layer);

    // add layer as a child to scene
    scene->addChild(layer);
} while (0);

// return the scene
return scene;

}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////

    CC_BREAK_IF(! CCLayer::init());


    setTouchEnabled(true);
    pSprite=CCSprite::create("map.jpg");

    pSprite->setPosition(ccp(160,240));

    this->addChild(pSprite);

       mscale=1.0;
    bRet = true;
} while (0);

return bRet;

}

void HelloWorld::registerWithTouchDispatcher(void)
{
CCDirector ::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
}

void HelloWorld::ccTouchesBegan(CCSet pTouches, CCEvent *pEvent)
{
if(pTouches->count()>=2)
{
CCSetIterator iter=pTouches->begin();
CCPoint mPoint1=((CCTouch
)(*iter))->locationInView();
mPoint1=CCDirector::sharedDirector()->convertToGL(mPoint1);
iter ++;
CCPoint mPoint2 =((CCTouch*)(*iter))->locationInView();
mPoint2=CCDirector::sharedDirector()->convertToGL(mPoint2);

    distance=sqrt((mPoint1.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
    deltax =(mPoint1.x+mPoint2.x)/2-pSprite->getPositionX();
    deltay =(mPoint1.y+mPoint2.y)/2-pSprite->getPositionY();
}

}
void HelloWorld::ccTouchesMoved(CCSet pTouches, CCEvent *pEvent)
{
if(pTouches->count()>=2)
{
CCSetIterator iter=pTouches->begin();
CCPoint mPoint1=((CCTouch
)(*iter))->locationInView();
mPoint1=CCDirector::sharedDirector()->convertToGL(mPoint1);
iter ++;
CCPoint mPoint2 =((CCTouch*)(*iter))->locationInView();
mPoint2=CCDirector::sharedDirector()->convertToGL(mPoint2);
double mdistance=sqrt((mPoint1.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
mscale=mdistance/distance*mscale;
distance=mdistance;
pSprite->setScale(mscale);
double x=(mPoint1.x+mPoint2.x)/2-deltax;
double y=(mPoint1.y+mPoint2.y)/2-deltay;
pSprite->setPosition(ccp(x,y));
deltax=(mPoint1.x+mPoint2.x)/2-pSprite->getPositionX();
deltay=(mPoint1.y+mPoint2.y)/2-pSprite->getPositionY();
}

}

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{

}
void HelloWorld::ccTouchesCancelled(CCSet *pTouch, CCEvent *pEvent)
{

}

在手机上测试的时候发现只要一点屏幕就黑了,请问是什么问题

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改
    • ¥15 请看一下,学校实验要求,我需要具体代码
    • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
    • ¥20 MATLAB绘制两隐函数曲面的交线