转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25656673

效果图:

CCTouch类装载了触摸点的信息。包含触摸点的横纵坐标值和触摸点的ID号,如获取触摸点转GL坐标:

	CCPoint point = pTouch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);

创建触摸事件流程:首先开启setTouchEnabled(true), 然后重写registerWithTouchDispatcher调用触摸代理函数addTargetedDelegate同意布景层接收触摸事件,再重写ccTouchBegan、ccTouchMoved、ccTouchEnded、ccTouchCancelled函数

程序代码:

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1); setTouchEnabled(true);
sp1 = CCSprite::create("cpp1.png");
sp1->setScale(0.5f);
sp1->setPosition(ccp(100, 200));
this->addChild(sp1); return true;
} //触摸移动
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
if (iscontrol)
{
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCLOG("ccTouchMoved...x:%f y:%f", location.x, location.y); //移动时又一次设置sprite坐标
float x = location.x-deltax;
float y = location.y-deltay;
sp1->setPosition(ccp(x, y));
} } //触摸開始点击,计算该点坐标与sprite坐标差值
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{ CCPoint pos = sp1->getPosition();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);//openGL
CCLOG("ccTouchBegan...x:%f y:%f", location.x, location.y); if (location.x > 0 && location.x <960 &&
location.y >0 && location.y < 640)//触摸的矩形区域
{
iscontrol = true;
//计算触摸点与sprite的坐标差值
deltax = location.x-pos.x;
deltay = location.y-pos.y;
} return true; }
//触摸结束
void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
CCLOG("ccTouchEnded...");
//iscontrol = false; } //注冊触摸事件
void HelloWorld::onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCLayer::onEnter();
} void HelloWorld::onExit()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. android init进程分析 ueventd
  2. gollum安装教程
  3. Flex随笔
  4. MySQL数据导出导入【转】
  5. java中线程池的使用方法
  6. js基础第七天
  7. leetcode Permutation
  8. 最小生成树Jungle Roads
  9. Linux启动新进程的几种方法及比较[转]
  10. Thinkphp中的内置标签用法
  11. android新建项目
  12. java web 项目打包(war 包)并部署
  13. 3.20 总结 java程序流程控制
  14. Mac 终端美化方法
  15. ADO五大对象
  16. kvm虚拟化2-qemu-kvm
  17. String类,ThreadLocal
  18. Nginx+Django搭建
  19. 【通信】JDK中的URLConnection参数详解
  20. Ubuntu 设置NAT共享网络(命令行方法)

热门文章

  1. Android 网络编程 Socket Http
  2. Python每隔一秒钟打印当地时间
  3. TCP/IP协议栈源码图解分析系列10:linux内核协议栈中对于socket相关API的实现
  4. 用VS2005编译生成Lua库文件和解释器
  5. C++——STL中三种顺序容器的简要差别
  6. include设置属性在relativelayout布局中无效
  7. LeetCode: Valid Palindrome [125]
  8. iOS 获取高速随机路径sandbox目录
  9. 用JavaScript实现网页动态水印
  10. 《炉石传说》建筑设计欣赏(7):采用Google.ProtocolBuffers处理网络消息