Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having trouble trying to get my character to move for a game I'm making in my sparetime for the Xbox. However, I can't seem to figure out what I'm doing wrong , and I'm not even sure if I'm doing it right. I've tried googling tutorials on this but I haven't found any helpful ones. Mainly, ones on 3d rotation on the XNA creators club website.

My question is : How can I get the character to walk towards the right in the MoveInput() function? What am I doing wrong? Did I code it wrong?

The problem is : The player isn't moving. I think the MoveInput() class isn't working.

Here's my code from my character class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Jumping
{
class Character
{
    Texture2D texture;
    Vector2 position;
    Vector2 velocity;
    int velocityXspeed = 2;
    bool jumping;

    public Character(Texture2D newTexture, Vector2 newPosition)
    {
        texture = newTexture;
        position = newPosition;
        jumping = true;
    }
    public void Update(GameTime gameTime)
    {
        JumpInput();
        MoveInput();
    }
    private void MoveInput()
    {
        //Move Character right 
        GamePadState gamePad1 = GamePad.GetState(PlayerIndex.One);
        velocity.X = velocity.X + (velocityXspeed * gamePad1.ThumbSticks.Right.X);
    }

    private void JumpInput()
    {
        position += velocity;

        if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed && jumping == false)
        {
            position.Y -= 1f;
            velocity.Y = -5f;
            jumping = true;
        }

        if (jumping == true)
        {
            float i = 1.6f;
            velocity.Y += 0.15f * i;
        }

        if (position.Y + texture.Height >= 1000)
            jumping = false;

        if (jumping == false)
            velocity.Y = 0f;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}

}

share|improve this question
1  
What have you done to debug this so far, what have you tried? Does Position actually work if you directly put different numbers, what are your joypad read values, logged velocities and positions, are these routines actually getting called? –  Patrick Hughes Jun 20 '12 at 14:30
    
The function is being called in the Main Class file update under Player.Update, and the update in the Character class (this class), is calling MoveInput(). But for some odd reason the character isn't walking across the screen. –  Monstr92 Jun 20 '12 at 14:32
    
Start with a known good example. This question is too localized to your code for it to help anyone else in the future. –  Byte56 Jun 20 '12 at 14:34
    
I'll keep that in mind for future questions! I just solved the riddle thanks to your suggestion Vector2 velocity = new Vector2(0,0); ! :3 –  Monstr92 Jun 20 '12 at 14:37
    
Great, you can accept the answer if that's what helped you solve your problem. –  Byte56 Jun 20 '12 at 14:39

1 Answer 1

up vote 4 down vote accepted

Though you haven't told us what the problem is. I can see that you never initialize your velocity variable.

Vector2 velocity = new Vector2(0,0);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.