基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动

2023-12-16 02:59

本文主要是介绍基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本来以为引擎会自带这个摇杆类,后来查了一下,Cocos2d-x里面没有,自己写了一个来试试,基本可以使用,感觉还可以,拿出来和大家分享一下,可能还有很多不足之处,请大家指出

头文件:

<pre name="code" class="cpp">
#ifndef _CONTROLLER_H_
#define _CONTROLLER_H_#include "cocos2d.h"
USING_NS_CC;class Controller : public Layer
{
public:bool init();CREATE_FUNC(Controller);static Scene* createScene();bool onTouchBegan(Touch *touch, Event *event);void onTouchMoved(Touch *touch, Event *event);void onTouchEnded(Touch *touch, Event *event);private:Point m_centerPoint;Point m_currentPoint;Point m_PlanePoint;float m_radius;Sprite* m_Sprite;Sprite* m_bgSprite;Sprite* m_plane;Size m_size;void update(float dt);EventListenerTouchOneByOne* m_listener;};
#endif

 源文件: 

#include "Controller.h"
#include "VisibleRect.h"Scene* Controller::createScene()
{auto scene = Scene::create();auto layer = Controller::create();scene->addChild(layer);return scene;
}
bool Controller::init()
{m_radius = 60.0;m_size = Director::getInstance()->getWinSize();m_PlanePoint = Vec2(m_size.width / 2, m_size.height / 2);auto bg = Sprite::create("image/background.png");bg->setPosition(m_size.width / 2, m_size.height / 2);this->addChild(bg);m_bgSprite = Sprite::create("yellow_round.png");m_bgSprite->setPosition(VisibleRect::leftBottom().x + m_bgSprite->getContentSize().width / 2, VisibleRect::bottom().y + m_bgSprite->getContentSize().height / 2);this->addChild(m_bgSprite,10);auto size = m_bgSprite->getContentSize();m_centerPoint = Vec2(size.width / 2, size.height / 2);m_currentPoint = m_centerPoint;m_Sprite = Sprite::create("CloseSelected.png");m_Sprite->setPosition(m_centerPoint);m_Sprite->setScale(1.5);this->addChild(m_Sprite,10);m_plane = Sprite::create("hero2.png");m_plane->setPosition(m_PlanePoint);this->addChild(m_plane,10);this->scheduleUpdate();m_listener = EventListenerTouchOneByOne::create();m_listener->onTouchBegan = CC_CALLBACK_2(Controller::onTouchBegan, this);m_listener->onTouchMoved = CC_CALLBACK_2(Controller::onTouchMoved, this);m_listener->onTouchEnded = CC_CALLBACK_2(Controller::onTouchEnded, this);_eventDispatcher->addEventListenerWithSceneGraphPriority(m_listener, this);return true;
}void Controller::update(float dt)
{m_Sprite->setPosition(m_currentPoint);//右if (m_currentPoint.x - m_centerPoint.x > 0&&m_plane->getPosition().x+m_plane->getContentSize().width/2<m_size.width){m_plane->setPosition(m_plane->getPosition().x + 3,m_plane->getPosition().y);m_plane->setFlipX(false);}//左if (m_currentPoint.x - m_centerPoint.x < 0 && m_plane->getPosition().x - m_plane->getContentSize().width / 2>0){m_plane->setPosition(m_plane->getPosition().x - 3, m_plane->getPosition().y);m_plane->setFlipX(true);}//上if (m_currentPoint.y - m_centerPoint.y > 0 && m_plane->getPosition().y + m_plane->getContentSize().height / 2<m_size.height){m_plane->setPosition(m_plane->getPosition().x, m_plane->getPosition().y+3);}//下if (m_currentPoint.y - m_centerPoint.y < 0 && m_plane->getPosition().y - m_plane->getContentSize().height / 2>0){m_plane->setPosition(m_plane->getPosition().x, m_plane->getPosition().y - 3);}}bool Controller::onTouchBegan(Touch *touch, Event *event)
{auto point = touch->getLocationInView();point = Director::getInstance()->convertToGL(point);if (ccpDistance(point, m_centerPoint) > m_radius){return false;}else{m_currentPoint = point;	}return true;}void Controller::onTouchMoved(Touch *touch, Event *event)
{auto point = touch->getLocationInView();point = Director::getInstance()->convertToGL(point);if (ccpDistance(point, m_centerPoint) > m_radius){m_currentPoint = ccpAdd(m_centerPoint, ccpMult(ccpNormalize(ccpSub(point, m_centerPoint)), m_radius));}else{m_currentPoint = point;}
}void Controller::onTouchEnded(Touch *touch, Event *event)
{m_currentPoint = m_centerPoint;
}



好,demo本身实现比较简单,只是单纯满足摇杆功能实现,以及简单控制角色移动。效果还不是很好,会加以改进

这篇关于基于Cocos2d-x3.2的虚拟摇杆实现及操控角色移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配