【2014-01-21】cocos2dx学习笔记:陀螺仪的使用
2026/7/16 5:44:43 网站建设 项目流程

[历史归档]本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。
发布时间:2014-01-21| 标题:cocos2dx学习笔记:陀螺仪的使用分类:编程 / C && C++ / cocos2dx |标签:cocos2dx·陀螺仪


cocos2dx学习笔记:陀螺仪的使用

    • 陀螺仪的使用:
      • 备注:

最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo,忙了半个星期,一边学习一边开发,总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。

陀螺仪的使用:

demo的核心就是陀螺仪的使用,怎么用,这里就不说了,楼主语文不好,大家可以参考另一款APP【 疯狂射手 】,我的DEMO就是仿照它的。

整理后的代码如下:

++.h文件++#ifndef__MOTION_CONTROL_H__#define__MOTION_CONTROL_H__#include"cocos2d.h"classMotionControl{public:virtualboolinit(void);voidrelease();voidsetbaseMotion(void);voidupdateMotion(void);floatgetYawOffset(void){returnm_fYawOffset;}floatgetRollOffset(void){returnm_fRollOffset;}floatgetPitchOffset(void){returnm_fPitchOffset;}private:void*getCurrAttitude();void*m_pMotionManger;boolm_bAvailable;void*m_BaseAttitude;floatm_fYaw;floatm_fRoll;floatm_fPitch;floatm_fLastYaw;floatm_fLastRoll;floatm_fLastPitch;floatm_fYawOffset;floatm_fRollOffset;floatm_fPitchOffset;boolm_bIsMotionUpdate;};#endif//___MOTION_CONTROL_H__++.m文件++#include"MotionControl.h"#include<CoreMotion/CoreMotion.h>#import<CoreFoundation/CoreFoundation.h>USING_NS_CC;boolMotionControl::init(void){CMMotionManager*motionManager=[[CMMotionManager alloc]init];motionManager.deviceMotionUpdateInterval=1.0/60.f;if(motionManager.isDeviceMotionAvailable){;}m_BaseAttitude=NULL;m_pMotionManger=(void*)motionManager;setbaseMotion();returntrue;}voidMotionControl::release(){if(m_pMotionManger!=NULL){CMMotionManager*tmpMotion=(CMMotionManager*)m_pMotionManger;;m_pMotionManger=NULL;}if(m_BaseAttitude!=NULL){CMAttitude*tmpAttitude=(CMAttitude*)m_BaseAttitude;;m_BaseAttitude=NULL;}}void*MotionControl::getCurrAttitude(){CMMotionManager*temMotion=(CMMotionManager*)m_pMotionManger;CMDeviceMotion*currentDeviceMotion=temMotion.deviceMotion;CMAttitude*currentAttitude=currentDeviceMotion.attitude;returncurrentAttitude;}voidMotionControl::setbaseMotion(void){if(m_BaseAttitude!=NULL){CMAttitude*tmpAttitude=(CMAttitude*)m_BaseAttitude;;m_BaseAttitude=NULL;}CMAttitude*currentAttitude=(CMAttitude*)getCurrAttitude();m_BaseAttitude=;m_bIsMotionUpdate=false;m_fYawOffset=0;m_fRollOffset=0;m_fPitchOffset=0;}voidMotionControl::updateMotion(void){if(NULL==m_BaseAttitude){setbaseMotion();}CMAttitude*currentAttitude=(CMAttitude*)getCurrAttitude();;m_fYaw=CC_RADIANS_TO_DEGREES(currentAttitude.yaw);m_fRoll=CC_RADIANS_TO_DEGREES(currentAttitude.roll);m_fPitch=CC_RADIANS_TO_DEGREES(currentAttitude.pitch);if(!m_bIsMotionUpdate){m_bIsMotionUpdate=true;m_fLastYaw=m_fYaw;m_fLastRoll=m_fRoll;m_fLastPitch=m_fPitch;}m_fYawOffset=m_fYaw-m_fLastYaw;m_fRollOffset=m_fRoll-m_fLastRoll;m_fPitchOffset=m_fPitch-m_fLastPitch;m_fLastYaw=m_fYaw;m_fLastRoll=m_fRoll;m_fLastPitch=m_fPitch;}

备注:

作者当时比较赶项目,结果欲速而不达了,在网上搜索的各种代码均不靠谱,反而浪费了时间,最后还是老老实实的看了下apple的文档,结果发现很简单。这里备份下几个网址,以备不时之需。

【 http://blog.csdn.net/aiyongyyy/article/details/7986810http://www.cnblogs.com/binbingg/p/3442624.html 】

【 http://blog.csdn.net/cocos2der/article/details/7030384 】

【 http://blog.sina.com.cn/s/blog_7b9d64af0101cu4p.html 】

apple的文档:

【 https://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMAttitude_Class/Reference/Reference.html 】

【 https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html#//apple_ref/doc/uid/TP40009541-CH6-SW1 】

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询