Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts

Sunday, January 9, 2011

XNA Game Development Tips and Tricks 5: Animate 2D Sprites

Animation is very important part of game development, if your game does not have any kind of animation it will look very dull.

While doing animation we have to consider many aspects of the game:

  1. Animation should not be Slower
  2. Animation should be not be Faster
  3. Animation should be Smoother
  4. Animation should load Faster

We will be using following image for the animation:

256x32

If you look at above image, it is single image and not 8 tanks. Reason why we are not having 8 different images and loading them in the game is because it will take more time compared to having single long image loaded. If you have just one single sprite in your game for animation it will not make any difference, but if you have many sprites to be animated you will see significant performance difference. So let us see how we can animate the sprite and how we can control frame rates for the sprite.

First we will declare all required variables to be use.

private int FrameCount = 8; //number of frames in image
private int CurrentFrame = 0; //current frame to display
private float FrameLength = 0.12f; //Frame length per frame.. calculated 1/8 i.e. one second divided by number of frames (8)
private float FrameTimer = 0.0f; //Keep note of time

Now let’s load the content in memory, and add image to the Content project.

mSpriteTexture = Content.Load<Texture2D>(“bluetank”);

now, lets write Update code, which will update the current frame number which we would want to show to the user. Code which we have written in Update method will render 8 Frames in a second.

public void Update(GameTime gameTime)
        {
            FrameTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; //Find out the CurrentTimer and based on this we will calculate which Frame to render.

            if (FrameTimer > FrameLength)
            {
                FrameTimer = 0.0f; //Reset FrameTimer to 0
                CurrentFrame = (CurrentFrame + 1) % FrameCount;  //Set CurrentFrame
            }
        }

Finally draw the sprite on the screen with the updated Frame.

theSpriteBatch.Draw(mSpriteTexture, Position, new Rectangle(CurrentFrame * 26, 0, 26, mSpriteTexture.Height), Color.White);

Once you write above code, build it, run it and you should be able to see Your Sprite Animating.

Wednesday, December 29, 2010

XNA Game Development Tips and Tricks 3: Windows Phone 7 Playing Music and Sound FX

You are developing a game, and your game is pretty decent but game without sound and music is not good game and does not create excitement that it needs to. For example Xbox 360 has Achievement earning, when you earn achievement sound of achievement earning is so nice to ears that you would love to play game just for the sake of earning achievements. Anyways that's apart coming back to todays tips and tricks. We are going to see how you can play Sound and Music in game using Windows Phone 7, what are challenges, tips and tricks.

XNA provides two ways of playing sound/music:

1 – SoundEffect
2 – Song using MediaPlayer

When you want to play small sounds, like explosion of bomb or sound of coin dropping on floor or similar sound effects you would want to use SoundEffect class. and when you want to play Background Music you would want to use Song class. Difference is that SoundEffect only plays WAV files, so when you use Music as WAV file, your music would be at-least 1 minute long, and 1 minute for WAV file is huge, your wav file will be very large approximate 20-30 MB.

Song class has another issue that at a time we can play only one file, we can not play more than 1 file simultaneously. So if you want to play Human voice, you might want to make them as WAV file and play them using SoundEffect class.

Have a look at following code:

SoundEffect sfx = Content.Load<SoundEffect>(“coinsound”);
sfx.CreateInstance().Play();

Above code will load content which is “coinsound” in wav file format, we create it’s instance and then play. Now you must be wondering why to call CreateInstance? when we are already initializing object. Now take a case when you collect coin you want to play sound so you execute above code, now above code works fine, but what if we collect multiple coins at same time then what to do in that case? In that case above code will produce sound of coin only once, even if you execute Play method multiple times, and will not give kind of effect we are looking for. So to produce multiple sound effect of same sound file, we can use same object but every time we will have to call CreateInstance and then call Play method.

Now, for playing Music we write below code:

Song sng = Content.Load<Song>(“backmusic”);
MediaPlayer.Play(sng);
MediaPlayer.IsRepeating = true;

Notice we are using MediaPlayer class for playing music, it means it will be using Zune Player to play the music. Now when you write above code and run your application you will come across given below error:

“Song playback failed. Please verify that the song is not DRM protected. DRM protected songs are not supported for creator games.”

image

This happens because your Windows Phone’s Zune Library is locked by Zune Software on PC.. Currently possible way of doing is to first run your game through Visual Studio on Device, when you encounter above error, stop debugging, disconnect device from PC and then start game on device through applications, and now you should be able to hear Music. NOTE: When you do SoundEffect you will not have any problem as above, It is only with MediaPlayer.

Sunday, December 26, 2010

XNA Game Development Tips and Tricks 2: Windows Phone 7 Full Screen Game

Whenever you develop a game for Windows Phone 7 using XNA game does not start in Full Screen mode, meaning when game starts it does not occupy the entire screen instead it leaves some space on top if running in Portrait mode or leaves some space on left side if running in Landscape mode.

image    image

This happens because Windows Phone Operating System makes top space or left space available for the battery meter indicator, time, cell signal, wifi signal, etc… now this can make your game look really bad and you will not be able to utilize the entire screen. So what to do if you want to occupy entire screen? Well, very simple you just have to write one single line of code, look at below code snippet:

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    graphics.IsFullScreen = true;
    Content.RootDirectory = "Content"; 
    TargetElapsedTime = TimeSpan.FromTicks(333333);
}

Writing above code will hide the bar which occupied the space. But this does not mean your game will block calls, notifications, etc.. whenever any kind of notification or call arrives Windows Phone Operating System will alert your game saying User is not able to see screen so that you can pause the game. This happens only when you receive a call or notification which would take up a space of screen.

For the partial-screen Obscured notification and the tombstoning notification to the Game.Deactivated event will be fired. PhoneApplicationService.Deactivated event will fires for both Silverlight and XNA when the app is being tombstoned. Obscured and Unobscured are not binded in App.xaml.cs file is because there will be many non-game applications which will not need to use these events.

Once you done with the above code, compile and run the code and notice the difference.

image    image

Saturday, December 25, 2010

XNA Game Development Tips and Tricks 1: Windows Phone 7 Portrait Mode

Hello, with this blog post I am starting tips and tricks on Developing Games on Windows Phone 7 using XNA.. or in general XNA.

By default whenever you develop game for Windows Phone 7 using XNA, your game will always start in Landscape mode. Your emulator will start in portrait mode, but game is playable in landscape mode.

image

Let’s say you have requirement of developing game for portrait mode and not landscape mode, you have to add 2 lines of code. In your game after you initialize GraphicsDeviceManager write following 2 lines:

 

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    graphics.PreferredBackBufferWidth = 480;
    graphics.PreferredBackBufferHeight = 800;

    TargetElapsedTime = TimeSpan.FromTicks(333333);
}

 

after writing above code, compile and run it. .and you will have your game starting in Portrait mode.

image