您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x实现触摸旋转键盘的例子

2013-09-23 12:01 274 查看
效果图



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: