Cocos2d-x 单点触摸--让精灵随手指移动起来
2014-05-12 22:19
288 查看
转载请注明出处:/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 让精灵随手指移动起来二(简单实现)
- Cocos2d-x 让精灵随手指移动起来二(简单实现)
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- Cocos2d-x《雷电大战》(2)-精灵随手指移动,你点哪我走哪!
- Cocos2d-x 《雷电大战》-精灵随手指移动,你点哪我走哪!
- Cocos2d-x《雷电大战》(2)-精灵随手指移动,你点哪我走哪!
- cocos2d-x触摸点使精灵移动到触摸点的位置
- Cocos2d-x中,如何通过触摸来移动一个精灵
- cocos2d-x精灵移动的方法
- cocos2d-x精灵移动的方法
- [cocos2d-x] 让精灵响应触摸 并把方向旋转到相对应的角度
- cocos2d-x 3.5 Layer移动只是显示移动,触摸区域没动
- Quick cocos2dx-Lua(V3.3R1)学习笔记(8) ---- 事件篇之单点触摸事件,让我们用精灵模仿一个按钮吧
- 如何用cocos2d-x来开发简单的Uphone游戏:(二) 移动的精灵
- cocos2d-x 精灵触摸事件
- 使用Android绘图技术绘制一个椭圆形,然后通过触摸事件让该椭圆形跟着手指移动