2.Cocos2dx 3.2重力系统Box2D
1
加入Box2D相关的库
步骤1:右击项目所在的解决方式à加入—>现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D\proj.win32\Box2D.vcxproj
步骤2:右击项目à生成依赖项à项目依赖项à将关于libBox2D的复选框选中
步骤3:为项目加入libBox2D的库
方法:右击项目à属性à链接器à输入—>附加依赖项à编辑,加入上libBox2d.lib,à确定
案例说明:
1.编写T32.h |
#ifndef #define #include USING_NS_CC; #define #define #endif |
2.编写TBack.h |
#ifndef #define #include class { public: CREATE_FUNC(TBack); bool }; #endif |
3编写TBack.cpp |
#include bool { Layer::init(); setLocalZOrder(100); Menu* MenuItemImage* [](Ref*){ Director::getInstance()->popScene(); }); menu->addChild(item); item->setPosition(winSize.width item->getBoundingBox().size.height addChild(menu); return } |
4.编写T06Box2D.h |
#ifndef #define #include #include class { public: CREATE_FUNC(T06Box2D); bool b2World* b2Body* void }; #endif |
5编写:T06Box2D.cpp |
#include #define bool { Layer::init(); //创建世界,后面的-9.8表示向下的重力加速度为9.8 //b2Vec2 gravity(0,-9.8f); //这个表示没有重力加速度 b2Vec2 _world = { b2BodyDef //这里是一个动态的body,默认是静态的body def.type //设置位置。要转换成重力场中的位置要除以PTM_RATIO def.position.Set(winSize.width b2Body* //让body受力 body->SetLinearVelocity(b2Vec2(10,20)); //显示body的精灵 Sprite* addChild(sprite); sprite->setPosition(body->GetPosition().x*PTM_RATIO, //设置body的形状,让它和sprite相一致,是圆形的 b2CircleShape //设置半径 shape.m_radius //后面的一个參数表示的是密度系数 b2Fixture* //设置摩擦系统 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); //关联body和精灵 body->SetUserData(sprite); } //加个地板 { b2BodyDef // def.position.Set(0, 0); b2Body* //设置边界类型的形状 b2EdgeShape //设置地板的開始点和结束点 shape.Set(b2Vec2(0, b2Fixture* //设置摩擦系数 fixture->SetFriction(0.0f); //设置弹性系数 fixture->SetRestitution(1.0f); } //加个天花板 { b2BodyDef def.position.Set(0, b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* //摩擦系统 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); } //左挡板 { b2BodyDef //def.position.Set(0, winSize.height / PTM_RATIO); b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* fixture->SetFriction(0.0f); fixture->SetRestitution(1.0f); } //右挡板 { b2BodyDef def.position.Set(winSize.width b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* //摩擦系数 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); } //球拍 { b2BodyDef def.position.Set(winSize.width b2Body* _bat = Sprite* body->SetUserData(sprite); addChild(sprite); sprite->setPosition(body->GetPosition().x*PTM_RATIO, Size Size sprite->setScale(batSize.width b2PolygonShape shape.SetAsBox(batSize.width b2Fixture* //摩擦系统 fixture->SetFriction(0.0f); //弹性系统 fixture->SetRestitution(1.0f); //touch EventListenerTouchOneByOne* ev->onTouchBegan ev->onTouchMoved float b2Vec2 pos.x //以下的函数等价于setPosition() _bat->SetTransform(pos, }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } scheduleUpdate(); return } void { //时间在流逝 _world->Step(dt, //遍历这个世界的body b2Body* while (body) { //设置body相关的精灵的位置 Sprite* if (sprite) { sprite->setPosition(body->GetPosition().x*PTM_RATIO, sprite->setRotation(body->GetAngle()*180.0 } body = } } |
6.编写TMenu.h |
#ifndef #define #include class { public: CREATE_FUNC(TMenu); bool bool }; #endif |
7. |
#include #include #include #include #include #include #include static "T01CPP11", "T02Vector", "T03Map", "T04Label", "T06Box2D" }; bool { Layer::init(); Menu* addChild(menu); for (int { MenuItemFont* MenuItem* int Layer* if (title[i] if (title[i] if (title[i] if (title[i] if (title[i] if (l) { TBack* Scene* s->addChild(b); s->addChild(l); Director::getInstance()->pushScene(s); } }); menu->addChild(item); item->setTag(1000 } menu->alignItemsVertically(); // auto #if 0 ev->onTouchBegan return }; #endif //ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2); ev->onTouchBegan ev->onTouchMoved setPositionY(getPositionY() }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, return } bool { return } |
8.编写AppDelegate.cpp |
#include #include #include USING_NS_CC; AppDelegate::AppDelegate() } AppDelegate::~AppDelegate() { } bool // initialize director auto auto if(!glview) glview = glview->setFrameSize(480, director->setOpenGLView(glview); } glview->setDesignResolutionSize(480, // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 // create a scene. it's an autorelease object auto scene->addChild(TMenu::create()); scene->addChild(TBack::create()); // run director->runWithScene(scene); return } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again void Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } |
执行效果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""> |
版权声明:本文博主原创文章,博客,未经同意不得转载。
2.Cocos2dx 3.2重力系统Box2D的更多相关文章
- 2.Cocos2dx 3.2中的重力系统Box2D
1 添加Box2D相关的库 步骤1:右击项目所在的解决方案à添加->现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D ...
- Cocos2d-x v3.1 GUI系统--环境构建(七)
Cocos2d-x v3.1 GUI系统--环境构建(七) 在使用Cocos2d-x的GUI系统时,由于生成的工程默认是没有将GUI系统所需的库导入到项目的,所以我们必须把库导入到工程中并对工程做一些 ...
- 【极客学院出品】Cocos2d-X系列课程之九-BOX2D物理引擎
Cocos2d-x 是时下最热门的手游引擎,在国内和国外手机游戏开发使用的份额各自是70%和25%,在App Store的top10中,有7个是用它开发的. 本节课程为Cocos2d-x系列课程之九, ...
- Cocos2d-x中由sprite来驱动Box2D的body运动(用来制作平台游戏中多变的机关)
好久都没写文章了,就来一篇吧.这种方法是在制作<胖鸟大冒险>时用到的.<胖鸟大冒险>中使用Box2D来进行物理模拟和碰撞检測,因此对每一个机关须要创建一个b2body.然后&l ...
- cocos2d-x JS 获取当前系统时间(解决屏幕双击点击事件)
记录一下,好开心,感觉今天自己又学到东西了,对于屏幕双击事件本来还毫无头绪的,今天得以解决总算没白费加班,其实原理很简单:就是在点击事件里做一个判断,这个判断就是需要获取当前系统的时间的毫秒差,第一次 ...
- cocos2dx 3.1获取系统当前时间
std::string Tools::getcurrTime() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATF ...
- Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo
本节为大家提供有关物理游戏的知识,讲解了一个简单的圆形自由落体Demo的编写.. Java代码 package com.himi; import java.util.Random; import ja ...
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...
- [原创]cocos2d-x研习录-第三阶 特性之物理引擎
游戏物理引擎是指在游戏中涉及物理现象的逻辑处理,它用于模拟现实世界的各种物理规律(如赛车碰撞.子弹飞行.物体掉落等),让玩家能够在游戏中有真实的体验. Cocos2D-x中支持Box2D和Chipmu ...
随机推荐
- Unity3D游戏开发从零单排(四) - 制作一个iOS游戏
提要 此篇是一个国外教程的翻译,尽管有点老,可是适合新手入门. 自己去写代码.debug,布置场景,能够收获到非常多.游戏邦上已经有前面两部分的译文,这里翻译的是游戏的最后一个部分. 欢迎回来 在第一 ...
- Android之场景桌面(一)
声明:转载请务必注明出处,本文代码和主题仅供学习交流,请勿用于商业用途. 引言:最近Android场景桌面开始流行起来了,跟原始的Android桌面相比,场景桌面能逼真的模拟各种自然物体,并且通过点击 ...
- hdu2222Keywords Search (特里)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- Lucene 实例教程(四)之检索方法总结
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)
如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...
- JSTL 中<c:forEach>使用
<c:forEach 详解 博客分类: JSTL <c:forEach>标签用于通用数据循环,它有以下属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 ...
- 【原创】leetCodeOj --- Sliding Window Maximum 解题报告
天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...
- Oracle 阅读器-刚看完表空间回复的详细解释
(一) 当使用一个控制文件的备份恢复,例如下面的附图.使用备份控制文件恢复位置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGVtb25zb24=/fo ...
- hdu-2814-Interesting Fibonacci-斐波那契周期节
哇,其实我2A该....否1A纯脑损伤.. 乞讨:F(a^b)^(F(a^b) ^ (n-1))%c 既是求F(a^b)^(F(a^b) ^ (n-1)%phi[c]+phi[c])%c 先求x=F ...
- hadoop-ha组态
HADOOP HA组态 hadoop2.x的ha组态.这份文件是在那里的描述中hdfs与yarn的ha组态. 这份文件的假设是zk它已被安装并配置,事实上,任何安装. hdfs ha组态 首先.配置c ...