Forum Libraries, Components, and Plugins

How do I enable the accelerometer in v1.7?

I use to do this to enable the accelerometer in the older version:

Window.Accelerometer.Enabled = true;

How do I enable it in the latest version used with Xamarin Forms since "Window" doesn't exist anymore?

I tried to get to the accelerometer through the "GameView" object but don't see anything there either.

Please help.


  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Hashen,

    Within the implementation of your LoadGame event you can do the following

    void LoadGame (object sender, EventArgs e)
        var nativeGameView = sender as CCGameView;
        var accelerometer = nativeGameView.Accelerometer;

    Hope that helps.

  • DevHashDevHash ZAMember
    edited January 2016

    Hi Rami,

    Thanks very much for the quick reply, however CCGameView does not expose a property for Accelerometer. Looking at the source code and accelerometer test I see that the Accelerometer property is defined in EmbeddableView/CCGameView.Mobile.cs but this does not carry through into the CocosSharp package v1.7.1.0 definition for CCGameView.

    I managed to solve this by creating a new instance of CCAccelerometer:
    accelerometer = new CCAccelerometer (GameView); accelerometer.Enabled = true;

    and calling the accelerometer update method from my game update loop:
    if (accelerometer != null) { accelerometer.Update (); }

Sign In or Register to comment.