class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
void OnAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event);
};
Implementation:
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Device::setAccelerometerEnabled(true);
auto listener = EventListenerAcceleration::create(
CC_CALLBACK_2(HelloWorld::OnAcceleration, this));
Director::getInstance()->getEventDispatcher()
->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void HelloWorld::OnAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event){
CCLOG("%f",acc->z);
}
No comments:
Post a Comment