• cocos2dx 菜鸟进阶篇(二) 重力感应


    本文没你想象的那么,,复杂。其实就是通过重力感应控制个小球移动而已。

    先看头文件:

    #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;
    }
    
    
    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);
    }

    
    
    
  • 相关阅读:
    骨骼动画的原理及在Unity中的使用
    GUI之ScrollView的使用
    Java编程技术之浅析Java容器技术
    Linux系统 Centos7 环境基于Docker部署Rocketmq服务
    Linux系统环境基于Docker搭建Mysql数据库服务实战
    Java编程技术之浅析JVM内存
    Java编程技术之浅析SPI服务发现机制
    Linux系统环境基于Docker搭建系统基础镜像
    Linux Centos7 环境搭建Docker部署Zookeeper分布式集群服务实战
    Linux Centos7 环境基于Docker部署Zookeeper服务搭建实战
  • 原文地址:https://www.cnblogs.com/start530/p/3834367.html
Copyright © 2020-2023  润新知