现在很多手机APP都有模拟操控摇杆的功能,今天项目中接触到一个委托事件,我顺便试试实现这个摇杆功能。
代码如下
/*
* Joystick.h
* YaoGan
*
* Created by Liu Yanghui on 11-10-27.
* Copyright 2011 ard8. All rights reserved.
*
*/
#ifndef Joystick_H
#define Joystick_H
#include "cocos2d.h"
using namespace cocos2d;
class Joystick :public CCLayer {
public :
CCPoint centerPoint;//摇杆中心
CCPoint currentPoint;//摇杆当前位置
bool active;//是否激活摇杆
float radius;//摇杆半径
CCSprite *jsSprite;
void Active();
void Inactive();
CCPoint getDirection();
float getVelocity();
void updatePos(ccTime dt);
//初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景
static Joystick* JoystickWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);
Joystick * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
LAYER_NODE_FUNC(Joystick);
};
#endif
/*
* Joystick.cpp
* YaoGan
*
* Created by Liu Yanghui on 11-10-27.
* Copyright 2011 ard8. All rights reserved.
*
*/
#include "Joystick.h"
void Joystick::updatePos(ccTime dt){
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5)));
}
void Joystick::Active()
{
if (!active) {
active=true;
schedule(schedule_selector(Joystick::updatePos));//添加刷新函数
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0,false);//添加触摸委托
}else {
}
}
//冻结摇杆
void Joystick::Inactive()
{
if (active) {
active=false;
this->unschedule(schedule_selector(Joystick::updatePos));//删除刷新
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);//删除委托
}else {
}
}
bool Joystick::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if (!active)
return false;
CCPoint touchPoint = touch->locationInView(touch->view());
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if (ccpDistance(touchPoint, centerPoint) > radius)
return false;
currentPoint = touchPoint;
return true;
}
void Joystick::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->locationInView(touch->view());
touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);
if (ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}else {
currentPoint = touchPoint;
}
}
void Joystick::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
currentPoint = centerPoint;
}
//获取摇杆方位,注意是单位向量
CCPoint Joystick::getDirection()
{
return ccpNormalize(ccpSub(centerPoint, currentPoint));
}
//获取摇杆力度
float Joystick::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg){
Joystick *jstick=Joystick::node();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg);
return jstick;
}
Joystick* Joystick::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg){
active = false;
radius = aRadius;
centerPoint = aPoint;
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
aJsBg->setPosition(centerPoint);
this->addChild(jsSprite);
this->addChild(aJsBg);
return this;
}
好了,基本都实现了摇杆的功能,最后我来调用一下,看看结果如何。随便创建一个场景
初始化时加入我要得资源文件。
CCSize screenSize=CCDirector::sharedDirector()->getWinSize();
CCSprite *mainBg=CCSprite::spriteWithFile("bg.png");
mainBg->setPosition(ccp(screenSize.width*0.5,screenSize.height*0.5));
this->addChild(mainBg);
CCSprite *testPointL=CCSprite::spriteWithFile("point.png");//摇杆
CCSprite *testBGL=CCSprite::spriteWithFile("joytickbg.png");//摇杆背景
Joystick *testJSL=Joystick::JoystickWithCenter(ccp(80.0f,80.0f),60.0f ,testPointL ,testBGL);
this->addChild(testJSL);
testJSL->Active();
return true;
运行,Ok,一切正常。
汗。突然发现了个很悲催的事,csdn不许我上传图片。注册不达一星期。我勒个去。以后加上效果图吧