閱讀535 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Cocos2d-x加速度計實例:運動的小球

下麵我們通過一個實例介紹一下如果通過層加速度計事件實現訪問加速度計。該實例場景如下圖所示場景中有一個小球當我們把移動設備水平放置屏幕向上然後左右晃動移動設備來改變小球的位置。

 

下麵我們再看看具體的程序代碼首先看一下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++卷》
本書交流討論網站https://www.cocoagame.net
更多精彩視頻課程請關注智捷課堂Cocos課程https://v.51work6.com
歡迎加入Cocos2d-x技術討論群257760386


《Cocos2d-x實戰 C++卷》現已上線各大商店均已開售

京東https://item.jd.com/11584534.html

亞馬遜https://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

當當https://product.dangdang.com/23606265.html

互動出版網https://product.china-pub.com/3770734

《Cocos2d-x實戰 C++卷》源碼及樣章下載地址

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

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

歡迎關注智捷iOS課堂微信公共平台

最後更新:2017-04-03 05:39:33

  上一篇:go 仿刮刮樂刮獎效果
  下一篇:go Android Manifest.xml文件解析