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(); } |
|
运行效果: |
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 ...
- JAVA中获取当前系统时间及格式转换
JAVA中获取当前系统时间 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- 【腾讯GAD暑期训练营游戏程序开发】游戏中的动画系统作业
游戏中的动画系统作业说明文档 一.实现一个动画状态机:至少包含3组大的状态节点
- cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西。
cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西.
- 解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题
解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题 本文的实践来源是参照了两个帖子完成的: http://dis ...
- cocos2d-x 3.0rc2中读取sqlite文件
cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败.须要复制到可写的路径下 sqlite3* dbFile = NULL; std::string ...
- 配置SecureCRT连接本地虚拟机中的Linux系统
转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...
- Linux内核中的GPIO系统之(3):pin controller driver代码分析
一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...
随机推荐
- C++11 作用域内枚举
enum class MyEnum{ P1 = , P2, P3, P4, P5 }; MyEnum myEnum = MyEnum::P2; 使用作用域的方式获取并限定P2的值.之所以要使用作用域, ...
- Spring Cloud Eureka 自我保护机制
Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果 ...
- BZOJ 4372 烁烁的游戏
Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠.题意:给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠.烁烁他每次会跳到一个节点u,把周围与他距离不超过d的节点各吸引出w只皮皮 ...
- bzoj 3214: [Zjoi2013]丽洁体
Description 平时的练习和考试中,我们经常会碰上这样的题:命题人给出一个例句,要我们类比着写句子.这种往往被称为仿 写的题,不单单出现在小学生的考试中,也有时会出现在中考中.许多同学都喜欢做 ...
- 【BZOJ2241】【Sdoi2011R1D1】打地鼠
原题传送门 Description 打地鼠是这样的一个游戏:地面上有一些地鼠洞,地鼠们会不时从洞里探出头来很短时间后又缩回洞中.玩家的目标是在地鼠伸出头时,用锤子砸其头部,砸到的地鼠越多分数也就越高. ...
- Codeforces278E Tourists
来自FallDream的博客,未经允许,请勿转载,谢谢. 给定一张无向图,有点权,要支持单点修改点权和询问从一个点到另一个点不重复经过节点的路径上点权最小值的最小值. n,m<=10^5 考虑求 ...
- 华科机考:IP地址
时间限制:1秒 空间限制:32768K 题目描述 输入一个ip地址串,判断是否合法. 输入描述: 输入的第一行包括一个整数n(1<=n<=500),代表下面会出现的IP地址的个数. 接下 ...
- python中str常用操作
1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + str2) >> ...
- Python中模块之xml的讲解
xml模块的功能介绍 这里主要讲解xml模块下的etree.ElementTree类. 1. 创建 具体代码如下 import xml.etree.ElementTree as XM namelist ...
- HashMap实现原理和源码解析
哈希表(hash table)也叫散列表,是一种非常重要的数据结构.许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,本文会对java集合框架中的对应实现HashMap的 ...