标签:
#include "cocos2d.h" class GameScene : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); //重写的父类的方法,用来处理重力加速器方法 void onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* event); CREATE_FUNC(GameScene); };
//重写重力加速器方法 void GameScene::onAcceleration(Acceleration* acc, Event* event){ auto director = Director::getInstance(); if(img == NULL){ return; } auto imgSize = img->getContentSize(); auto imgPosition = img->getPosition(); auto imgX = img->getPositionX(); auto imgY = img->getPositionY(); imgX += acc->x * gravityValue; //imgY += acc->y * gravityValue; //auto imgTemp = director->convertToUI(imgPosition); //imgTemp.x += acc->x * gravityValue; //imgTemp.y -= acc->y * gravityValue; //auto imgNext = director->convertToGL(imgTemp); FIX_POS(imgX, (origin.x + imgSize.width/2), (origin.x + visibleSize.width - imgSize.width/2)); FIX_POS(imgY, (origin.y + + imgSize.height/2), (origin.y + visibleSize.height - imgSize.height/2)); img->setPosition(Point(imgX, imgY)); log("onAcceleration: acc->x: %f, acc->y: %f, acc->z: %f, imgX: %f, imgY: %f", acc->x, acc->y, acc->z, imgX, imgY); }
cocos2d-x V3.0 呼叫加速度计 Acceleration
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5032861.html