• banner contest_big deadline

    Deadline
    Extended!!

    To give more time to developers 
    we have decided to extend
    Cell·APP Awards until June 15th.

  •     Developed with CELL·SDK

    img zombeee

    google iOS WP

  • banner contest_big 

                    Vote Now!

    You can vote and be voted until 
    June 22nd! Vote and Share 
    the best application on
    facebook, twitter... 

    podium
  • slide pitada

     Pitada contra 
    el Hambre

    Multiplatform Application 
    developed by TheGameKitchen 
    using CELL·SDK.

  • Easy of Use
    and Great Potential

    High level API linked to that
    which creates applications,
    obtaining maximun profitability
    thanks to automatic translation
    and native code.

    img gallery
  • Official Emulator

    CELL·SDK allows you to debug your
    applications using the official
    emulators of Android, iOS and
    Windows Phone, increasing the
    reliability of your rest.

    img gallery 05

  home text  

Uses C# to create applications on Android, iOS
and Windows Phone.  

 

    devices home  

bt new_trial_downloadbt quick_tour 

Accelerometer

Welcome to another CellSDK Tutorial.

In this tutorial we will see the basics to work with the accelerometer sensors of our mobile devices, and who could know more about accelerometers than Fernando Alonso? So, please a big applause to Fernando who will teach us about accelerometer uses.

Well, hello, so we need be concentrated just because this is something we need to learn carefully, we need the team focused to get the victory in this race. A victory in this race will gave us the opportunity to make more fantastic applications and games so it is very important to be focused, you know.

So, let’s starts our cars, and create a new project. Our team has prepared a little images to make our sample a little funnier called “cell1.png”, “cell2.png”, “cell3.png”, “cell4.png”. So add it to our Content project.

We will start by initiating the accelerometer sensor. So, add a reference to the library called Syderis.CellSDK."Platform".IO.AccelerometerSystem.dll located in the CellSDK installation directory, depending on the target I am sure what you have to do with the Platform in bold, and now add these lines to the Initialize method:

 

AccelerometerSensor.Instance.Start();

 

Now we are going to need some class variables to be able to access them in the Update() method we will override in a minute, so please, declare this variables as class variables:

 

int accelfactor = 10; 
List<Label> labels= new List<Label>();
float maxX;
float maxY;
Vector2 centerposition;
Vector2 actualPosition;

 

If you are worried about the sense of this variables, do not worry, you will see it in one second. By now, add these initiation lines to the Initialize() method:

 

labels.Add(new Label(ResourceManager.CreateImage("cell1")));
labels.Add(new Label(ResourceManager.CreateImage("cell2")));
labels.Add(new Label(ResourceManager.CreateImage("cell3")));
labels.Add(new Label(ResourceManager.CreateImage("cell4")));
centerposition = new Vector2(Preferences.Width / 2 - labels[0].Size.X / 2, Preferences.Height / 2 - labels[0].Size.Y / 2);
maxX = Preferences.Width - labels[0].Size.X;
maxY = Preferences.Height - labels[0].Size.Y;
AddComponent(labels[0], 10, 10);
AddComponent(labels[1], 250, 10);
AddComponent(labels[2], 10, 600);
AddComponent(labels[3], 250, 600);

 

Now we need to override the Update() method just to actualize the movement of the cells label when the accelerometer values change.

The logic we will implement is that for each cell that gets out of the screen margin it will come back to the center of the screen and starts once again. And while the label is into the screen margins, we will add the accelerometer data to the correspondent position coordinate. So use override the Update(method in this way:

 

public override void Update(GameTime gameTime)
{
     base.Update(gameTime);
     foreach (Label lbl in labels)
     {
         if (lbl.Position.X >= 0 && lbl.Position.X <= maxX && lbl.Position.Y >= 0 && lbl.Position.Y <= maxY)
         {
             actualPosition.X += AccelerometerSensor.Instance.Data2.X * (accelfactor);
             actualPosition.Y -= AccelerometerSensor.Instance.Data2.Y * (accelfactor);
             lbl.Position = actualPosition;
         }
         else
         {
             lbl.Position = centerposition;
         }
     }
}

 

As you may notice you will be wondering why the accelerometer Y coordinate is not adding to the actualPosition variable. This is because the positive values to this data means the up direction, so that is why we are substracting it.

So if you build and run this application in a physic device you will be able to see how this example ends:

accelerometer1

One thing you should take care if you are going to access special properties like for exampleAccelerometerSensor.Instance.IsConnected in the update method. As the accelerometer is a physical device, this kind of properties has a special overhead, meaning that it takes too much time to access them. So it is better to declare a private variable and set it in the Initialize method in this way:

 

if(AccelerometerSensor.Instance.IsConnected)
    accelerometerIsConnected=true;

 

So now, you have a variable with the value and without the overhead mentioned before.accelerometerIsConnected is the variable you should use in the Update() method.

Now we can play with the accelerometer … but wait we are forgetting something. We have to stop the accelerometer system when we leave the application, so add this line to release the accelerometer resource:

 

public override void BackButtonPressed()
{
     AccelerometerSensor.Instance.Stop();
     base.BackButtonPressed();
}

 

So I hope you enjoyed this tutorial, it has been a hard work, but the team could not be better.

Summary

We have learned the basics to work with accelerometer system in our applications, so let your mind to create better user experience with this new knowledge.

"Keep on cool coding."

You can get the code in Github

Social Network

This e-mail address is being protected from spambots. You need JavaScript enabled to view it.