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的时候,会触发底层的触摸事件,怎么不触发底层的触摸事件啊?
随机推荐
- OpenLayers学习笔记4——使用jQuery UI实现測量对话框
OpenLayers学习最好的方式就是跟着其自带的演示样例进行学习,另外对web前端的开发设计要了解,慢慢积累,这样在一般的小项目中应该是足够用了. 本篇參照量測demo实现对话框形式的量測,抛砖引玉 ...
- 全栈JavaScript之路(十八)HTML5 自己定义数据属性
HTML5 规范规定,用户能够为元素 自己定义非标准属性, 可是要加入 data- 前缀. 目的是为元素提供与页面渲染无关的信息.或者语义信息.这些属性名能够任意加入,仅仅要带上前缀 data- 开头 ...
- 利用 C++ 单向链表实现队列
利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取. #pragma once #include "stdio.h ...
- Javascript Base64编码与解码
原文:[转]Javascript Base64编码与解码 <html> <head> <META HTTP-EQUIV="MSThemeCompatible&q ...
- java整合flex
java+flex项目整合 评论0 字号:大中小 订阅 第一种:javaproject和flexproject独立 这样的方式也是非常多人使用的方式.flex程序猿和java程序猿相互独立的工作. ...
- SQL SERVER IN参数化处理
方法一. CREATE TABLE [dbo].[Users] ( Id INTEGER IDENTITY(1, 1) PRIMARY KEY , Name NVARCHAR(50) NOT NULL ...
- HDU4309-Seikimatsu Occult Tonneru(最大流)
Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- android 去除标题
//去除标题,必须在setContentView之前设置 requestWindowFeature(Window.FEATURE_NO_TITLE);
- OWIN与Katana
OWIN与Katana详解 前言 我胡汉三又回来了,!!!!, 最近忙成狗,实在没空写博文,实在对不起自己,博客园上逛了逛发现 我大微软还是很给力的 asp.net core 1.0 .net c ...
- DB2 “The transaction log for the database is full” 存在的问题及解决方案
DB2在执行一个大的insert/update操作的时候报"The transaction log for the database is full.. "错误,查了一下文档是DB ...