Need Help with the C++

Discussion in 'Web development / Programming' started by Hy-Def, Jul 15, 2016.

  1. Hy-Def

    Hy-Def Chubbychu. (AKA Pikachu-bby (AKA Pikachu-BBW))

    Messages:
    3,198
    Likes Received:
    161
    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.
  2. NGEmu.com Advertisement

  3. Hy-Def

    Hy-Def Chubbychu. (AKA Pikachu-bby (AKA Pikachu-BBW))

    Messages:
    3,198
    Likes Received:
    161
  4. @ruantec

    @ruantec Crazy GFX coder Emu Author

    Messages:
    14,659
    Likes Received:
    1,035
    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;
    }
    
  5. Hy-Def

    Hy-Def Chubbychu. (AKA Pikachu-bby (AKA Pikachu-BBW))

    Messages:
    3,198
    Likes Received:
    161
    Hey @@ruantec. Thanks for the response. The first method isn't working quite right. It gave me a strange number in Windows. But remains as 0 in Android.

    I'll try and see if i can figure anything out but thanks so far.
  6. Hy-Def

    Hy-Def Chubbychu. (AKA Pikachu-bby (AKA Pikachu-BBW))

    Messages:
    3,198
    Likes Received:
    161
    Got it working fine with the second option, setting the label straight from the function. Thanks for the help, mate!

Share This Page