#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,布布扣,bubuko.com
cocos2d-x V3.0 调用重力加速器 Acceleration
原文地址:http://blog.csdn.net/lzan13/article/details/32714775