2012-04-19 14:35:52 +08:00
|
|
|
#include "AccelerometerTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
#include "cocos2d.h"
|
|
|
|
|
|
|
|
#define FIX_POS(_pos, _min, _max) \
|
|
|
|
if (_pos < _min) \
|
|
|
|
_pos = _min; \
|
|
|
|
else if (_pos > _max) \
|
|
|
|
_pos = _max; \
|
|
|
|
|
2010-11-10 17:47:06 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AccelerometerTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2012-04-19 14:35:52 +08:00
|
|
|
AccelerometerTest::AccelerometerTest(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _lastTime(0.0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AccelerometerTest::~AccelerometerTest(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-10 17:47:06 +08:00
|
|
|
std::string AccelerometerTest::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "AccelerometerTest";
|
2010-11-10 17:47:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccelerometerTest::onEnter()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::onEnter();
|
2010-11-10 17:47:06 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setAccelerometerEnabled(true);
|
2010-11-10 17:47:06 +08:00
|
|
|
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create(title().c_str(), "Arial", 32);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 1);
|
2013-07-12 14:11:55 +08:00
|
|
|
label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y-50) );
|
2010-11-10 17:47:06 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_ball = Sprite::create("Images/ball.png");
|
2013-07-12 14:11:55 +08:00
|
|
|
_ball->setPosition(Point(VisibleRect::center().x, VisibleRect::center().y));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_ball);
|
2010-11-10 17:47:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_ball->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void AccelerometerTest::onAcceleration(Acceleration* acc, Event* event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// double fNow = pAccelerationValue->timestamp;
|
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
// if (_lastTime > 0.0)
|
2012-04-19 14:35:52 +08:00
|
|
|
// {
|
2013-08-16 16:05:27 +08:00
|
|
|
// auto ptNow = convertToUI
|
2012-04-19 14:35:52 +08:00
|
|
|
// }
|
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
// _lastTime = fNow;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pDir = Director::getInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
/*FIXME: Testing on the Nexus S sometimes _ball is NULL */
|
|
|
|
if ( _ball == NULL ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto ballSize = _ball->getContentSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-19 00:04:50 +08:00
|
|
|
auto ptNow = _ball->getPosition();
|
|
|
|
auto ptTemp = pDir->convertToUI(ptNow);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-19 00:04:50 +08:00
|
|
|
ptTemp.x -= acc->x * 9.81f;
|
|
|
|
ptTemp.y -= acc->y * 9.81f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-19 00:04:50 +08:00
|
|
|
auto 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));
|
|
|
|
_ball->setPosition(ptNext);
|
2010-11-10 17:47:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AccelerometerTestScene
|
|
|
|
//
|
2012-04-19 14:35:52 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
void AccelerometerTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = new AccelerometerTest();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
|
|
|
layer->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|