cocos2d-x实现触摸旋转键盘的例子
2013-09-23 12:01
274 查看
效果图
RotateNode.h
RotateNode.cpp
HelloWorldScene.h
HelloWorldScene.cpp
RotateNode.h
// // RotateNode.h // 转动菜单 // // Created by 杜甲 on 13-9-23. // // #ifndef ________RotateNode__ #define ________RotateNode__ #include "cocos2d.h" USING_NS_CC; class RotateNode:public CCLayerColor{ public: CREATE_FUNC(RotateNode); virtual bool init(); CCSprite* player1; CCSprite* player2; CCSprite* player3; CCSprite* player4; CC_SYNTHESIZE(CCPoint, circleCenter, CircleCenter); void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); cocos2d::CCArray circlayerOutAlogrithm(cocos2d::CCPoint centerPoint,int radius); }; #endif /* defined(________RotateNode__) */
RotateNode.cpp
// // RotateNode.cpp // 转动菜单 // // Created by 杜甲 on 13-9-23. // // #include "RotateNode.h" bool RotateNode::init() { bool bRet = false; do { CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255, 25, 255, 255))); player1 = CCSprite::create("Icon.png"); player2 = CCSprite::create("Icon.png"); player3 = CCSprite::create("Icon.png"); player4 = CCSprite::create("Icon.png"); this->setContentSize(CCSizeMake(150, 150)); this->addChild(player1); this->addChild(player2); this->addChild(player3); this->addChild(player4); circleCenter = ccp(90, 90); circlayerOutAlogrithm(circleCenter, 150); this->setTouchEnabled(true); bRet = true; } while (0); return bRet; } void RotateNode::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { CCTouch* touch = (CCTouch*)pTouches->anyObject(); CCPoint location = this->convertTouchToNodeSpace(touch); float offRealX = location.x - circleCenter.x; float offRealY = location.y - circleCenter.y; float angleRadians = atanf(offRealY / offRealX); float angleDegress = CC_RADIANS_TO_DEGREES(angleRadians); float cocosAngle = -1 * angleDegress; // this->runAction(CCRotateTo::create(1, cocosAngle)); this->setRotation(cocosAngle); } CCArray RotateNode::circlayerOutAlogrithm(CCPoint centerPoint,int radius) { CCPoint centerPo = centerPoint; int circleRadius = radius; //与圆心成直角时,按钮的相对 x,y的长度 int xLengthN = abs(centerPo.x - circleRadius); int yLengthN = abs(centerPo.y - circleRadius); //与圆心成45度角时 int xyLengthF = sqrtf(circleRadius* circleRadius /2); int xLengthF = abs(centerPo.x - circleRadius / 2); int yLengthF = abs(centerPo.y - circleRadius / 2); CCArray* pointArray = CCArray::create(); //第一个按钮的坐标180 CCPoint point1; point1 = ccp(centerPo.x - circleRadius, centerPo.y); player1->setPosition(point1); CCPoint point2; point2 = ccp(centerPo.x - xyLengthF, centerPo.y - xyLengthF); player2->setPosition(point2); CCPoint point3; point3 = ccp(centerPo.x, centerPo.y - radius); player3->setPosition(point3); CCPoint point4; point4 = ccp(centerPo.x + xyLengthF, centerPo.y - xyLengthF); CCPoint point5; point5 = ccp(centerPo.x + circleRadius, centerPo.y); CCLog("%f",point5.x); CCPoint point6; point6 = ccp(centerPo.x + xyLengthF, centerPo.y + xyLengthF); CCPoint point7; point7 = ccp(centerPo.x,centerPo.y + radius); CCPoint point8; point8 = ccp(centerPo.x - xyLengthF, centerPo.y + xyLengthF); player4->setPosition(point8); return NULL; }
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "RotateNode.h" class HelloWorld : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); RotateNode* rotateNode; // void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } ///////////////////////////// // 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(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); rotateNode = RotateNode::create(); this->addChild(rotateNode); rotateNode->setPosition(ccp(300, 200)); return true; }
相关文章推荐
- Android 实现旋转键盘的例子
- 如何实现用户触摸屏幕就隐藏弹出的键盘功能
- Cocos2d-x Win32键盘模拟触摸事件
- [cocos2d-x] 让精灵响应触摸 并把方向旋转到相对应的角度
- cocos2d-x window实现鼠标移动 键盘事件
- cocos2d中实现触摸按钮换图效果方案
- [置顶] [cocos2d-x]捕鱼达人炮台射击角度的旋转实现
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- IOS开发之触摸背景关闭键盘的代码实现
- 使用日志钩子实现键盘消息勾挂的例子[z]
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- javascript 实现键盘上下左右功能的小例子
- <转>cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- 如何实现触摸textField以外的地方来关闭键盘?
- cocos2d-x让精灵响应触摸 并把方向旋转到相对应的角度
- 利用VB远线程注入技术实现键盘拦截的例子(无DLL)
- 如何实现触摸textField以外的地方来关闭键盘?(两种方法)
- 使用日志钩子实现键盘消息勾挂的例子
- Cocos2d-x3.2实现虚拟摇杆多点触摸