androidxamarinaccelerometercocossharp

CocosSharp Accelerometer Axes


Ok so i have to create a game for a competiton but i encountered the following problem: I have to get the x ,y ,z Axes of a Acceleromter.I use xamarin and i create a CocosSharp game.i manage to get them in MainActivity but i have to get them in GameLayer.Cs. Here i have to use CCEventAcceleration and CCAccelerometer but it doesn't seems to work.Until now i tryed to get them this way

CCEventAccelerate Acl;
CCLabel label;
label = new CCLabel("Score: 0", "Arial", 20, CCLabelFormat.SystemFont);
label.PositionX = 50;
label.PositionY = 880;
label.AnchorPoint = CCPoint.AnchorUpperLeft;
AddChild(label);

Then i tryed

try
   {
      label.text = Acl.Acceleration.X.ToString();
   }
catch(Exception ex)
{
label.text = ex.ToString();
}

Solution

  • You need to activate the accelerometer, and register an eventlistener, see example below. Checkout the CocosSharp test project for more details: https://github.com/mono/CocosSharp/blob/master/tests/tests/classes/tests/AccelerometerTest/AccelerometerTest.cs

    GameView.Accelerometer.Enabled = true;
    var accelListener = new CCEventListenerAccelerometer();
    accelListener.OnAccelerate = DidAccelerate;
    AddEventListener(accelListener); 
    
    public void DidAccelerate(CCEventAccelerate accelEvent)
    {
       label.text = accelEvent.Acceleration.X.ToString();
    }