Cocos2d-x 单点触摸--让我们用手指动起来的精灵
转载请注明出处: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 单点触摸--让我们用手指动起来的精灵的更多相关文章
- Cocos2d-x示例:单点触摸事件
为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例.使用事件触发器实现单点触摸事件.该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们. ...
- 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬
1 触摸 Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能. 2 单点触摸 打开触摸开关和触摸方式 setTouchEnabled(true); setTouchMode(kCCT ...
- registerWithTouchDispatcher 注册单点触摸事件
Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...
- Cocos2d-x开发实例:单点触摸事件
下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程.感受一下它的缺点和优点.该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们. 下面我们看看HelloWorldScen ...
- Cocos2d-x实例:单点触摸事件
addChild(boxC,30, kBoxC_Tag); ...
- android开发之单点触摸
相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...
- cocos2dx触屏响应(单点触摸)CCTouchBegan,CCTouchMove,CCTouchEnd
今天白白跟大家分享一下cocos2dx单点触摸经验. cocos2dx触摸CCTouch类的单点触摸有四个函数CCTouchBegan,CCTouchMove,CCTouchEnd,CCTouchCa ...
- quick-cocos2d-x游戏开发【9】——单点触摸
quick的触摸机制,我想廖大已经在这篇文章里说的非常清楚了.我们这些小辈们就是在他的基础上完备一下,说说使用方法就能够了.嘿嘿. 在2.2.3之前的版本号(不包含2.2.3).触摸机制和廖大在那篇文 ...
- COCOS2D-X多层单点触摸分发处理方案?
如今的问题是点击button的时候,会触发底层的触摸事件,怎么不触发底层的触摸事件啊?
随机推荐
- unity中的MonoBehaviour.OnMouseDown()
在官网的api文档中仅说明了 Description OnMouseDown is called when the user has pressed the mouse button while ov ...
- C++ Primer 学习笔记_98_特殊的工具和技术 --优化内存分配
特殊的工具和技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自己主动执行合适的构造函数来初始化每一个动态 ...
- 本地或者服务器同时启动2个或多个tomcat
一,修改配置文件server.xml的端口 C:\apache-tomcat-5.5.23-1\conf\server.xml用记事本什么的打开修改3个地方 第一: <Server port ...
- SWT的TreeViewer和TableViewer的交互
左边是一个TreeViewer,右边是一个TableViewer.当点击左边的treeitem的时候,右边的tableViewer要将该item的子节点信息显示出来.就像这样: 左边的treeView ...
- 推测的手机型号和cpu模型
<span style="font-size:18px;">推断手机型号:</span> <span style="font-size:18 ...
- 对consistencygroup的一些研究和实践
声明: 本博客欢迎转载,但请保留原作者信息! 作者:李人可 团队:华为杭州OpenStack团队 consistency group,直译是一致性组,是Juno版本号cinder新引进的一个概念.顾名 ...
- 去掉word中向下的箭头^l----->^p
去掉word中向下的箭头 在网页上复制文章到word中,会发现有很多向下的箭头,这些 符号叫做软回车符.如何去掉这些向下的箭头呢.步骤如下: 方法/步骤 按Ctrl+H,弹出全局替换窗口,输入查找内容 ...
- Java开发环境的基本设置
作为Java的刚開始学习的人,不知道其它的刚開始学习的人有没有和我一样的感受:用Java开发须要配置这么复杂 的环境.太难了.第一次配置时,一团混乱.Oracle监听服务打不开了,PLSql连接不上O ...
- Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍
原文 Visual Studio跨平台开发实战(4) - Xamarin Android基本控制项介绍 前言 不同于iOS,Xamarin 在Visual Studio中针对Android,可以直接设 ...
- HTTPDNS成为移动互联网的标配–原因与原理解析(转)
DNS,作用就是将域名解析成IP.一个DNS查询,先从本地缓存查找,如果没有或者已经过期,就从DNS服务器查询,如果客户端没有主动设置DNS服务器,一般是从服务商DNS服务器上查找.这就出现了不可控. ...