cocos2d-x模拟摇杆
2013-10-23 18:46
204 查看
现在很多手机APP都有模拟操控摇杆的功能,今天项目中接触到一个委托事件,我顺便试试实现这个摇杆功能。
代码如下
[cpp] view
plaincopy
/*
* 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
[cpp] view
plaincopy
/*
* 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;
}
好了,基本都实现了摇杆的功能,最后我来调用一下,看看结果如何。随便创建一个场景
初始化时加入我要得资源文件。
[cpp] view
plaincopy
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;
代码如下
[cpp] view
plaincopy
/*
* 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
[cpp] view
plaincopy
/*
* 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;
}
好了,基本都实现了摇杆的功能,最后我来调用一下,看看结果如何。随便创建一个场景
初始化时加入我要得资源文件。
[cpp] view
plaincopy
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;
相关文章推荐
- 模拟摇杆操作
- 模拟摇杆操作
- iPhone上模拟摇杆 控制对象的代码例子下载
- iPhone游戏中模拟摇杆的教程
- android模拟摇杆绘制
- 【cocos2dx 3.3 lua】07 模拟摇杆
- 几行代码为您的游戏增加模拟摇杆
- iPhone游戏中模拟摇杆的教程
- 模拟请求页式存储管理--LRU
- Javascript 模拟 ArrayList
- 菜鸟如何用servlet模拟一个简单的登陆
- JavaScript模拟重力状态下抛物运动的方法
- php通过curl模拟功能总结
- ab 模拟测试秒杀
- poj Pots(BFS)(路径记录)(模拟)
- spring集成httpclient配置 模拟httpclient
- Wannafly挑战赛4,C题割草机,(模拟)
- 关于模拟方式
- python模拟鼠标、捕获按键事件
- Chrome浏览器通过切换user agent模拟其他浏览器方法