xna port of shooter tutorial from app hub to iPhone

May 3, 2012 at 1:24 PM

I'm using the shooter tutorial from the app hub to learn xna and monogame. I have completed it with the exception of adding sound.  When I run the program I get 2 explosions when an enemy is destroyed by either colliding with the ship or being hit with the projectile.  I have even gone so far as to copy the game1, projectile, and enemy classes from the tutorial into my project and I get the same result.

Question, has anyone used this tutorial successfully and/or has anyone had a similar experience.

Link to code: Complete Game - for Phone

There is a fix in the paralaxingbackground.cs that I had to do get this to work in iOS

in the initialize method I had to change this line from +1 to +2 

positions = new Vector2[(screenWidth / texture.Width) + 2];

 

It is a simple example and I would expect it to work without issue.

Any help is appreciated.

Thanks,

May 3, 2012 at 7:45 PM
Edited May 3, 2012 at 7:47 PM

Hi,

Try adding a reference to the PNG for the explosion file. I made a similar game and I saw two explosions when I used the XNB for some reason.

I maintain that example, so please send me any code samples and I will sync them up.

By the way, did you use this code:

http://www.jmawebtechnologies.com/company-blog/april-2012/porting-an-xna-game-to-ios-(iphone,-ipad)

I also checked it into the GitHub samples recently.

May 4, 2012 at 1:11 PM
Edited May 4, 2012 at 1:14 PM

@joe_a84, thanks for the reply.  The sample from MS did not have any XNB files in it, is it a best practice to convert all content to XNB files?  If so what tool do you recommend to do that.

I just downloaded your sample and it worked, I will spend some time and compare the two, again I think its revolving around the XNB content.

As mentioned above I did make a minor modification to the parallaxingbackgound.cs, its bolded below.

namespace Shooter
{
    class ParallaxingBackground
    {
        // The image representing the parallaxing background
        Texture2D texture;

        // An array of positions of the parallaxing background
        Vector2[] positions;

        // The speed which the background is moving
        int speed;

        public void Initialize(ContentManager content, String texturePath, int screenWidth, int speed)
        {
            // Load the background texture we will be using
            texture = content.Load<Texture2D>(texturePath);

            // Set the speed of the background
            this.speed = speed;

            // If we divide the screen with the texture width then we can determine the number of tiles need.
            // We add 1 to it so that we won't have a gap in the tiling
            positions = new Vector2[screenWidth / texture.Width + 2];

            // Set the initial positions of the parallaxing background
            for (int i = 0; i < positions.Length; i++)
            {
                // We need the tiles to be side by side to create a tiling effect
                positions[i] = new Vector2(i * texture.Width, 0);
            }
        }

        public void Update()
        {
            // Update the positions of the background
            for (int i = 0; i < positions.Length; i++)
            {
                // Update the position of the screen by adding the speed
                positions[i].X += speed;
                // If the speed has the background moving to the left
                if (speed <= 0)
                {
                    // Check the texture is out of view then put that texture at the end of the screen
                    if (positions[i].X <= -texture.Width)
                    {
                        positions[i].X = texture.Width * (positions.Length - 1);
                    }
                }

                // If the speed has the background moving to the right
                else
                {
                    // Check if the texture is out of view then position it to the start of the screen
                    if (positions[i].X >= texture.Width * (positions.Length - 1))
                    {
                        positions[i].X = -texture.Width;
                    }
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < positions.Length; i++)
            {
                spriteBatch.Draw(texture, positions[i], Color.White);
            }
        }
    }
}

May 4, 2012 at 2:09 PM
Edited May 4, 2012 at 2:11 PM

Thanks. I sent in the update.

Visual Studio makes the XNB. In my game, here is the location:

\bin\Windows Phone\Debug\Content

I know for fonts you need the XNBs, though pictures and music can go in as is.