Next Generation Emulation banner
1 - 5 of 5 Posts

· Thiccachu (Chubbychu AKA Pikachu-bby/Pikachu-BBW)
Joined
·
4,410 Posts
Discussion Starter · #1 ·
Hey guys, wuddup?

This is my first time posting in this section. I've gotten quite serious with my C++ towards Game Programming.

Anyway, I've been having a problem with some code using cocos2d-x, but I'm sure anyone with a grasp of C++ could assist me here.

My objective is simple. I want to pass accelerometer values to a text label. For debug purpose you could say.

I have a function for obtaining the values. Something like:

Void onAcceleration (cocos2d::Acceleration* acc, Event* event)
[
Float x = acc->x;
]

That's basically it.

Now in a text function in the main(), I want to pass that x variable to the label, so I use StringUtils::toString(or format, any one).

The problem now is this. That x value is out of scope. Can't access it.

If I change the function to a float, and return a float variable, and try and equate a new variable to the returned 'x' of the function, I can get quite a few errors.

If i use the function without () : argument list is missing.

If I use with blank () : function does not take 0 arguments.

I don't see what I'm meant to put into the parentheses.

As well, if I try initializing the acc function straight in the main, it plain doesn't work.

If anything is unclear, tell me and I'll clarify. Hoping I didn't leave anything out... Just holla if I did.
 

· Premium Member
Joined
·
19,572 Posts
The problem you are having is that you have a function where you read the accelerometer value and another one where you want to write the values to the label and since you are in different contexts that's not gonna work right away. There are different ways to solve the problem if i understand you correctly and here few:

Solution 1:
Make a global variable and set the accelerometer value each time the event is triggered. Having a global variable allows you to use it on your other function and since you update the value each time the accelerometer event is triggered you will always have the current value at hand.

Code:
Here some quick code(so that you get an idea)

Solution 1:

//Global variable
float fAccelerationVal;

//Each time the event is triggered add the x value
//of the acc pointer to the global variable.
void onAcceleration(Acceleration* acc, Event* event)
{
  fAccelerationVal = acc->x;
}

void AddValueToLable()
{
    pLabel->Text = fAccelerationVal;//convert/format value to string if necessary.
}
Solution 2:
Why not just simply set the label value in the accelerometer change event in the first place if possible? alternatively you can add a parameter to the function you use to set the label and call it each time the value change.

Code:
Solution 2 way #1

void onAcceleration(Acceleration* acc, Event* event)
{
  pLabel->Text = acc->x;//convert/format value to string
}
Code:
Solution 2 way #2

void onAcceleration(Acceleration* acc, Event* event)
{
  AddValueToLable(acc->x);//convert/format the x value to string before passing.
}

void AddValueToLable(CString& strVal)
{
    pLabel->Text = strVal;
}
 
1 - 5 of 5 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top