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 ...
随机推荐
- [SHOI 2011]双倍回文
Description 题库链接 记一个字符串为 \(X\) ,它的倒置为 \(X^R\) .现在给你一个长度为 \(n\) 的字符串 \(S\) ,询问其最长的形同 \(XX^RXX^R\) 的子串 ...
- [NOIp 2009]靶形数独
Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他 ...
- [HNOI 2009]有趣的数列
Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...
- SAC E#1 - 一道难题 Tree
题目背景 冴月麟和魏潇承是好朋友. 题目描述 冴月麟为了守护幻想乡,而制造了幻想乡的倒影,将真实的幻想乡封印了.任何人都无法进入真实的幻想乡了,但是她给前来救她的魏潇承留了一个线索. 她设置了一棵树( ...
- [BZOJ]3672 购票(Noi2014)
革命尚未成功,同志还需努力. Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ ...
- bzoj1934
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 2406 Solved: 1498[Submit][ ...
- Linux中LCD设备驱动-framebuffer(帧缓冲)【】
转自:https://blog.csdn.net/linux_devices_driver/article/details/7079442 1.framebuffer 帧缓冲 帧缓冲(fram ...
- python txt文件的写入和读取
1.文件的打开 使用open () 函数 打开文件.他有两个参数,文件路径或文件名和文件的打开方式. "r" 只读模式,不能编辑和删除文件内容. "w" 写入模 ...
- VS2012不能加载想要打开的项目/解决方案
今天回宿舍用自己的电脑敲代码,想要打开之前的项目,可是VS2012打开之后项目却显示“无法加载” 查了之后才知道原来是由于某个安装包缺少引起的,具体做法请看如下 链接:http://jingyan.b ...
- selenium常用内容
一.声明浏览器对象 注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入 from selenium import webdriver #webdriver可以认为是浏览器的 ...