Sending Accelerometer Values to Label

It’s me again… :frowning:

What I’m trying to do now is get the values the accelerometer gives for debug purposes. I also need the scale of its values for some other operations, but I have no idea of it, so I’ve been trying to work a way to get the acceleration values on the x axis into a label, but I’m stumped.

Can anyone of the experts here help me out?

//acceleration event
Device::setAccelerometerEnabled(true);
auto acclistener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(acclistener, this);

void HelloWorld::onAcceleration(Acceleration* acc, Event *unused_event)
{
//get value of x and y…
float x = acc->x;
float y = acc->y;
…
}

Thank you!

But how will I get the variable ‘x’ into a label? Shouldn’t the function return a float? Then I use toString to use it in a label?

use StringUtils…
for example:
std::string str = StringUtils::format("%.2f", acc->x);
label->setString(str);

Okay. I’ll try that out.

Okay, I’m back. You see, what I tried was to are two functions. One with the listener and event dispatcher code, and the other for onAcceleration.

Then I put the string code in the main. But I’m told ‘acc’ is undefined.

I’m guessing that’s cuz it’s out of scope, in the function.

Am I doing any of this wrong?

Should this code be in the main? Because I’m told ‘acc’ is undefined.

I think you should learn C++ at first and then try to use cocos2d.

I have knowledge of the basics of C++, but how much C++ is enough? Cuz I finished a book on C++, and it covered the basics, and the logic. When I ask for help here, it is because I have attempted to fix the issue, AND consulted my brother who is significantly more advanced than I am. In this particular case, I’ve tried A LOT.

I can handle cocos2d-x. Just that some parts get tricky for me, so I’ve got to ask for help if I want to make any progress. And I haven’t previously worked with the accelerometer, so help here is appreciated.

Yes! So I finally got it working! For future reference to anyone interested, it works on a scale of -1 to +1. I can now read the tilt values from the label on the screen. Thanks for the assistance guys!