Cocos2d-x 单点触摸--让我们用手指动起来的精灵
2015-07-03 20:50
666 查看
转载请注明出处:/article/1379401.html
效果图:
CCTouch类装载了触摸点的信息。包含触摸点的横纵坐标值和触摸点的ID号,如获取触摸点转GL坐标:
创建触摸事件流程:首先开启setTouchEnabled(true), 然后重写registerWithTouchDispatcher调用触摸代理函数addTargetedDelegate同意布景层接收触摸事件,再重写ccTouchBegan、ccTouchMoved、ccTouchEnded、ccTouchCancelled函数
程序代码:
效果图:
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地图系统1-8
- Cocos2d-x 3.0final 终结者系列教程14-L新abel-Cocos2d-x公文
- cocos2dx的CC_CALLBACK是个什么宏
- 将 cocos2dx 背景 设置 成 透明 的方法
- cocos2dx3.x 的变化总结
- Cocos 开发环境安装与编译(CocosForWindows)
- cocos2dx 大地图分块加载的研究(初)
- cocos2d-x截图功能clippingnode它也可用于——白费
- cocos2dx[3.2](11)——新回调函数std::bind
- android cocos2d demo
- cocos2dx 几个精灵按照顺序播放动画解决方法
- Cocos2d-x 3.2编译Android程序错误的解决方案
- cocos2dx 3.5 资源文件加密
- quick-cocos2dx 3.3 c++绑定到Lua
- cocos2dx3.6 实现带光标的输入框(二);光标可移动
- Cocos2d-x-3.6学习(二)----全平台配置
- cocos2d-x v3.3开发环境配置
- cocos2d-x v3.3开发环境配置
- 14.2Cocos2d-x 中的粒子系统(笔记);14.2.1 ParticleBatchNode;
- cocos2d-x3.2 scrollView