Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26
  1. #11
    Join Date
    Sep 2006
    Location
    Polysorbate 60
    Posts
    925
    I fixed it for you, but I hard coded some values in there for you to learn off of.

    Game.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;
    
    namespace WindowsGame6
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
            Sprite background1;
            Sprite background2;
            Sprite background3;
            Sprite background4;
            Sprite background5;
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                graphics.PreferredBackBufferWidth = 640;
                graphics.PreferredBackBufferHeight = 480;
                Content.RootDirectory = "Content";
            }
    
            protected override void Initialize()
            {            
                background1 = new Sprite();
                background2 = new Sprite();
                background3 = new Sprite();
                background4 = new Sprite();
                background5 = new Sprite();
    
                base.Initialize();
            }
    
            protected override void LoadContent()        
            {
    
                spriteBatch = new SpriteBatch(GraphicsDevice);
    
                background1.LoadContent(this.Content, "Cannon_Background1");
                background1.Position = new Vector2(0, 0); //Top left origin
                background2.LoadContent(this.Content, "Cannon_Background2");
                background2.Position = new Vector2(0, -480); //Off the top of the screen is in negative values!
                background3.LoadContent(this.Content, "Cannon_Background3");
                background3.Position = new Vector2(0, -480 *2); //Size of last photo times two
                background4.LoadContent(this.Content, "Cannon_Background4");
                background4.Position = new Vector2(0, -480 *3); //ect...
                background5.LoadContent(this.Content, "Cannon_Background5");
                background5.Position = new Vector2(0, -480 *4); //ect...
            }
    
            protected override void UnloadContent()
            {
    
            }
    
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                if (background1.Position.Y > 480) //Bottom of the screen is positive values!
                {
                    background1.Position.Y = background5.Position.Y - 480; //place it above the last photo in the chain offset in NEG!
                }
    
                if (background2.Position.Y > 480)
                {
                    background2.Position.Y = background1.Position.Y - 480;
                }
    
                if (background3.Position.Y > 480)
                {
                    background3.Position.Y = background2.Position.Y - 480;
                }
    
                if (background4.Position.Y > 480)
                {
                    background4.Position.Y = background3.Position.Y - 480;
                }
    
                if (background5.Position.Y > 480)
                {
                    background5.Position.Y = background4.Position.Y - 480;
                }
    
                Vector2 direction = new Vector2(0, 1);
                Vector2 speed = new Vector2(0, 70);
    
                background1.Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                background2.Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                background3.Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                background4.Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                background5.Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
    
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                spriteBatch.Begin();
                background1.Draw(this.spriteBatch);
                background2.Draw(this.spriteBatch);
                background3.Draw(this.spriteBatch);
                background4.Draw(this.spriteBatch);
                background5.Draw(this.spriteBatch);
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    42

  2. #12
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    234
    You're welcome for the handout. Please look at ALL the changes I made and try to make sense of them all. If I were you, I'd strip out most of this code and encapsulate it in a class, perhaps clean it up a little.

    Edit: Didn't realize till I posted the code -- you should change your IDE to use a single tab rather than 4 spaces.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;
    using Microsoft.Xna.Framework.Storage;
    
    namespace WindowsGame6
    {
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
    
            Sprite background1;
            Sprite background2;
            Sprite background3;
            Sprite background4;
            Sprite background5;
    				public const int ScreenWidth = 640;
    				public const int ScreenHeight = 480;
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
    						graphics.PreferredBackBufferWidth = ScreenWidth;
    						graphics.PreferredBackBufferHeight = ScreenHeight;
                Content.RootDirectory = "Content";
            }
    
            protected override void Initialize()
            {            
                background1 = new Sprite();
                background2 = new Sprite();
                background3 = new Sprite();
                background4 = new Sprite();
                background5 = new Sprite();
    
                base.Initialize();
            }
    
            protected override void LoadContent()        
            {
    
                spriteBatch = new SpriteBatch(GraphicsDevice);
    
                background1.LoadContent(this.Content, "Cannon_Background1");
                background1.Position = new Vector2(0, 0);
                background2.LoadContent(this.Content, "Cannon_Background2");
                background2.Position = new Vector2(0, background1.Position.Y - background1.Size.Height);
                background3.LoadContent(this.Content, "Cannon_Background3");
                background3.Position = new Vector2(0, background2.Position.Y - background2.Size.Height);
                background4.LoadContent(this.Content, "Cannon_Background4");
                background4.Position = new Vector2(0, background3.Position.Y - background3.Size.Height);
                background5.LoadContent(this.Content, "Cannon_Background5");
                background5.Position = new Vector2(0, background4.Position.Y - background4.Size.Height);
            }
    
            protected override void UnloadContent()
            {
    
            }
    
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                if (background1.Position.Y >= ScreenHeight)
                {
                    background1.Position.Y = background5.Position.Y - background1.Size.Height;
                }
    
                if (background2.Position.Y >= ScreenHeight)
                {
                    background2.Position.Y = background1.Position.Y - background2.Size.Height;
                }
    
                if (background3.Position.Y >= ScreenHeight)
                {
                    background3.Position.Y = background2.Position.Y - background3.Size.Height;
                }
    
                if (background4.Position.Y >= ScreenHeight)
                {
                    background4.Position.Y = background3.Position.Y - background4.Size.Height;
                }
    
                if (background5.Position.Y >= ScreenHeight)
                {
                    background5.Position.Y = background4.Position.Y - background5.Size.Height;
                }
    
                Vector2 direction = new Vector2(0, 1);
                Vector2 speed = new Vector2(0, 70);
    						Vector2 deltaY = direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                background1.Position += deltaY;
                background2.Position += deltaY;
                background3.Position += deltaY;
                background4.Position += deltaY;
                background5.Position += deltaY;
    
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                spriteBatch.Begin();
                background1.Draw(this.spriteBatch);
                background2.Draw(this.spriteBatch);
                background3.Draw(this.spriteBatch);
                background4.Draw(this.spriteBatch);
                background5.Draw(this.spriteBatch);
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    Last edited by Olson.dev; 02-10-2009 at 10:34 PM.

  3. #13
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    234
    Quote Originally Posted by ZanQuance View Post
    I fixed it for you, but I hard coded some values in there for you to learn off of.
    Gah, you beat me to it.

  4. #14
    Join Date
    Sep 2006
    Location
    Polysorbate 60
    Posts
    925
    Let him learn proper coding practices himself through trial and error. The only thing he needed to understand is how the screen coordinates are setup, with 0,0 in the top left and positive values on the visible portion. Everything off the left side of the screen are negative X values, and everything off the top of the screen are negative Y values.

    This was the only thing that confused him and needed help with
    42

  5. #15
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    234
    Quote Originally Posted by ZanQuance View Post
    Let him learn proper coding practices himself through trial and error. The only thing he needed to understand is how the screen coordinates are setup, with 0,0 in the top left and positive values on the visible portion. Everything off the left side of the screen are negative X values, and everything off the top of the screen are negative Y values.

    This was the only thing that confused him and needed help with
    I tried getting that through in a previous post and it didn't stick, hence my handout. I'm going to go out on a limb here... proper coding practices should be taught immediately, whether through handout or not. Why force every developer to learn EVERYTHING from trial and error? Sure, there are some things you should learn from trial and error... but, proper coding practices? You don't learn all of those through trial and error. Now that I've posted more than I intended...

  6. #16
    Join Date
    Sep 2006
    Location
    Polysorbate 60
    Posts
    925
    Sorry, I didn't mean it like that. What I meant were the additional things added that would change his coding style completely. If his current method of coding is easier for him then let him improve his own code over time is what I meant

    For instance he could easily replace those multiple IF statements with something more efficient, but if they are easier for him to understand as they are now, then let him upgrade his code to something better later.

    Buzz teaches in the easiest to learn method for new students. There is no reason to force proper coding techniques right away on someone who is still learning the basics.
    In time he will evolve his code to look and perform in a proper way.
    42

  7. #17
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    234
    I understand and agree on most points (of the few I don't, don't care to get into ). And, yeah, he could definitely make it more efficient (hence my comment to clean it up). I am putting faith in him to not just copy-paste my code without looking through it first. He can translate it to his style if he so-chooses.

  8. #18
    Join Date
    Jun 2008
    Posts
    38
    Olson & ZanQuance - thank you both for the responses! The code "handout" worked perfectly and it was the coordinates that had me all screwed up as you had mentioned. I wasn't aware that in computing the coordinates work differently than the Cartesian system. Believe me, asking for this level of help was a last resort after hours and hours of banging my head against the desk. I just needed a push in the right direction!

    Just to give you a little background, I'm extremely new to programming and C# is the first language I've every attempted to learn. My first lick of C# programming was about 2 months ago with XNA Xtreme Vol I. I completed all the videos and learned a good deal of fundamentals but obviously I have a WAYS to go. Vol II is on it's way and in the meantime, I've been working hard on studying tutorials and then attempting to implement various updates into the Cannon 3 project. I realize my coding isn't all that effecient or "proper" but right now I'm just focused on coding in a format that is most logical for my experience level and one that is easier for me to reference when needed.

    Thank you both again for the help - it's very much appreciated. And while I'm at it, I should thank Chronos78 who took some time to help me iron out some unrelated audio/XACT issues recently.

  9. #19
    Join Date
    Jul 2007
    Location
    Minnesota
    Posts
    234
    No problem. Keep at it; it's very rewarding. You'll enjoy Volume II... and III.

  10. #20
    Join Date
    Sep 2006
    Location
    Polysorbate 60
    Posts
    925
    Word of advice! since this is your first language, try not to bite of more than you can chew! start with simple projects first. I know that making Hyperion and Cannon seem like simple games, that don't really accomplish what a person who wishes to get into game development really want to create. But they hammer in the basics, which must become second nature to you when doing any advanced programming.

    In time it will become critical to manage your code and keep everything organized into their own classes, but for now some sloppy coding is acceptable since your still learning the concepts. One does not run a programming marathon until he has crawled at least once ^_^

    If you want to turn Cannon3 into a typical Vertical scrolling-shooter, then perhaps not splitting the map plane would be a wiser choice. Most arcade games of that type use one large map, larger than your viewport size which can be scrolled side to side a bit, and also when they reach the end for the boss fight, the map stops scrolling, or gets replaced with a loopable background like water or clouds.

    If you are after a simple scrolling starfield background, there are easier ways to do this than with many pictures that repeat
    42

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •