小羊驼和你一起学习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);


 



 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值