Hi, I am new to cocos2dx……i want to use accelerometer in my game on iOS……but i am unable to did it….
is there any link which can guide it to do……please help me
can anybody help me…
Thanks in advance
Hi, I am new to cocos2dx……i want to use accelerometer in my game on iOS……but i am unable to did it….
is there any link which can guide it to do……please help me
can anybody help me…
Thanks in advance
In .h file the declaration will be :
//For Accelerometer———————————————
virtual void didAccelerate(cocos2d::CCAcceleration* pAccelerationValue);
In .m file the implementation method will be :
In init()
setIsAccelerometerEnabled(true); //If using cocos2d-x ver 2.0.1 or earlier…
setAccelerometerEnabled(true); //If using cocos2d-x ver 2.0.2 or later…
//For Accelerometer————————————————-
void GameplayLayer::didAccelerate(cocos2d::CCAcceleration* pAccelerationValue)
{
pAccelerationValue~~>x; //horizontal acceleration
pAccelerationValue~~>y; //vertical acceleration
//If you want to move sprite then can use it to change the position…
sprite~~>setPosition.x+pAccelerationValue~~>x*3,sprite->getPosition().y)); //I have not tested it but to move sprite horizontally with some speed like x*6…you can change it as accordingly…
}
Hey Viraj, thanks for the reply,
Actually i have trying to run my game on iPad….i have written the code also but *didAccelerate(CCAcceleration* pAccelerationValue)* this method is not getting called……
I am not understanding that where is the issue…
thanks , waiting for your reply
check for the enable method of accelerometer in init mentod in .m file…
setIsAccelerometerEnabled(true); //If using cocos2d-x ver 2.0.1 or earlier…
setAccelerometerEnabled(true); //If using cocos2d-x ver 2.0.2 or later…
Hey Viraj,
its working…but the problem is that its not working when i accelrate my iPAD….
m not getting why….
Thankx in advance
If you post your didAccelerate code then i can check the problem.
Hey Viraj,
Here is my onEnter() method and didAccelerate()……Thanks in advance
void HelloWorld::onEnter()
{
accX=1;
accY=1;
timer=0;
level=0;
CCLayer::onEnter();
setIsAccelerometerEnabled(true);
this~~>addGraphics;
}
void HelloWorld::didAccelerate
{
// double rollingX ;
pAccelerationValue~~>x=pAccelerationValue>y;
CCLOG (“didAccelerate->>>”);
player_sprite->setPosition(ccp(pAccelerationValue~~>x+5,player_sprite~~>getPosition().y));
}
If you take logs of the position values coming from your sprite then you can find the problem…
Your x position values are too small…
So try with this…
player_sprite~~>setPosition.x+pAccelerationValue~~>x*6,player_sprite~~>getPosition.y));
And pAccelerationValue~~>x*6 not pAccelerationValue~~>x+6
You can set some initial position of sprite for testing like this
At creation of sprite
player_sprite~~>setPosition(ccp(100,100));
And if still you don’t get it work then i prefer first you make yourself more comfortable with the framework…
And go to this link as starting point:
http://www.raywenderlich.com/11338/cocos2d-x-for-ios-and-android-space-game
Hey Viraj, i have tried this……it works awesomly….thanks a lot…
Hey Viraj, i am facing an issue regarding accelerometer.
This is my code
void GameScene::onAcceleration(cocos2d::CCAcceleration* pAccelerationValue, cocos2d::Event* event)
{
pAccelerationValue~~>x;
clown~~>setPosition(Point ((clown~~>getPosition.x+),clown~~>getPosition().y));
}
It is not working, it goes left, right, i think depending on its mood. or i don’t know how to grip acceleration values
I am new here. please help