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

2024-02-16 11:40

本文主要是介绍使用Sneaky 的源码改写的在cocos2d-x 2.x后的版本中~可以使用来遥控物体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面的源码是用开源的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));
//}
	//这里加入你要控制方向的精灵。
}
fireTime+=dt;
if(buttonA->getIsActive()&&fireTime>=FIRE_INTERVAL)
{
CCLOG("buttonA pressed.");
//if(GameScene::shareGameScene()->getShip())
//	GameScene::shareGameScene()->getShip()->shootButtleFromToggle();
fireTime=0;
}
}


使用后的效果示意图:




这篇关于使用Sneaky 的源码改写的在cocos2d-x 2.x后的版本中~可以使用来遥控物体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/714427

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时