cocos2d-x 重力感应
本文没你想象的那么,,复杂。事实上就是通过重力感应控制个小球移动而已。
先看头文件:
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- USING_NS_CC;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- HelloWorld(void);
- ~HelloWorld(void);
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
- // there's no 'id' in cpp, so we recommand to return the exactly class pointer
- static cocos2d::CCScene* scene();
- // a selector callback
- void menuCloseCallback(CCObject* pSender);
- virtual void didAccelerate(CCAcceleration* pAccelerationValue);
- // implement the "static node()" method manually
- CREATE_FUNC(HelloWorld);
- protected:
- CCSprite* m_pBall;
- double m_fLastTime;
- };
- #endif // __HELLOWORLD_SCENE_H__
看.cpp
- #include "HelloWorldScene.h"
- using namespace cocos2d;
- #define FIX_POS(_pos, _min, _max) \
- if (_pos < _min) \
- _pos = _min; \
- else if (_pos > _max) \
- _pos = _max; \
- HelloWorld::HelloWorld()
- : m_fLastTime(0.0)
- {
- }
- HelloWorld::~HelloWorld()
- {
- m_pBall->release();
- }
- CCScene* HelloWorld::scene()
- {
- CCScene * scene = NULL;
- do
- {
- // 'scene' is an autorelease object
- scene = CCScene::create();
- CC_BREAK_IF(! scene);
- // 'layer' is an autorelease object
- HelloWorld *layer = HelloWorld::create();
- CC_BREAK_IF(! layer);
- // add layer as a child to scene
- scene->addChild(layer);
- } while (0);
- // return the scene
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
- CC_BREAK_IF(! CCLayer::init());
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,
- menu_selector(HelloWorld::menuCloseCallback));
- CC_BREAK_IF(! pCloseItem);
- // Place the menu item bottom-right conner.
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
- // Create a menu with the "close" menu item, it's an auto release object.
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- CC_BREAK_IF(! pMenu);
- // Add the menu to HelloWorld layer as a child layer.
- this->addChild(pMenu, 1);
- //add Accelerometer
- CSize size = CCDirector::sharedDirector()->getWinSize();
- setAccelerometerEnabled(true);//打开重力感应
- m_pBall = CCSprite::create("ball.png");
- m_pBall->setPosition(ccp(size.width/2, size.height/2));
- addChild(m_pBall);
- m_pBall->retain();
- bRet = true;
- } while (0);
- return bRet;
- }
- <pre name="code" class="cpp">void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- // "close" menu item clicked
- CCDirector::sharedDirector()->end();
- }
- void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)
- {
- // double fNow = pAccelerationValue->timestamp;
- //
- // if (m_fLastTime > 0.0)
- // {
- // CCPoint ptNow = convertToUI
- // }
- //
- // m_fLastTime = fNow;
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCDirector* pDir = CCDirector::sharedDirector();
- /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */
- if ( m_pBall == NULL ) {
- return;
- }
- CCSize ballSize = m_pBall->getContentSize();
- CCPoint ptNow = m_pBall->getPosition();
- CCPoint ptTemp = pDir->convertToUI(ptNow);
- //9.8 重力加速度
- ptTemp.x += pAccelerationValue->x * 9.81f;
- ptTemp.y -= pAccelerationValue->y * 9.81f;
- CCPoint ptNext = pDir->convertToGL(ptTemp);
- FIX_POS(ptNext.x, (0+ballSize.width / 2.0), (size.width - ballSize.width / 2.0));
- FIX_POS(ptNext.y, (0+ballSize.height / 2.0), (size.height - ballSize.height / 2.0));
- m_pBall->setPosition(ptNext);
- }</pre>
- <p></p>
- <pre></pre>
cocos2d-x 重力感应的更多相关文章
- cocos2dx 3.x(让精灵随着重力感应的方向移动而移动)
// // GameScene.hpp // HelloWord // // Created by apple on 2017/1/7. // // #ifndef GameScene_hpp #de ...
- Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)
这节我们来学习Cocos2d-x的最后一节.怎样处理重力感应事件.移植到Android后加入再按一次返回键退出游戏等.我这里用的Android.IOS不会也没设备呃 效果图不好弄,由于是要移植到真机上 ...
- H5案例分享:html5重力感应事件
html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...
- 【Android开发学习笔记】【第九课】重力感应
概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...
- iOS 重力感应 学习1 陀螺仪 水平仪 指南针
小球可以随着重力感应 四处乱撞. 放大了坐标位移 就可以看见小球动了. 然后规定小球的路径 当滑到中间时候 弹出一张图片 作为提示. 我做了一个小demo 效果不错哦 CMMotionManager ...
- 移动终端学习2:触屏原生js事件及重力感应
如今智能移动设备已经渗透到人们生活的方方面面,用户数量也在不断迅速增长(市场研究机构 eMarketer 在今年初发表的趋势报告中预测,2014年至2018年,中国智能手机用户从总人口的 38.3%增 ...
- Android重力感应开发
http://blog.csdn.net/mad1989/article/details/20848181 一.手机中常用的传感器 在Android2.3 gingerbread系统中,google提 ...
- 基于野火M3开发板(STM32F103VET6)的迷宫小球(重力感应控制)游戏开发
2013-03-03 这是研一上学期<实时嵌入式系统实验>课程的大作业,是利用野火板的资源,加上一个AHRS(Attitude and Heading Reference System,姿 ...
- iOS设备的重力感应
重力感应是每台iOS设备都具备的功能,所以在应用用好重力感应会有意想不到的效果 1.添加CoreMotion框架 2.在需要使用重力感应的类中添加头文件 #import <CoreMotion/ ...
随机推荐
- 基于C++11实现线程池的工作原理
目录 基于C++11实现线程池的工作原理. 简介 线程池的组成 1.线程池管理器 2.工作线程 3.任务接口, 4.任务队列 线程池工作的四种情况. 1.主程序当前没有任务要执行,线程池中的任务队列为 ...
- ARC 101E.Ribbons on Tree(容斥 DP 树形背包)
题目链接 \(Description\) 给定一棵\(n\)个点的树.将这\(n\)个点两两配对,并对每一对点的最短路径染色.求有多少种配对方案使得所有边都至少被染色一次. \(n\leq5000\) ...
- Centos 7 安装 Mysql 5.5 5.6 5.7
环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -a Linu ...
- Git Windows 安装
环境 Windows版本:Windows 7 旗舰版 处理器:Inte i5 系统类型:64 位操作系统 下载 Git Windows https://github.com/git-for-windo ...
- iOS Application Extension
链接: iOS App Extension入门 iOS10通知及通知拓展Extension使用详解(附Demo) iOS10 通知extension之 Content Extension
- php_ssh2操作linux
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/9/15 * Time: 14:11 */ header( ...
- sql ,内连接,外连接,自然连接等各种连接
1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和c ...
- Struts2漏洞拉响网站安全红色警报以及把Struts2更新为最新版本Struts2.3.15.1步骤
360网站安全检测平台今日发布红色警报称,广泛应用在国内大型网站系统的Struts2框架正在遭到黑客猛烈攻击.利用Struts2“命令执行漏洞”,黑客可轻易获得网站服务器ROOT权限.执行任意命令,从 ...
- Morris图表使用小记
挺好用的,碰到几个问题,有的是瞎试解决了的: 1.我想折线图能够响应单击事件,即点击某个节点后,就能加载进一步的信息,帮助没找到,参照另外一个地方的写法,居然支持事件 Morris.Line({ el ...
- Android中Bundle和Intent的区别
Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...