转载请注明出处: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();
}

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

Cocos2d-x 单点触摸--让我们用手指动起来的精灵的更多相关文章

  1. Cocos2d-x示例:单点触摸事件

    为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例.使用事件触发器实现单点触摸事件.该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们. ...

  2. 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬

     1 触摸 Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能. 2 单点触摸 打开触摸开关和触摸方式 setTouchEnabled(true); setTouchMode(kCCT ...

  3. registerWithTouchDispatcher 注册单点触摸事件

    Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...

  4. Cocos2d-x开发实例:单点触摸事件

    下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程.感受一下它的缺点和优点.该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们.   下面我们看看HelloWorldScen ...

  5. Cocos2d-x实例:单点触摸事件

    addChild(boxC,30, kBoxC_Tag);                                                                        ...

  6. android开发之单点触摸

    相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...

  7. cocos2dx触屏响应(单点触摸)CCTouchBegan,CCTouchMove,CCTouchEnd

    今天白白跟大家分享一下cocos2dx单点触摸经验. cocos2dx触摸CCTouch类的单点触摸有四个函数CCTouchBegan,CCTouchMove,CCTouchEnd,CCTouchCa ...

  8. quick-cocos2d-x游戏开发【9】——单点触摸

    quick的触摸机制,我想廖大已经在这篇文章里说的非常清楚了.我们这些小辈们就是在他的基础上完备一下,说说使用方法就能够了.嘿嘿. 在2.2.3之前的版本号(不包含2.2.3).触摸机制和廖大在那篇文 ...

  9. COCOS2D-X多层单点触摸分发处理方案?

    如今的问题是点击button的时候,会触发底层的触摸事件,怎么不触发底层的触摸事件啊?  

随机推荐

  1. WPF界面设计技巧(8)—自制山寨版CheckListBox

    原文:WPF界面设计技巧(8)-自制山寨版CheckListBox 近年来IT市场山寨横行啊,我们今天也来发扬一下山寨精神,搞个自制的CheckListBox出来. 喏,CheckListBox 就是 ...

  2. 安装dotnet core

    CentOS 7.1下安装dotnet core .NET CORE的官方(http://dotnet.github.io/getting-started/)只提供了Windows, Ubuntu14 ...

  3. robot framework 使用四:分层设计和截图以及注意事项

    再说一下眼下的主要环境信息和版本号: 操作系统:win7 64位 python版本号:2.7.6 RIDE版本号:1.2.3 selenium2library:1.5.0 selenium:2.40. ...

  4. windows phone (16) UI变换 下

    原文:windows phone (16) UI变换 下 上一篇中说到四个变换类,都是比较简单的,这里要说到四个变换类,分别为: MatrixTransfrom矩阵变换,一句标准矩阵表示的变换 Tra ...

  5. uvalive 2911 Maximum(贪心)

    题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...

  6. 使IIS Express支持其他网络客户端访问

    今天尝试利用Android客户端Web浏览器访问VS2012 IIS Express调试中的Web应用,发现这个IIS Express仅支持localhost主机名地址访问. 上网搜索找到解决方案,几 ...

  7. BDB (Berkeley DB)简要数据库(转载)

    使用最近DBD.然后搜了下相关资料,首先公布的是一门科学: 转会http://www.javaeye.com/topic/202990 DB综述DB最初开发的目的是以新的HASH訪问算法来取代旧的hs ...

  8. CentOS6.5 Nginx优化编译配置

    说到Nginx,它真的算是我在运维工作中的好朋友,它优异的性能和极高的工作效率实在是让人大爱,来自internet的报告称其epoll模型能够支持高达50000个并发连接数. Epoll[维基百科]: ...

  9. bat脚本命令

    注:本文转载地址 http://www.cnblogs.com/yefengmeander/archive/2011/12/01/2887978.html 1.Echo 命令  打开回显或关闭请求回显 ...

  10. APUE学习总结

    简介 本文总结了个人,一个数字,对应称号<APUE>第一版的每一章,但是,独立的二级标题和书,人需求进行编写. 3.文件I/O 本章所说明的函数常常被称之为不带缓存的I/O(与第5章中说明 ...