Cocos2d-x之testCpp---Accelerometer(重力感应)分析

本文详细介绍了在Cocos2d-X中使用重力感应实现对象移动的过程,包括如何启用重力感应、处理重力变化、解决对象边界问题等,并解答了关于对象创建和内存管理的疑惑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 决定好好总结下testCpp里面的知识,这里我把testCpp中AccelerometerTest剥离出来了,总结下~

我用的最新版的2dx。。。。cocos2d-2.1rc0-x-2.1.3  

ndk是android-ndk-r8d

class文件下载地址:http://download.youkuaiyun.com/detail/bill_ming/5568563

#include "HelloWorldScene.h"
#include "cocos2d.h"


#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;
}
std::string HelloWorld::titile()
{
	return "hehehaha~";
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
		//打开重力感应
		setAccelerometerEnabled(true);

		CCLabelTTF *label = CCLabelTTF::create(titile().c_str(),"Arial",32);
        addChild(label,1);
	    label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );
		
		m_pBall = CCSprite::create("ball.png");
		//m_pBall->setPosition(ccp(winSize.height/2,winSize.width/2));
		m_pBall->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y));
		addChild(m_pBall);
		/************************************************************************/
		/* 必须要调用了对象的autoRelease函数之后,retain和release函数才会生效,
		   否则,一切都是徒劳。create已经包含了autoRelease
	   	    注意:addChild函数已经包含了retain                                                                     
			参考:http://www.xue5.com/Mobile/Mobile/680881.html
	   */
		/************************************************************************/
            
        bRet = true;
    } while (0);

    return bRet;
}
void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)
{
	CCDirector *pDir = CCDirector::sharedDirector();
	if ( m_pBall == NULL)
	{
		return;
	}
	CCSize ballSize = m_pBall->getContentSize();
	CCPoint ptNow = m_pBall->getPosition();
	CCPoint ptTemp = pDir->convertToUI(ptNow);

	ptTemp.x += pAccelerationValue->x * 9.81f;
	ptTemp.y -= pAccelerationValue->y * 9.81f;

	CCPoint ptNext = pDir->convertToGL(ptTemp);
	FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
	FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
	m_pBall->setPosition(ptNext);
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}


1.先调用CCLayer的方法setAccelerometerEnabled(true);启用重力感应

2.在重力方向改变时,会调用CCLayer的didAccelerate(CCAcceleration *pAccelerationValue),所以自己的HelloWorld类继承了CCLayer,这里重写这个方法

问题总结:

1.原来testCpp里实在controller.ccp中在CreateTestScene函数中调用的,他用的是

new AccelerometerTestScene();进入到AccelerometerTest.cpp,我们会发现,它还是用CCLayer* pLayer = new AccelerometerTest();而不是create方法,这两种有什么区别呢?我们知道当我们调用create()方法时,会调用init来初始化,但直接用new AAA();这种方式时,会调用onEnter()而不会是init...可以参考下:http://blog.youkuaiyun.com/bill_ming/article/details/9080271


2.testCpp里,它的onEnter()函数中,创建ball精灵后有一句:m_pBall->retain();

但我记得调用addChild(m_pBall);这个函数后,就不必要再retain()了啊。。。可以参考下:

笨木头的一篇博文:http://www.xue5.com/Mobile/Mobile/680881.html

希望看到的朋友能稍微解释下哈,是不是我理解错了。。。感谢感谢~~~


3.或许很多朋友也和我一样,被各种getWinSize(),getContentSize(),getVisibleSize(),以及一些坐标转换搞混了、、、

可以参考下我总结的另一篇文章:http://blog.youkuaiyun.com/bill_ming/article/details/9078781

它根据重力移动的过程:

 (1.取得小球的逻辑尺寸:为了在后边移动时修正小球位置,不让它跑出边界

 (2.取得当前位置,然后转化为屏幕坐标,然后根据当前位置以及加速度(9.81)计算出下一处的坐标。

 (3.把坐标系转换为openGl坐标,然后根据小球逻辑尺寸,修正位置,最后setPosition就好了


4.但还有一个问题,小球可以根据重力移动,但如何设定加速减速的速度大小呢?

可以参考下:http://blog.youkuaiyun.com/jinglijun/article/details/8048013

这里我没用到,希望看到的朋友推荐一些资料~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值