小羊驼和你一起学习cocos2d-x之四(摇杆)

本文转之himi的blog

#ifndef HRocker_H 
#define HRocker_H 
#include "cocos2d.h"   
using namespace cocos2d;   
class HRocker :public CCLayer 
{   
public :     
	//初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景     
	static HRocker*  HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);       
	//启动摇杆     
	void Active();       
	//解除摇杆     
	void Inactive();   
private:     
	HRocker * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);       
	CCPoint centerPoint;//摇杆中心      
	CCPoint currentPoint;//摇杆当前位置      
	bool active;//是否激活摇杆       
	float radius;//摇杆半径       
	CCSprite *jsSprite;      
	bool isFollowRole;//是否跟随用户点击       
	CCPoint getDirection();       
	float getVelocity();       
	void  updatePos(float dt);       
	virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);     
	virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);     
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);     
	CREATE_FUNC(HRocker); 
}; 
#endif 
#include "HRocker.h"   
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; 
} 		//CCSprite *spRocker=CCSprite::create("hand_shank.png");
		//CCSprite *spRockerBG=CCSprite::create("hand_shank_background.png");
		//HRocker *rocker=HRocker::HRockerWithCenter(ccp(210.0f,130.0f),50.0f ,spRocker ,spRockerBG,false);
		//this->addChild(rocker);


 



 
 

你可能感兴趣的:(小羊驼和你一起学习cocos2d-x之四(摇杆))