domingo, 20 de fevereiro de 2011

XNA Adventures: Building a Sprite Class

Hi everybody!
After some time playing with XNA I decided to start spreading what I know so others don't need to suffer in some problems and can enjoy as much or even more than I'm enjoying play with it.
In this first tutorial I'll show you something very basic that is a simple base Sprite class that you can expand or use in any project (I'll use it in my next tutorial to show 2D animation).

Visual Studio 2010 or 2008; (I'm using VS2010)
XNA 4.0 or 3.x; (I'm using 4.0 so somethings might need changes, just ask me if you got any problems ok?)
Start by creating a new windows game project in visual studio then create a new class named Sprite. This class will be the base for any sprite (i.e. char sprite, life sprite, background and so on) that you create so don't add any specific logic here.

A sprite needs a texture (the image), a position, a size, a scale and his asset name. Let's add them as attributes

The properties are now in their place but we don't set any of them, we're going to do this using two methods: Rescale and LoadContent
-Rescale: This method will verify if the texture exists and then will rescale the image trough Size property; -LoadContent: This method will load our texture and rescale the image properly calling Rescale.

Ok almost there, we just need to draw our sprite and update it so it'll be prepared to future movement input.
To achieve that we will add two drawing methods and one update method.
-Draw: Use a SpriteBatch.Draw method to draw our texture in the screen;
-Update: Change position using sprite speed and direction and the game time.
The drawing method should be like this

Basically the parameters we're passing to SpriteBatch.Draw are:
-Texture: _spriteTexture;
-Position: Position;
-SourceRectangle: sourceRectangle; This represents the area of your image that actually will be draw to the screen, everything that is not inside this rectangle will be ignored.
-Color: Color.White; This is the a color to tint your sprite. Use white if you don't want to add any color layer to your image.
-Rotation: 0.0f;
-Origin:Your sprite origin, in this case it will be upper-left corner (0,0).
-SpriteEffects: flip your image here or use none;
-LayerDepth: xna draw textures in layers, use 0 so xna organize the layers or specify a number to set your texture layer;
To finish our Sprite class we only need an update method

From here you can change and adapt it as you desire.
If you just want to test it go on and change your Game class with the following code

That's it guys!

sábado, 5 de fevereiro de 2011

Before starting to code for Android

So many people asked me "what is needed to start creating apps for Android?" that I decided to make a small guide here to help you in this awesome quest. First of all download the following:
Now start by installing JDK package, if you already have be sure that it's updated to version 5 or 6.
Then extract the sdk zip file in an easy to find folder. Do the same for eclipse but in another folder.
Now I'll try to make this easy so follow the image:
  • Click in Install New Software on Help;

  • Wait for it to update and select Developer Tools. Now you see the installation list, read the license agreements (accept it otherwise you can't install anything :P);
  • Reestart Eclipse. 

In the future when you need to update your SDK, simply repeat the steps above, just skip the Software Site adding.

To complete the SDK installation  we need a few final steps.

  • Open Eclipse, if isn't already opened, click on Window > Android SDK and AVD Manager. The manager gives you the capability of controlling your virtual devices (like iphone simulator), your installed packages and install the availables packages. 

  • After that your gonna select available packages > select all of android packages (if you want to select the third party packages is ok but will take longer to download). 

  • Click in install selected and go drink some juice. 

The installation is completed but we're not there yet, there's still the AVD to configure.
Access the Android SDK and AVD Manager again, once there click on Virtual Devices > New, the window bellow should appear. Fill the information and click  in Create AVD.

Name -> A name for your virtual device;
Target -> Which version of the Android OS the device will simulate;
SD Card -> If you need it fill the size or use a file (a .iso file);
Snapshot -> Check this if you want to save state. The same as vmware/sun virtualbox snapshot;
Skin -> Specify the resolution, I often use built-in;
Hardware -> Set your hardware needs. Like GPS and Track ball for example.

Good to go guys!!! Have fun... Any problems, doubts, sugestions plz let me know. Oh almost forgot the reference: Android Development Site


If anything goes wrong check if the SDK folder is set in preferences. Access Preferences on Window and you should see the image bellow. Select Android and then browse the SDK location