Cocos2d-x加速度计实例:运动的小球

来源:互联网 发布:ubuntu cache 编辑:程序博客网 时间:2024/05/16 17:55

下面我们通过一个实例介绍一下如果通过层加速度计事件实现访问加速度计。该实例场景如下图所示,场景中有一个小球,当我们把移动设备水平放置,屏幕向上,然后左右晃动移动设备来改变小球的位置。

 

下面我们再看看具体的程序代码,首先看一下HelloWorldScene.h文件,它的代码如下:

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #define kBall_Tag                 100                                                                                                          ①#define SPEED                      30.0                                                                                                        ② class HelloWorld : public cocos2d::Layer{public:   static cocos2d::Scene* createScene();   virtual bool init();                   virtual void onEnter();         virtual void onExit();            virtual voidonAcceleration(cocos2d::Acceleration* acc, cocos2d::Event *unused_event);         ③   CREATE_FUNC(HelloWorld);}; #endif // __HELLOWORLD_SCENE_H__

上述代码第①行定义宏kBall_Tag,它是小球精灵的标签Tag数值。第②行定义宏SPEED,它表示小球运动的速度。第③行代码声明了onAcceleration函数,。

HelloWorldScene.cpp文件,它的主要代码如下:

bool HelloWorld::init(){    if( !Layer::init() )    {         returnfalse;    }     SizevisibleSize = Director::getInstance()->getVisibleSize();    Pointorigin = Director::getInstance()->getVisibleOrigin();     //贴图的纹理图片宽高必须是2的n次幂,128x128    autobg = Sprite::create("BackgroundTile.png",                                       Rect(0,0, visibleSize.width, visibleSize.height));    //贴图的纹理参数,水平重复平铺,垂直重复平铺    Texture2D::TexParamstp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};    bg->getTexture()->setTexParameters(tp);    bg->setPosition(origin+ Point(visibleSize.width/2, visibleSize.height/2));    addChild(bg,0);     autoball = Sprite::create("Ball.png");    ball->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2));    addChild(ball,10, kBall_Tag);     returntrue;} void HelloWorld::onEnter(){    Layer::onEnter();    log("HelloWorldonEnter");     setAccelerometerEnabled(true);                                                                                                      ①} void HelloWorld::onAcceleration(Acceleration*acc, Event *unused_event){   log("{x = %f, y = %f}", acc->x,acc->y);   Size visibleSize = Director::getInstance()->getVisibleSize();   Sprite* ball = (Sprite*)this->getChildByTag(kBall_Tag);                                                  ②   Size s = ball->getContentSize();                                                                                                    ③   Point p0 = ball->getPosition();                                                                                                      ④    float p1x =  p0.x + acc->x *SPEED ;                                                                                  ⑤   if ((p1x - s.width/2) <0) {                                                                                                       ⑥       p1x = s.width/2;                                                                                                               ⑦   }   if ((p1x + s.width / 2) > visibleSize.width) {                                                                                   ⑧       p1x = visibleSize.width - s.width / 2;                                                                             ⑨   }    float p1y =  p0.y + acc->y *SPEED ;   p1y = p1y < 0 ? -p1y : p1y;   if ((p1y - s.height/2) < 0) {       p1y = s.height/2;   }   if ((p1y + s.height/2) > visibleSize.height) {       p1y = visibleSize.height - s.height/2;   }   ball->runAction(Place::create(Point( p1x, p1y)));                                                                       ⑩} void HelloWorld::onExit(){    Layer::onExit();    log("HelloWorldonExit");}

上述代码①行开启加速计设备,这个代码是在HelloWorld::onEnter()函数中,意味着在进入层的时候就开启加速度计设备。

在第②行代码是通过标签属性获得小球精灵对象。第③行代码ball->getContentSize()获得小球尺寸大小。第④行代码ball->getPosition()是获得小球的位置。第⑤行代码是p0.x + acc->x * SPEED是获得小球的x轴方向移动的位置,但是需要考虑左右超出屏幕的情况,第⑥行代码是 (p1x - s.width/2) <0是判断超出左边屏幕,这种情况下我们需要通过第⑦行代码p1x = s.width/2重新设置它的x轴坐标。第⑧行代码(p1x+ s.width / 2) > visibleSize.width是判断超出右边屏幕,这种情况下我们需要通过第⑨行代码p1x = visibleSize.width - s.width / 2重新设置它的x轴坐标。类似的判断y轴也需要,代码就不再解释了。

重新获得小球的坐标位置后,通过第⑩行代码ball->runAction(Place::create(Point( p1x, p1y)))是执行一个动作使小球移动到新的位置。


更多内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
本书交流讨论网站:http://www.cocoagame.net
更多精彩视频课程请关注智捷课堂Cocos课程:http://v.51work6.com
欢迎加入Cocos2d-x技术讨论群:257760386


《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:

京东:http://item.jd.com/11584534.html

亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当:http://product.dangdang.com/23606265.html

互动出版网:http://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源码及样章下载地址:

源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台

0 0