使用Sneaky 的源码改写的在cocos2d-x 2.x后的版本中~可以使用来遥控物体

cocos2d-x的初步学习十三之虚拟摇杆SneakyInput 这篇文章中,我们将用另一个虚拟摇杆类,SneakyInput,这个是开源的,已经有c++版,SneakInput c++的源码下载地址为:https://github.com/Ntran013/SneakyInput 注意,这个下载下来需要修改,因为那是基于老版本的cocos2dx的。OK,我们下面直接上代码: CCSize wsize=CCDirector::sharedDirector 阅读详情

原帖:http://blog.youkuaiyun.com/c_qiang0_0/article/details/8006261

下面的源码是用开源的Sneaky 改的, 可以在cocos2d-x 2.0.x后的版本运行。  只需要修改几个地方。



在当前改好的源码里,我加入了JoyStick控制方向的时候改变按钮的状态。 分为diable 和press  。 加入了相应方法。 有的方法里对应的进行修改。 


如果有哪里改的不好或是还要改进的建议,需要提下,谢谢留个言~   

这个sneaky 主要是四个类。 SneakyButtton ,SneakButtonSkinnedBase,SneakyJoystic,SneakyJoysticSkinnerBase。

 我在我的工程里用到,写了个使用sneaky的类ToggleLayer。


使用的时候可以复制所有,然后修改toggleLayer的相应方法里的数据就可以达到控制目的。


SneakyButton.h~~~~~~~~~~~

#ifndef __SNEAKY_BUTTON_H__
#define __SNEAKY_BUTTON_H__

#include "cocos2d.h"

class SneakyButton : public cocos2d::CCNode, public cocos2d::CCTargetedTouchDelegate
{
public:
	static SneakyButton *initSneakyButton();

protected:
	cocos2d::CCPoint center;
	float radiusSq;

	cocos2d::CCRect bounds;

	CC_SYNTHESIZE(bool, status, Status);
	CC_SYNTHESIZE_READONLY(bool, active, IsActive);
	CC_SYNTHESIZE_READONLY(bool, value, Value);
	CC_SYNTHESIZE(bool, isHoldable, IsHoldable);
	CC_SYNTHESIZE(bool, isToggleable, IsToggleable);
	CC_SYNTHESIZE(float, rateLimit, RateLimit);

	CC_SYNTHESIZE_READONLY(float, radius, Radius);

	//Public methods
	virtual void onEnterTransitionDidFinish();
	virtual void onExit();
	bool initWithRect(cocos2d::CCRect rect);
	void limiter(float delta);
	void setRadius(float r);

	virtual bool ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchCancelled(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);

	void touchDelegateRelease();
	void touchDelegateRetain();
};

#endif



 

sneakyButton.cpp

#include "SneakyButton.h"

using namespace cocos2d;

void SneakyButton::onEnterTransitionDidFinish()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
}

void SneakyButton::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

bool SneakyButton::initWithRect(CCRect rect)
{
	bool pRet = false;
	//if(CCSprite::init()){
		
		bounds = CCRectMake(0, 0, rect.size.width, rect.size.height);
		center = CCPointMake(rect.size.width/2, rect.size.height/2);
		status = 1; //defaults to enabled
		active = false;
		value = 0;
		isHoldable = 0;
		isToggleable = 0;
		radius = 32.0f;
		rateLimit = 1.0f/120.0f;
		
		setPosition(rect.origin); //not sure about this
		pRet = true;
	//}
	return pRet;
}

void SneakyButton::limiter(float delta)
{
	value = 0;
	this->unschedule(schedule_selector(SneakyButton::limiter));
	active = false;
}

void SneakyButton::setRadius(float r)
{
	radius = r;
	radiusSq = r*r;
}

bool SneakyButton::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
	if (active) return false;

	CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
	location = this->convertToNodeSpace(location);
		//Do a fast rect check before doing a circle hit check:
	if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
		return false;
	}else{
		float dSq = location.x*location.x + location.y*location.y;
		if(radiusSq > dSq){
			active = true;
			if (!isHoldable && !isToggleable){
				value = 1;
				this->schedule(schedule_selector(SneakyButton::limiter), rateLimit);
			}
			if (isHoldable) value = 1;
			if (isToggleable) value = !value;
			return true;
		}
	}
return false;
}

void SneakyButton::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
	if (!active) return;
	
	CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
	location = this->convertToNodeSpace(location);
		//Do a fast rect check before doing a circle hit check:
	if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
		return;
	}else{
		float dSq = location.x*location.x + location.y*location.y;
		if(radiusSq > dSq){
			if (isHoldable) value = 1;
		}
		else {
			if (isHoldable) value = 0; active = false;
		}
	}
}

void SneakyButton::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
	if (!active) return;
	if (isHoldable) value = 0;
	if (isHoldable||isToggleable) active = false;
}

void SneakyButton::ccTouchCancelled(CCTouch *touch, CCEvent *event)
{
	this->ccTouchEnded(touch, event);
}

void SneakyButton::touchDelegateRelease()
{
	this->release();
}

void SneakyButton::touchDelegateRetain()
{
	this->retain();
}

SneakyButton * SneakyButton::initSneakyButton()
{
	SneakyButton *button = new SneakyButton();
	button->initWithRect(CCRectZero);
	button->autorelease();
	return button;
}


SneakyButtonSkinnedBase.h

#ifndef __SNEAKY_BUTTON_SKINNED_H__
#define __SNEAKY_BUTTON_SKINNED_H__

#include "SneakyButton.h"

class SneakyButtonSkinnedBase : public cocos2d::CCLayer 
{
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, defaultSprite, DefaultSprite);
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, activatedSprite, ActivatedSprite);
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, disabledSprite, DisabledSprite);
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, pressSprite, PressSprite);

	CC_SYNTHESIZE_READONLY(SneakyButton *, button, Button); //Not sure about this

	//Public methods
	LAYER_NODE_FUNC(SneakyButtonSkinnedBase);
	 SneakyButtonSkinnedBase();
	virtual ~SneakyButtonSkinnedBase();
	bool init();
	void watchSelf(float delta);
	void setContentSize(cocos2d::CCSize s);
	void setDefaultSprite(cocos2d::CCSprite *aSprite);
	void setActivatedSprite(cocos2d::CCSprite *aSprite);
	void setDisabledSprite(cocos2d::CCSprite *aSprite);
	void setPressSprite(cocos2d::CCSprite *aSprite);
	void setButton(SneakyButton *aButton);
};

#endif


SneakyButtonSkinnerBase.cpp

#include "SneakyButtonSkinnedBase.h"

using namespace cocos2d;

SneakyButtonSkinnedBase::~SneakyButtonSkinnedBase()
{
	if (defaultSprite)
	{
		defaultSprite->release();
		defaultSprite = NULL;
	}
	if (activatedSprite)
	{
		activatedSprite->release();
		activatedSprite = NULL;
	}
	if (disabledSprite)
	{
		disabledSprite->release();
		disabledSprite = NULL;
	}
	if (pressSprite)
	{
		pressSprite->release();
		pressSprite = NULL;
	}
	if (button)
	{
		button->release();
		button = NULL;
	}

	this->unschedule(schedule_selector(SneakyButtonSkinnedBase::watchSelf));

}

bool SneakyButtonSkinnedBase::init() //Possible errors here
{
	bool pRet = false;
	if(CCLayer::init()){
		this->defaultSprite = NULL;
		//defaultSprite->retain();
		this->activatedSprite = NULL;
		//activatedSprite->retain();
		this->disabledSprite = NULL;
		//disabledSprite->retain();
		this->pressSprite = NULL;
		//pressSprite->retain();
		this->button = NULL;
		//button->retain();

		this->schedule(schedule_selector(SneakyButtonSkinnedBase::watchSelf));
		pRet = true;
	}
	return pRet;
}

void SneakyButtonSkinnedBase::watchSelf(float delta) //Be Careful Here
{
	if (!this->button->getStatus()){
		if(disabledSprite){
			disabledSprite->setVisible(true);
		}
		else {
			disabledSprite->setVisible(false);
		}
	}
	else {
		if(!this->button->getIsActive()){
			pressSprite->setVisible(false);
			if(this->button->getValue() == 0){
				activatedSprite->setVisible(false);
				if(defaultSprite){
					defaultSprite->setVisible(true);
				}
			}
			else {
				activatedSprite->setVisible(true);
			}
		}
		else {
			defaultSprite->setVisible(false);
			if(pressSprite){
				pressSprite->setVisible(true);
			}
		}
	}
}

void SneakyButtonSkinnedBase::setContentSize(CCSize s)
{
	CCLayer::setContentSize(s);
	//if (defaultSprite)
	//{
	//	defaultSprite->setContentSize(s);
	//}
	//button->setRadius(s.width/2);
}

void SneakyButtonSkinnedBase::setDefaultSprite(CCSprite *aSprite)
{
	defaultSprite = aSprite; //Check again here
	defaultSprite->retain();
	if(aSprite){
		this->addChild(defaultSprite, 0);

		this->setContentSize(defaultSprite->getContentSize());
	}
}

void SneakyButtonSkinnedBase::setActivatedSprite(CCSprite *aSprite)
{
	/*if(activatedSprite){
		if(activatedSprite->getParent())
			activatedSprite->getParent()->removeChild(activatedSprite, true);
		activatedSprite->release();
	}*/
	aSprite->retain();
	activatedSprite = aSprite;
	if(aSprite){
		this->addChild(activatedSprite, 1);
		
		this->setContentSize(activatedSprite->getContentSize());
	}
}

void SneakyButtonSkinnedBase::setDisabledSprite(CCSprite *aSprite)
{
	if(disabledSprite){
		if(disabledSprite->getParent())
			disabledSprite->getParent()->removeChild(disabledSprite, true);
		disabledSprite->release();
	}
	aSprite->retain();
	disabledSprite = aSprite;
	if(aSprite){
		this->addChild(disabledSprite, 2);
		
		this->setContentSize(disabledSprite->getContentSize());
	}
}

void SneakyButtonSkinnedBase::setPressSprite(CCSprite *aSprite)
{
	/*if(pressSprite){
		if(pressSprite->getParent())
			pressSprite->getParent()->removeChild(pressSprite, true);
		pressSprite->release();
	}*/
	aSprite->retain();
	pressSprite = aSprite;
	if(aSprite){
		this->addChild(pressSprite, 3);
		
		this->setContentSize(pressSprite->getContentSize());
	}
}

void SneakyButtonSkinnedBase::setButton(SneakyButton *aButton)
{
	/*if(button){
		if(button->getParent())
			button->getParent()->removeChild(button, true);
		button->release();
	}*/
	aButton->retain();
	button = aButton;
	if(aButton){
		this->addChild(button, 4);
		if(defaultSprite)
			button->setRadius(defaultSprite->boundingBox().size.width/2);
	}
}

SneakyButtonSkinnedBase::SneakyButtonSkinnedBase()
{
	this->schedule(schedule_selector(SneakyButtonSkinnedBase::watchSelf));
}


SneakyJoyStick.h

#ifndef __SNEAKY_JOYSTICK_H__
#define __SNEAKY_JOYSTICK_H__

#include "cocos2d.h"

class SneakyJoystick : public cocos2d::CCNode, public cocos2d::CCTargetedTouchDelegate 
{
protected:
	float joystickRadiusSq;
	float thumbRadiusSq;
	float deadRadiusSq;

	//来来检测当前按钮是否被按下。
	CC_SYNTHESIZE_READONLY(bool, active, IsActive);
	CC_SYNTHESIZE_READONLY(cocos2d::CCPoint, stickPosition, StickPosition);
	CC_SYNTHESIZE_READONLY(float, degrees, Degrees);
	CC_SYNTHESIZE_READONLY(cocos2d::CCPoint, velocity, Velocity);
	CC_SYNTHESIZE(bool, autoCenter, AutoCenter);
	CC_SYNTHESIZE_READONLY(bool, isDPad, IsDPad);
	CC_SYNTHESIZE(bool, hasDeadzone, HasDeadzone);
	CC_SYNTHESIZE(int, numberOfDirections, NumberOfDirections);

	CC_SYNTHESIZE_READONLY(float, joystickRadius, JoystickRadius);
	CC_SYNTHESIZE_READONLY(float, thumbRadius, ThumbRadius);
	CC_SYNTHESIZE_READONLY(float, deadRadius, DeadRadius);

	virtual ~SneakyJoystick();

	bool initWithRect(cocos2d::CCRect rect);
	virtual void onEnterTransitionDidFinish();
	virtual void onExit();
	void setIsDPad(bool b);
	void setJoystickRadius(float r);
	void setThumbRadius(float r);
	void setDeadRadius(float r);
	virtual bool ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);
	virtual void ccTouchCancelled(cocos2d::CCTouch *touch, cocos2d::CCEvent *event);

	void touchDelegateRelease();
	void touchDelegateRetain();

private:
	void updateVelocity(cocos2d::CCPoint point);
	void setTouchRadius();
};
#endif


SnekayJoyStick.cpp

#include "SneakyJoystick.h"

using namespace cocos2d;

#define SJ_PI 3.14159265359f
#define SJ_PI_X_2 6.28318530718f
#define SJ_RAD2DEG 180.0f/SJ_PI
#define SJ_DEG2RAD SJ_PI/180.0f


SneakyJoystick::~SneakyJoystick()
{
}

bool SneakyJoystick::initWithRect(CCRect rect)
{
	bool pRet = false;
	//if(CCSprite::init()){
		stickPosition = CCPointZero;
		degrees = 0.0f;
		velocity = CCPointZero;
		autoCenter = true;
		isDPad = false;
		hasDeadzone = false;
		active = false;
		numberOfDirections = 4;
		
		this->setJoystickRadius(rect.size.width/2);
		this->setThumbRadius(32.0f);
		this->setDeadRadius(0.0f);
		
		//Cocos node stuff
		setPosition(rect.origin);
		pRet = true;
	//}
	return pRet;
}

void SneakyJoystick::onEnterTransitionDidFinish()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
}

void SneakyJoystick::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

float round(float r) {
    return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}

void SneakyJoystick::updateVelocity(CCPoint point)
{
	// Calculate distance and angle from the center.
	float dx = point.x;
	float dy = point.y;
	float dSq = dx * dx + dy * dy;
	
	if(dSq <= deadRadiusSq){
		velocity = CCPointZero;
		degrees = 0.0f;
		stickPosition = point;
		return;
	}

	float angle = atan2f(dy, dx); // in radians
	if(angle < 0){
		angle		+= SJ_PI_X_2;
	}
	float cosAngle;
	float sinAngle;
	
	if(isDPad){
		float anglePerSector = 360.0f / numberOfDirections * SJ_DEG2RAD;
		angle = round(angle/anglePerSector) * anglePerSector;
	}
	
	cosAngle = cosf(angle);
	sinAngle = sinf(angle);
	
	// NOTE: Velocity goes from -1.0 to 1.0.
	if (dSq > joystickRadiusSq || isDPad) {
		dx = cosAngle * joystickRadius;
		dy = sinAngle * joystickRadius;
	}
	
	velocity = CCPointMake(dx/joystickRadius, dy/joystickRadius);
	degrees = angle * SJ_RAD2DEG;
	
	// Update the thumb's position
	stickPosition = ccp(dx, dy);
}

void SneakyJoystick::setIsDPad(bool b)
{
	isDPad = b;
	if(isDPad){
		hasDeadzone = true;
		this->setDeadRadius(10.0f);
	}
}

void SneakyJoystick::setJoystickRadius(float r)
{
	joystickRadius = r;
	joystickRadiusSq = r*r;
}

void SneakyJoystick::setThumbRadius(float r)
{
	thumbRadius = r;
	thumbRadiusSq = r*r;
}

void SneakyJoystick::setDeadRadius(float r)
{
	deadRadius = r;
	deadRadiusSq = r*r;
}

bool SneakyJoystick::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
	CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
	//if([background containsPoint:[background convertToNodeSpace:location]]){
	location = this->convertToNodeSpace(location);
	//Do a fast rect check before doing a circle hit check:
	if(location.x < -joystickRadius || location.x > joystickRadius || location.y < -joystickRadius || location.y > joystickRadius){
		return false;
	}else{
		float dSq = location.x*location.x + location.y*location.y;
		if(joystickRadiusSq > dSq){
			this->updateVelocity(location);
			this->active = true;
			return true;
		}
	}
	return false;
}

void SneakyJoystick::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
	CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
	location = this->convertToNodeSpace(location);
	this->updateVelocity(location);
}

void SneakyJoystick::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
	CCPoint location = CCPointZero;
	if(!autoCenter){
		CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->locationInView());
		location = this->convertToNodeSpace(location);
	}
	this->updateVelocity(location);
	this->active = false;
}

void SneakyJoystick::ccTouchCancelled(CCTouch *touch, CCEvent *event)
{
	this->ccTouchEnded(touch, event);
}

void SneakyJoystick::touchDelegateRelease()
{
	this->release();
}

void SneakyJoystick::touchDelegateRetain()
{
	this->retain();
}


SneakyJoyStickSkinnerBase.h

#ifndef __JOYSTICK_SKINNED_H__
#define __JOYSTICK_SKINNED_H__

#include "cocos2d.h"
#include "SneakyJoystick.h"

class SneakyJoystickSkinnedBase : public cocos2d::CCLayer
{
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, backgroundSprite, BackgroundSprite);
	//当无压住按钮时候的sprite
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, disableThumbSprite, DisableThumbSprite);
	//当出现压住按钮时候的sprite
	CC_SYNTHESIZE_READONLY(cocos2d::CCSprite *, pressThumbSprite, PressThumbSprite);

	CC_SYNTHESIZE_READONLY(SneakyJoystick *, joystick, Joystick);	

	//Public methods
	LAYER_NODE_FUNC(SneakyJoystickSkinnedBase);
	virtual ~SneakyJoystickSkinnedBase();
	bool init();
	void updatePositions(float delta);
	void setContentSize(cocos2d::CCSize s);
	void setBackgroundSprite(cocos2d::CCSprite *aSprite);
	
	void setDisableThumbSprite(cocos2d::CCSprite *aSprite);
	void setPressThumbSprite(cocos2d::CCSprite *aSprite);

	void setJoystick(SneakyJoystick *aJoystick);
};
#endif


SneakyJoyStickSkinner.cpp

#include "SneakyJoystickSkinnedBase.h"

using namespace cocos2d;

SneakyJoystickSkinnedBase::~SneakyJoystickSkinnedBase()
{
	if (backgroundSprite)
	{
		backgroundSprite->release();
		backgroundSprite = NULL;
	}
	if (pressThumbSprite)
	{
		pressThumbSprite->release();
		pressThumbSprite = NULL;
	}
	if (disableThumbSprite)
	{
		disableThumbSprite->release();
		disableThumbSprite = NULL;
	}
	if (joystick)
	{
		joystick->release();
		joystick = NULL;
	}
}

bool SneakyJoystickSkinnedBase::init()
{
	bool pRet = false;
	if(CCLayer::init()){
		this->backgroundSprite = NULL;

		this->disableThumbSprite = NULL;
		this->pressThumbSprite = NULL;
		this->joystick = NULL;

		this->schedule(schedule_selector(SneakyJoystickSkinnedBase::updatePositions));
		pRet = true;
	}
	return pRet;	
}

void SneakyJoystickSkinnedBase::updatePositions(float delta)
{
	if(joystick)
	{
		pressThumbSprite->setVisible(false);
		disableThumbSprite->setVisible(false);
		if (joystick->getIsActive())
		{
			if (pressThumbSprite){
				pressThumbSprite->setVisible(true);
				pressThumbSprite->setPosition(joystick->getStickPosition());	
			}
		}
		else{
			if(disableThumbSprite)
			{
				disableThumbSprite->setVisible(true);
				disableThumbSprite->setPosition(joystick->getStickPosition());
			}
		}
	}
}

void SneakyJoystickSkinnedBase::setContentSize(CCSize s)
{
	CCLayer::setContentSize(s);
}

void SneakyJoystickSkinnedBase::setBackgroundSprite(CCSprite *aSprite)
{
	backgroundSprite = aSprite;
	backgroundSprite->retain();
	if(aSprite){
		this->addChild(backgroundSprite, 0);
		
		this->setContentSize(backgroundSprite->getContentSize());
	}
}

void SneakyJoystickSkinnedBase::setJoystick(SneakyJoystick *aJoystick)
{
	joystick = aJoystick;
	joystick->retain();
	if(aJoystick){
		this->addChild(aJoystick, 2);
		if(disableThumbSprite)
			joystick->setThumbRadius(disableThumbSprite->boundingBox().size.width/2);
		else
			joystick->setThumbRadius(0);
		
		if(backgroundSprite)
			joystick->setJoystickRadius(backgroundSprite->boundingBox().size.width/2);
	}
}

void SneakyJoystickSkinnedBase::setDisableThumbSprite( cocos2d::CCSprite *aSprite )
{
	disableThumbSprite = aSprite;
	disableThumbSprite->retain();

	if(aSprite){
		this->addChild(disableThumbSprite, 0);

		//joystick->setThumbRadius(thumbSprite->getContentSize().width/2);
	}
}

void SneakyJoystickSkinnedBase::setPressThumbSprite( cocos2d::CCSprite *aSprite )
{
	pressThumbSprite = aSprite;
	pressThumbSprite->retain();

	if(aSprite){
		this->addChild(pressThumbSprite, 1);

		//joystick->setThumbRadius(thumbSprite->getContentSize().width/2);
	}
}


ToggleLayer.h  //用来演示如何使用上面的类

#include "cocos2d.h"

#include "SneakyButton.h"
#include "SneakyButtonSkinnedBase.h"
#include "SneakyJoystick.h"
#include "SneakyJoystickSkinnedBase.h"

USING_NS_CC;
/**
*遥控层
**/
class ToggleLayer :public cocos2d::CCLayer
{
public:
	ToggleLayer(void);
	~ToggleLayer(void);
	
	//加入遥控和按钮
	void addToggleAndButton();

private:
	//摇杆和按钮触控update更新
	void update(float dt);

	SneakyButton *buttonA;
	SneakyJoystick *joystick;
	static float fireTime;
};


ToggleLayer.cpp

#include "ToggleLayer.h"
#include "GameScene.h"

ToggleLayer::ToggleLayer(void)
{
	addToggleAndButton();
}


ToggleLayer::~ToggleLayer(void)
{
}


void ToggleLayer::addToggleAndButton()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	float buttonRadius=50;		

	buttonA=new SneakyButton();
	buttonA->initWithRect(CCRectZero);
	buttonA->setIsToggleable(false);
	buttonA->setIsHoldable(true);		

	//SneakyButtonSkinnedBase *buttonASkin=new SneakyButtonSkinnedBase();
 	SneakyButtonSkinnedBase *buttonASkin= new SneakyButtonSkinnedBase();//::createSkin();
	buttonASkin->init();
	buttonASkin->setPosition(ccp(size.width-buttonRadius,buttonRadius));
	buttonASkin->setDefaultSprite(CCSprite::create("button-default.png"));//可以修改
	// buttonASkin->setDisabledSprite(CCSprite::spriteWithFile("button-disabled.png"));//
	buttonASkin->setPressSprite(CCSprite::create("button-pressed.png"));//可以修改
	buttonASkin->setActivatedSprite(CCSprite::create("button-activated.png"));//可以修改
	buttonASkin->setButton(buttonA);

	this->addChild(buttonASkin);

	float joystickRadius=50;

	joystick=new SneakyJoystick();
	joystick->initWithRect(CCRectZero);
	joystick->setAutoCenter(true);
	joystick->setHasDeadzone(true);
	joystick->setDeadRadius(10);

	SneakyJoystickSkinnedBase *joystickSkin=new SneakyJoystickSkinnedBase();
	joystickSkin->init();
	CCSprite *bgSprite = CCSprite::create("skeany_bg.png");
	joystickSkin->setBackgroundSprite(bgSprite);
	//默认的Sprite
	joystickSkin->setDisableThumbSprite(CCSprite::create("skeanyCenter.png"));  //disable的状态的图片
	//当出现压住按钮时候的sprite
	joystickSkin->setPressThumbSprite(CCSprite::create("skeanyCenter2.png")); //press的状态时候的图片

	//joystickSkin->getThumbSprite()->setScale(0.5f);
	joystickSkin->setPosition(
		ccp(bgSprite->getContentSize().width*0.5f+10,bgSprite->getContentSize().height*0.5+10));
	joystickSkin->setJoystick(joystick);

	this->addChild(joystickSkin);

	this->scheduleUpdate();
}
#define FIRE_INTERVAL 0.3f
float ToggleLayer::fireTime=0;

void ToggleLayer::update( float dt )
{
	CCPoint velocity=joystick->getVelocity();
	if(velocity.x!=0||velocity.y!=0)
	{
		velocity = ccpMult(velocity,2.5);
		CCLOG("joystick:[%f,%f]",velocity.x,velocity.y);
		//CCPoint position = GameScene::shareGameScene()->getShip()->getPosition();
		//if (GameScene::shareGameScene()->getShip())
		//{
		//	GameScene::shareGameScene()->getShip()->setPosition(ccpAdd(position,velocity));
		//}


 

<SPAN style="WHITE-SPACE: pre">	</SPAN>//这里加入你要控制方向的精灵。

	}

	fireTime+=dt;

	if(buttonA->getIsActive()&&fireTime>=FIRE_INTERVAL)
	{
		CCLOG("buttonA pressed.");
		//if(GameScene::shareGameScene()->getShip())
		//	GameScene::shareGameScene()->getShip()->shootButtleFromToggle();
		fireTime=0;
	}
}


用后的效果示意图:




 

 

数量1倍数特性.pdf 立即下载

相关推荐

【高速互连技术】合规性测试规范:物理层信号完整性与链路均衡验证系统设计

内容概要:本文档介绍了分析工具进行实践对照,并关注后续规范版本更新以确保符合最终发布标准。

cocos2d-x(2.1.4)之虚拟摇杆SneakyInput

这篇文章中,我们将用另一个虚拟摇杆类,SneakyInput,这个是开源的,已经有c++版,SneakInput c++的源码下载地址为:https://github.com/Ntran013/SneakyInput 注意,这个下载下来需要修改,因为那是基于老版本的cocos2dx的。已修改后的源码下载地址为:http://download.csdn.net/detail/my183100521

小枫的专栏 2803

万人睡眠健康与生活方式调查数据集

成年人群睡眠健康与生活方式研究数据集,合成模拟10000行20列单CSV文件,睡眠障碍标记人群占比约四分之一。设置双预测目标:睡眠障碍二分类字段与日间疲劳得分回归字段,可分别开展分类与回归建模。特征字段涵盖年龄、性别、十一种职业类别、每周工作时长、轮班工作标记、自我压力评分、身体质量指数BMI、吸烟状态、每周饮酒量、每日咖啡因摄入毫克数、睡前一小时屏幕使用时间、每周运动分钟数、日均步行数、静息心率、习惯就寝时间点、平均睡眠时长、睡眠质量自评等二十项。数据完整无缺失,结果列与特征列分离清晰,便于目标泄漏防范练习。适用场景:睡眠障碍预测建模、日间疲劳程度回归分析、咖啡因与睡前屏幕对睡眠时长的影响量化、不同职业群体睡眠质量对比研究、逻辑回归与梯度提升模型对比实战等。合成数据不含真实个人信息,适合健康数据分析教学、特征工程练习与机器学习课程实训项目使用。

底部操作栏的封装

底部操作栏包括手柄,按钮和一个帝都

kingabcde的专栏 647

FANUC-R2000iC165F-六轴机械臂码垛仿真

FANUC_R2000iC165F_六轴机械臂码垛仿真

frontend-design skills

frontend-design skills

Cocos2d-x的SneakyInput

SneakyInput是虚拟摇杆库。 SneakyInput是2dx里面的虚拟手柄。但是从官网下载下来2dx的版本太旧,要对他进行修改,适用于cocos2d-x 2.1.4版本。理论上适用所有的2dx的2.1+版本。 下载地址:http://download.csdn.net/detail/a3895177/6445821 废话少说,直接进入正题: 声明: #includ

Alen的博客 2163

政府科技管理者在推动区域科技成果转化过程中,如何解决成果供需匹配效率低的问题?.docx

政府科技管理者在推动区域科技成果转化过程中,如何解决成果供需匹配效率低的问题?

SneakInput在cocos2d-x下的示例

看了很多教程和文档,无论2d还是2d-x都推荐使用开源的SneakInput作为其触屏的手柄组件。 因此我也下载了它的源码并将其融合到自己的游戏里, 整个演示的源码下载地址为: http://download.csdn.net/detail/weyson/4469089 我的环境为vs2010 + cocos2d-1.0.1-x-0.12.0 另外SneakInput c++的源码下载地...

mmdev 182

用于计算系统状态的卡尔曼最优增益和最小均方误差(MMSE)估计研究(Matlab代码实现)

内容概要:本文围绕用于计算系统状态的卡尔曼最优增益和最小均方误差(MMSE)估计展开研究,深入探讨了卡尔曼滤波在动态系统状态估计中的核心作用。文章系统阐述了卡尔曼滤波的数学基础,包括状态空间模型的建立、先验与后验状态估计、协方差矩阵的递推更新,以及最优增益矩阵的推导过程,确保在高斯白噪声环境下实现对系统状态的最小均方误差估计。结合Matlab代码实现,完整展示了算法从理论建模到仿真实验的全流程,涵盖系统建模、噪声设置、滤波迭代、状态更新与误差分析等环节,并通过仿真实验验证了算法在噪声干扰下对系统真实状态的高精度跟踪能力。该研究为控制工程、导航系统、信号处理及自动化领域中的状态估计问题提供了坚实的理论依据与高效的数值实现方案。; 适合人群:具备一定控制理论、概率统计基础和Matlab编程能力,从事自动化、电子信息、航空航天、机器人、导航定位等相关领域的科研人员、工程技术人员及高校研究生。; 使用场景及目标:①掌握卡尔曼滤波的基本原理及其在状态估计中的数学推导过程;②学习如何利用Matlab实现卡尔曼滤波算法并进行系统仿真与性能评估;③应用于目标跟踪、传感器融合、惯性导航、故障诊断、时间序列预测等实际工程中的动态状态估计问题。; 阅读建议:建议读者结合文中提供的Matlab代码进行动手实践,通过调整系统模型参数、过程噪声与观测噪声强度,观察滤波效果的变化,深入理解卡尔曼增益的自适应调节机制与MMSE估计的收敛特性。同时可进一步拓展学习扩展卡尔曼滤波(EKF)、无迹卡尔曼滤波(UKF)等非线性滤波方法,以应对更复杂的非线性系统状态估计挑战。

高校技术转移办公室人员如何提升成果转化服务效率?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

Cocos2d开发系列(六)

对于射击类游戏,使用重力感应进行游戏控制是不可接受的,采用虚拟手柄将会更恰当。出于“不重新发明轮子”的原则,我们将采用开源库SneakyInput。 控制玩家的飞船进行移动只是其中一件事情。我们还需要让背景能够滚动,以造成在某个方向上“前进”的感觉。为此必须自己实现背景滚动。由于CCParallaxNode的限制,它不能无限制地滚动卷轴式背景。

kmyhy的专栏 1万+

国央企创新负责人如何利用平台实现技术需求的精准挖掘与匹配?.docx

科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。

16周全【Harness&Hermes】多智能体开发特训营

2026年的AI开发者圈子里,几乎没有人再怀疑多智能体的价值。Gartner预测年底40%的企业应用将内置智能体,LangChain、AutoGen、CrewAI等框架轮番登场,演示视频里的AI团队分工明确、配合流畅。然而,一个尴尬的现实是:大多数开发者手里跑通的多智能体项目,停留在Jupyter Notebook的最后一行。一旦试图把它塞进真实业务环境,问题便如潮水般涌来——长任务跑到一半崩了要重来,智能体之间的消息黑盒般不可追溯,换个场景就得推倒重写,上线之后连“刚才那次调用为什么花了三倍的钱”都查不出来。 这正是【Harness&Hermes】多智能体开发特训营试图回应的命题。它的切入点不在“再教你写一个Demo”,而在于一个更朴素却更稀缺的能力:让多智能体系统从演示玩具变成可交付、可运维、可演进的生产资产。 要理解这门课的价值,得先看清多智能体开发当前的断层在哪里。单Agent时代,提示词写得好、API调得通,基本就能交差。但多Agent协作的本质是编排:谁负责拆解需求,谁负责执行,谁负责校验,上下文如何在角色之间传递而不丢失,某个环节出错时整条链路如何回退。这些问题的难度不在模型能力,而在工程控制。市面上不缺讲LangGraph节点怎么连、RAG检索怎么做的教程,缺的是把“多个智能体在真实业务压力下稳定协作”这件事讲透的人。 Harness与Hermes的分工,恰好对应了工程控制的左右手。根据课程的设计逻辑,Harness管“流程”,Hermes管“记忆与进化”。Harness 负责长任务的流程治理:一个面试模拟流程跑了二十分钟,中途用户关掉页面,下次打开能不能接着上次的进度继续?某个Agent调用工具超时了,整条链路是崩溃还是优雅降级?线上出了问题,日志能不能精确到“第几轮对话、哪个Agent、调了什么工具、花了多少token”。这些听起来不性感,但任何一

自适应多机器人编队规划,以包围和跟踪具有运动和可见性约束的目标(Matlab代码实现)

内容概要:本文提出了一种自适应多机器人编队规划方法,旨在实现对具有运动能力和可见性约束目标的高效包围与持续跟踪,采用Matlab进行算法实现与仿真验证。该方法综合考虑了多机器人系统在动态环境中的协同控制、编队形态生成与保持、障碍物规避以及目标可见性维护等关键问题,通过设计分布式控制律使编队能够根据目标行为和环境变化自适应调整队形与运动轨迹,确保稳定包围并最大化目标观测质量。文中详细阐述了系统的数学建模过程、控制策略设计原理及约束处理机制,并提供了多场景下的仿真实验结果,验证了所提方法在复杂条件下的鲁棒性、灵活性与有效性。; 适合人群:具备自动化、机器人学、控制理论或人工智能等相关领域基础知识的科研人员及研究生,熟悉Matlab编程环境并致力于多智能体系统、协同控制、路径规划或智能无人系统研究的专业人士。; 使用场景及目标:①应用于多机器人系统在监视、围捕、搜救等任务中对移动目标的协同包围与跟踪控制;②为研究分布式编队控制、可见性优化、动态避障与多智能体协同决策等核心技术提供可复现的算法实例与理论支撑; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现细节,重点关注控制律设计、约束建模与仿真结果对比部分,宜在仿真环境中调整参数配置以深入理解系统性能边界与优化潜力。

考虑风光火储+需求响应+P2G多能源系统NSGA-II多目标优化调度(Matlab代码实现)

考虑风光火储+需求响应+P2G多能源系统NSGA-II多目标优化调度(Matlab代码实现)内容概要:本文介绍了基于NSGA-II多目标优化算法对包含风能、光伏、火电、储能系统、需求响应及电转气(P2G)等多种能源形式的综合能源系统进行优化调度的研究,采用Matlab进行代码实现。研究综合考虑了系统运行成本、碳排放、能源利用率等多个优化目标,通过建立精确的数学模型,结合可再生能源出力不确定性、负荷波动以及需求响应机制,实现多能源协同调度的帕累托最优解集求解,旨在提升能源系统的经济性与环保性,并验证所提方法的有效性和实用性。; 适合人群:具备一定电力系统、优化算法及Matlab编程基础的研究生、科研人员以及从事能源系统规划与运行相关工作的工程技术人员。; 使用场景及目标:①应用于多能源互补的综合能源系统优化调度研究;②为实现低碳、高效、可靠的能源供应提供决策支持;③学习和掌握NSGA-II算法在复杂多目标优化问题中的实际应用方法。; 阅读建议:建议读者结合Matlab代码与文中模型描述逐步理解算法实现流程,重点关注目标函数构建、约束处理及Pareto解集的生成与分析,有条件者可进一步修改参数或扩展模型以深化理解。

上一篇: 一个高效的A-star寻路算法(四方向)
下一篇: cocos2dx监听home键,锁屏,后台转前台暂停功能
java886o
博客等级 码龄19年 23粉丝 21原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值