C# XNA 4.0 -> Need HELP!

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1871 Views - Last Post: 17 April 2013 - 12:10 PM

#1 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 10:09 AM

Hello!

I'm new in XNA game developing. Today I created simple game in which you can moving character right and left. Now I want to add gravity, so character will be able to jump. I tried to make something alone, but character went only up and not down. I spent about 3 hours searching on google/youtube. I found few great tutorials, but I dont understand anything...Tutorials simply don't help me... I would be happy if there is someone who is ready to look into my little project via teamviewer.


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: C# XNA 4.0 -> Need HELP!

#2 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 10:16 AM

No not over Teamviewer thanks. No personal tuition.

Please post your code of what you have and we will take it from there.
Was This Post Helpful? 0
  • +
  • -

#3 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 10:22 AM

View PostButchDean, on 14 April 2013 - 10:16 AM, said:

No not over Teamviewer thanks. No personal tuition.

Please post your code of what you have and we will take it from there.



OK.. Thanks for your reply.


My code:

Game1.cs
namespace _2D_Game
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        paris paris = new paris(new Vector2(50, 50));
        cursor cursor = new cursor(new Vector2(0, 0));


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Items.Initilize();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            foreach (obj o in Items.objList)
            {
                o.LoadContent(this.Content);
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            foreach (obj o in Items.objList)
            {
                o.Update();
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            foreach (obj o in Items.objList)
            {
                o.Draw(spriteBatch);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}




obj.cs
namespace _2D_Game
{
    class obj
    {
        public Vector2 position;
        public float rotation = 0.0f;
        public Texture2D spriteIndex;
        public string spriteName = "";
        public float speed = 0.0f;
        public float scale = 1.0f;
        public bool alive = true;

        public obj(Vector2 pos)
        {
            position = pos;
        }
        public obj()
        {

        }
        public virtual void Update()
        {
            if (!alive) return;
            pushTo(speed, rotation);
        }
        public virtual void LoadContent(ContentManager content)
        {
            spriteIndex = content.Load<Texture2D>("sprites\\" + this.spriteName);
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (!alive) return;
            Vector2 center = new Vector2(spriteIndex.Width/2, spriteIndex.Height/2);
            spriteBatch.Draw(spriteIndex, position, null, Color.White, MathHelper.ToRadians(rotation), center, scale, SpriteEffects.None, 0);
        }
        public void pushTo(float pix, float dir)
        {
            float newX = (float)Math.Cos(MathHelper.ToRadians(dir));
            float newY = (float)Math.Sin(MathHelper.ToRadians(dir));
            position.X += pix * (float)newX;
            position.Y += pix * (float)newY;

        }
    }
}



items.cs
namespace _2D_Game
{
    class Items
    {
        public static List<obj> objList = new List<obj>();

        public static void Initilize()
        {
            objList.Add(new paris(new Vector2(50, 50)));
            objList.Add(new cursor(new Vector2(0, 0)));
        }

        public static void Reset()
        {
            foreach (obj o in objList)
            {
                o.alive = false;
            }
        }

    }
}



paris.cs
namespace _2D_Game
{
    class paris : obj
    {
        KeyboardState keyboard;
        KeyboardState prevKeyboard;
        float spd;

        public paris(Vector2 pos)
            : base(pos)
        {
            position = pos;
            spd = 2;
            spriteName = "paris-mage";
        }
        public override void Update()
        {
            if (!alive) return;
            keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Up))
            {

            }
            if (keyboard.IsKeyDown(Keys.Left))
            {
                position.X -= spd;
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                position.X += spd;
            }
            prevKeyboard = keyboard;
            base.Update();
        }
    }
}



cursor.cs
namespace _2D_Game
{
    class cursor : obj
    {
        MouseState mouse;

        public cursor(Vector2 pos)
            : base(pos)
        {
            position = pos;
            spriteName = "cursor";
        }
        public override void Update()
        {
            mouse = Mouse.GetState();
            position = new Vector2(mouse.X, mouse.Y);

            base.Update();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 12:28 PM

Not seeing anything in your code to make your sprite jump.
Was This Post Helpful? 0
  • +
  • -

#5 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 01:03 PM

View PostButchDean, on 14 April 2013 - 12:28 PM, said:

Not seeing anything in your code to make your sprite jump.


Ofcourse not, beacuse I deleted not working code. It's seriously hard to tell here what is my problem... I have added platforms and now I have to make that character wont go through them. Then I have to add gravity and jump code. Im totally lost! I read something about velocity but I dont know what the heck is this.

//I added this code into Update void
position.Y += 4.0f; //Code pulling down character

//Then I add code for jump
   if (keyboard.IsKeyDown(Keys.Up))
            {
                position.Y -= 10.0f; //when up key is pressed character will jump
            }

//OK now i dont know if I made anything good or not... I have to make that character wont fall out of screen.



Was This Post Helpful? 0
  • +
  • -

#6 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 01:18 PM

So you need to put code in that brings the character back to a specific height once the key is released! Why is your y-axis inverted?

If you don't understand elementary physics maybe you should take on an easier project.
Was This Post Helpful? 0
  • +
  • -

#7 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 02:01 PM

View PostButchDean, on 14 April 2013 - 01:18 PM, said:

So you need to put code in that brings the character back to a specific height once the key is released! Why is your y-axis inverted?

If you don't understand elementary physics maybe you should take on an easier project.


thanks for your time! I was trying a little more and I successfully made everything I wanted. So my game now have gravity etc.

Now I have new problem. I want to flip my character sprite everytime I press left or right button!

this is my left and right keydown code:
if (Keyboard.GetState().IsKeyDown(Keys.Right)) velocity.X = 3f;
            else if (Keyboard.GetState().IsKeyDown(Keys.Left)) velocity.X = -3f; else velocity.X = 0f;


Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14925
  • View blog
  • Posts: 59,593
  • Joined: 12-June 08

Re: C# XNA 4.0 -> Need HELP!

Posted 14 April 2013 - 02:29 PM

The sprite batch 'draw' has a method signature that takes a 'SpriteEffects' which that you can set to flip horizontally. Track which key is pressed (right or left) by what ever method you need, and in the draw just check that and use the right SpriteEffects.
Was This Post Helpful? 0
  • +
  • -

#9 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 08:58 AM

View Postmodi123_1, on 14 April 2013 - 02:29 PM, said:

The sprite batch 'draw' has a method signature that takes a 'SpriteEffects' which that you can set to flip horizontally. Track which key is pressed (right or left) by what ever method you need, and in the draw just check that and use the right SpriteEffects.


ok thanks for help, but it wont work.


here is my code:
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectangle, Color.White);
        }


when i try to add SpriteEffects I get error
public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectangle, Color.White, SpriteEffects.FlipHorizontally);
        }



Thanks
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14925
  • View blog
  • Posts: 59,593
  • Joined: 12-June 08

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:01 AM

So... which 'draw' method were you using? I certainly do not see one that is texture2d, rectangle, color, spriteeffects.

Here - I'll give you the link to the MSDN docs on the available methods..
http://msdn.microsof...batch.draw.aspx
Was This Post Helpful? 0
  • +
  • -

#11 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:05 AM

Its me again. I want to tell I make it work now.


Working code for people with same problem:
SpriteEffects horizontalFlip = SpriteEffects.FlipHorizontally;

public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectangle, null, Color.White, 0.0f, Vector2.Zero, horizontalFlip, 0.0f);
        }



I will post here again if I will need more help! thanks
Was This Post Helpful? 0
  • +
  • -

#12 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:24 AM

I have short little question. I cant find answer on google.


Currently I have more then 10 platforms:

platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(0, 150)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(32, 150)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(64, 150)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(96, 182)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(128, 182)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(160, 214)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(192, 214)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(224, 246)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(256, 246)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(288, 278)));
            platforms.Add(new platform(Content.Load<Texture2D>("platform"), new Vector2(320, 278)));



BUT, I'm planing to create about 100-500 more platforms. So that mean I have to write 500 lines? 1 line for each platform like I did now? Is that best and easier way or is there any better way to do?

thanks
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14925
  • View blog
  • Posts: 59,593
  • Joined: 12-June 08

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:28 AM

If their placement was more linear you could use a for loop, but yes.. if you are going to add a ton of objects in various locations then yes. Gotta get your assets in there somehow, right?
Was This Post Helpful? 0
  • +
  • -

#14 TheMatto   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 13

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:37 AM

Thanks! I will defenetly use FOR loop for ground.


My next question is:

My "RectangleHelper" code:
static class RectangleHelper
{
    const int penetrationMargin = 5;
    public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Bottom >= r2.Top - penetrationMargin &&
            r1.Bottom <= r2.Top + 1 &&
            r1.Right >= r2.Left + 5 &&
            r1.Left <= r2.Right - 5);
    }
}

//code in update()
foreach (platform platform in platforms)
                if (player.rectangle.isOnTopOf(platform.rectangle))
                {
                    player.velocity.Y = 0f;
                    player.hasJumped = false;
                }



When I jump there is sometime space between platform and character. If I change "const int penetrationMargin = 5;" from 5 to 1. Problem is fixed, but if I change that, my character will randomly fall through platform. (after jump, when landing).


Ty
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14925
  • View blog
  • Posts: 59,593
  • Joined: 12-June 08

Re: C# XNA 4.0 -> Need HELP!

Posted 15 April 2013 - 09:40 AM

I have no idea what "penetrationMargin" means.. so if 1 doesn't work - how about trying 1.5, or 2?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2