{"id":17688204,"url":"https://github.com/ccapton/cocos2d-x-rolecontroller","last_synced_at":"2025-03-30T21:28:04.512Z","repository":{"id":176302386,"uuid":"137926208","full_name":"Ccapton/Cocos2d-x-RoleController","owner":"Ccapton","description":"This a role controller using Cocos2d-x , which can be used in 2d game designing.这是一个使用cocos2dx引擎设计的精灵移动控制器，适用于开发2d游戏","archived":false,"fork":false,"pushed_at":"2018-06-20T06:02:33.000Z","size":5456,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-06T01:41:22.394Z","etag":null,"topics":["cocos2d-x","cocos2dx","controller"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ccapton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-19T17:50:02.000Z","updated_at":"2020-03-18T17:32:41.000Z","dependencies_parsed_at":"2023-06-29T21:48:46.481Z","dependency_job_id":null,"html_url":"https://github.com/Ccapton/Cocos2d-x-RoleController","commit_stats":null,"previous_names":["ccapton/cocos2d-x-rolecontroller"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ccapton%2FCocos2d-x-RoleController","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ccapton%2FCocos2d-x-RoleController/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ccapton%2FCocos2d-x-RoleController/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ccapton%2FCocos2d-x-RoleController/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ccapton","download_url":"https://codeload.github.com/Ccapton/Cocos2d-x-RoleController/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246382633,"owners_count":20768242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cocos2d-x","cocos2dx","controller"],"created_at":"2024-10-24T11:43:45.976Z","updated_at":"2025-03-30T21:28:04.490Z","avatar_url":"https://github.com/Ccapton.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cocos2d-x-RoleController\nThis a role controller using Cocos2d-x , which can be used in 2d game designing.\n.这是一个使用cocos2dx引擎设计的精灵移动控制器，适用于开发2d游戏\n\n![](https://raw.githubusercontent.com/Ccapton/Cocos2d-x-RoleController/master/rolecontroller_demo.gif)\n\n### 引用\n1、将Classes内的RoleController.cpp和RoleController.h文件复制到你的Classes目录下，并在你想要的场景或者层中引入。\n\n2、RoleController类的创建和使用,例如\n```\n// 在你的合适位置初始化控制器即可\nbool MainMenuScene::init() {\n\tif (!Scene::init()) {\n\t\treturn false;\n\t}\n\n\twinSize = Director::getInstance()-\u003egetWinSize();\n\n        // 创建你要控制的精灵，并添加到容器中\n\trole = LayerColor::create(Color4B::BLUE);\n\trole-\u003esetContentSize(Size(30,30));\n\trole-\u003esetPosition(winSize.width/2,winSize.height/2);\n\taddChild(role,0);\n \n\t// 初始化控制器，并添加到容器中\n\tauto controller = RoleController::createController(80,Vec2(50,80));\n\t// MainMenuScene（场景）类要继承RoleControllerListenr接口，然后将this指向的实例对象作为参数传进去\n\tcontroller-\u003esetRoleControllerListenr(this);  \n\taddChild(controller,1); \n\n\n\treturn true;\n}\n\n// 控制器父类容器需要继承RoleControllerListenr接口，例如\n#include \"RoleController.h\"\n\nclass MainMenuScene : public cocos2d::Scene , public RoleControllerListenr\n{\npublic:\n  LayerColor * role; \n  Vec2 m_velocity;\n  Size winSize;\n  // ... 其他代码\n  \n  // 声明3个接口的抽象函数，并在你对应的cpp文件中实现它们\n  void onControllerTouchBegan(Vec2 velocity); \n  void onControllerTouchMoving(Vec2 velocity);\n  void onControllerTouchEnded(Vec2 velocity);\n}\n\n// 在对应的cpp文件中实现这3个RoleControllerListenr接口的方法,例如在MainMenuScene.cpp中\nvoid MainMenuScene::onControllerTouchBegan(Vec2 velocity)\n{\n\tCCLOG(\"MainMenuScene start %f %f\", velocity.x, velocity.y);\n\tthis-\u003em_velocity = velocity;\n\tscheduleUpdate();\n}\nvoid MainMenuScene::onControllerTouchMoving( Vec2 velocity)\n{\n\tCCLOG(\"MainMenuScene moving %f %f\", velocity.x, velocity.y);\n\tthis-\u003em_velocity = velocity;\n}\nvoid MainMenuScene::onControllerTouchEnded(Vec2 velocity)\n{\n\tCCLOG(\"MainMenuScene end %f %f\", velocity.x, velocity.y);\n\tunscheduleUpdate();\n}\n\n// 在此更新函数内，编写控制角色位置的逻辑代码\nvoid MainMenuScene::update(float dt) { \n\tfloat speed = 3.0f; // 每一帧角色移动的速度\n\t// 因为是默认的更新方法，所以dt的值为 1/60 秒，即一秒内定时器会执行60次该方法，\n        // 因此要将偏移量除以 60 ,防止因每一帧偏移量太大而使角色位移太快\n\tVect newPosition = role-\u003egetPosition() + Vec2(this-\u003em_velocity.x, this-\u003em_velocity.y) / 60.0f * speed;\n\tif (newPosition.x\u003e 0 \u0026\u0026 newPosition.x \u003c winSize.width \n\t\t\u0026\u0026 newPosition.y \u003e 0 \u0026\u0026 newPosition.y \u003c winSize.height)\n\t   role-\u003esetPosition(newPosition);\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccapton%2Fcocos2d-x-rolecontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fccapton%2Fcocos2d-x-rolecontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fccapton%2Fcocos2d-x-rolecontroller/lists"}