最近想弄下摇杆,发现了两种方法,备份如下:
CONTENTS
使用cocoswidget来实现
很简答,配置好cocoswidget之后,就可以直接使用了。代码如下:
class WelcomeScene : public cocos2d::CCLayer { public: 。。。。。 。。。。。 virtual bool init(); void onTouched(CCObject* pSender, float fx, float fy); }; bool WelcomeScene::init() { if ( !CCLayer::init() ) { return false; } CWidgetWindow* m_pWindow = CWidgetWindow::create(); m_pWindow->setMultiTouchEnabled(true); this->addChild(m_pWindow); CControlView* pView = CControlView::create("control_baseboard.png", "control_joystick.png"); pView->setPosition(CCPoint(100, 100)); pView->setAnchorPoint(CCPointZero); pView->setRadius(pView->getContentSize().width / 2);//直接设置即可,原作者没有透漏作用 pView->setOnControlListener(this, ccw_control_selector(WelcomeScene::onTouched));//回调函数 pView->setOpacity(200);//设置透明度 m_pWindow->addChild(pView); return true; } void WelcomeScene::onTouched(CCObject* pSender, float fx, float fy) { CCLOG("x = %f y = %f", fx, fy); }
如何配置cocoswidget请参考【 http://116.62.110.235/blog/cocos2dx-study-notes-custom-engineering-path-and-cocoswidget-configuration/ 】。
截图:
自己写一个
参考
【 http://www.himigame.com/iphone-cocos2dx/721.html 】
【 http://blog.csdn.net/kuloveyouwei/article/details/9101833 】
Joystick.h:
#ifndef __JOYSTICK_H__ #define __JOYSTICK_H__ #include "cocos2d.h" class HRocker :public cocos2d::CCLayer { public : //初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景 static HRocker* HRockerWithCenter(cocos2d::CCPoint aPoint ,float aRadius ,cocos2d::CCSprite* aJsSprite,cocos2d::CCSprite* aJsBg,bool _isFollowRole); //启动摇杆 void Active(); //解除摇杆 void Inactive(); cocos2d::CCPoint getDirection(); private: HRocker * initWithCenter(cocos2d::CCPoint aPoint ,float aRadius ,cocos2d::CCSprite* aJsSprite,cocos2d::CCSprite* aJsBg,bool _isFollowRole); cocos2d::CCPoint centerPoint;//摇杆中心 cocos2d::CCPoint currentPoint;//摇杆当前位置 bool active;//是否激活摇杆 float radius;//摇杆半径 cocos2d::CCSprite *jsSprite; bool isFollowRole;//是否跟随用户点击 float getVelocity(); void updatePos(float dt); virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); CREATE_FUNC(HRocker); }; #endif // __JOYSTICK_H__
Joystick.cpp:
#include "Joystick.h" using namespace cocos2d; void HRocker::updatePos(float dt){ jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5))); } //启动摇杆 void HRocker::Active() { if (!active) { active=true; schedule(schedule_selector(HRocker::updatePos));//添加刷新函数 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0,false); }else { } } //解除摇杆 void HRocker::Inactive() { if (active) { active=false; this->unschedule(schedule_selector(HRocker::updatePos));//删除刷新 CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);//删除委托 }else { } } //摇杆方位 CCPoint HRocker::getDirection() { return ccpNormalize(ccpSub(centerPoint, currentPoint)); } //摇杆力度 float HRocker::getVelocity() { return ccpDistance(centerPoint, currentPoint); } HRocker* HRocker:: HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){ HRocker *jstick=HRocker::create(); jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg,_isFollowRole); return jstick; } bool HRocker::ccTouchBegan(CCTouch* touch, CCEvent* event) { if (!active) return false; this->setVisible(true); CCPoint touchPoint = touch->getLocationInView(); touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint); if(!isFollowRole){ if (ccpDistance(touchPoint, centerPoint) > radius){ return false; } } currentPoint = touchPoint; if(isFollowRole){ centerPoint=currentPoint; jsSprite->setPosition(currentPoint); this->getChildByTag(88)->setPosition(currentPoint); } return true; } void HRocker::ccTouchMoved(CCTouch* touch, CCEvent* event) { CCPoint touchPoint = touch->getLocationInView(); touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint); if (ccpDistance(touchPoint, centerPoint) > radius) { currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius)); }else { currentPoint = touchPoint; } } void HRocker::ccTouchEnded(CCTouch* touch, CCEvent* event) { currentPoint = centerPoint; if(isFollowRole){ this->setVisible(false); } } HRocker* HRocker::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){ isFollowRole =_isFollowRole; active = false; radius = aRadius; if(!_isFollowRole){ centerPoint =aPoint; }else{ centerPoint =ccp(0,0); } currentPoint = centerPoint; jsSprite = aJsSprite; jsSprite->setPosition(centerPoint); aJsBg->setPosition(centerPoint); aJsBg->setTag(88); this->addChild(aJsBg); this->addChild(jsSprite); if(isFollowRole){ this->setVisible(false); } this->Active();//激活摇杆 return this; }
使用方法:
class WelcomeScene : public cocos2d::CCLayer { public: 。。。。。 。。。。。 virtual bool init(); }; bool WelcomeScene::init() { if ( !CCLayer::init() ) { return false; } CCSprite *spRocker= CCSprite::create("control_joystick.png"); CCSprite *spRockerBG=CCSprite::create("control_baseboard.png"); HRocker *rocker=HRocker::HRockerWithCenter(ccp(210.0f,130.0f),50.0f ,spRocker ,spRockerBG,false); this->addChild(rocker); CCSprite *spRocker2 = CCSprite::create("control_joystick.png"); CCSprite *spRockerBG2 = CCSprite::create("control_baseboard.png"); HRocker* rocker2=HRocker::HRockerWithCenter(ccp(210.0f,130.0f),50.0f ,spRocker2 ,spRockerBG2,true); this->addChild(rocker2); return true; }
截图:
发表评论