cocos2d-x下实现摇杆

2024-06-17 10:32
文章标签 实现 cocos2d 摇杆

本文主要是介绍cocos2d-x下实现摇杆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

cocos2d-x下实现摇杆

(2011-08-15 10:50:10)
转载
标签:

杂谈

 

实现方法采至http://www.cocoachina.com/bbs/read.php?tid-16365-keyword-ҡ��.html

他的使用oc实现,我翻译成c++版的。。

Joystick.h文件

#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文件

#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;
}

最终效果图为cocos2d-x下实现摇杆

在helloworld 里面添加

CCSprite *testPointL=CCSprite::spriteWithFile("LeftPoint.png");//摇杆
CCSprite *testBGL=CCSprite::spriteWithFile("LeftPlane.png");//摇杆背景
Joystick *testJSL=Joystick::JoystickWithCenter(ccp(80.0f,80.0f),60.0f ,testPointL ,testBGL);
this->addChild(testJSL);
testJSL->Active();

这篇关于cocos2d-x下实现摇杆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Pydantic中model_validator的实现

《Pydantic中model_validator的实现》本文主要介绍了Pydantic中model_validator的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录引言基础知识创建 Pydantic 模型使用 model_validator 装饰器高级用法mo

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文