Chester
Chester

Reputation: 21

Visual studio doesn't recognize my constructor?

I recently started working with monogame and everything went great until this happened. I am not very experienced so this might just seem stupid but I've tried everything and this is my only option, so this is my constructor:

public void qlayer(Vector2 position, Texture2D texture)
{
    this.position = position;
    this.texture = texture;
}

"qlayer" is the name of my class but and it keeps saying: "member names cannot be the same as their enclosing type" which would make sense if i didn't want to make a constructor!

Just to be safe, here's the entire class:

class qlayer
{
    Vector2 position;
    Point speed;
    Rectangle hitbox;
    Texture2D texture;
    Point currentFrame;
    int timeSinceLastFrame = 0;
    int millisecondsPerFrame;
    bool isFront = true;
    Point sheetSize = new Point(2,3);

    public void qlayer(Vector2 position, Texture2D texture, SpriteBatch spritebatch)
    {
        this.position = position;
        this.texture = texture;

    }

    enum PlayerAni
    {
        left, front, right
    };

    PlayerAni currentAni = PlayerAni.front;

    public void Update(GameTime gameTime)
    {
        timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame = 0;
            ++currentFrame.X;
            if (currentFrame.X >= sheetSize.X)
            {
                currentFrame.X = 0;
                ++currentFrame.Y;
                if (currentFrame.Y >= sheetSize.Y)
                    currentFrame.Y = 0;
            }
        }

        if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
        {
            position.X -= 3;
            currentAni = PlayerAni.left;
        }
        else
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
            {
                position.X += 3;
                currentAni = PlayerAni.right;
            }
            else
            {
                currentAni = PlayerAni.front;
            }
        }

    }

    public void Draw(GameTime gameTime, SpriteBatch spritebatch)
    {
        spritebatch.Begin();
        spritebatch.Draw(texture,
               position, new Rectangle(currentFrame.X * 76,
                   currentFrame.Y * 54,
                   76, 54),
               Color.White, 0, Vector2.Zero,
               1f, SpriteEffects.None, 0);

        spritebatch.End();
    }
}

Upvotes: 2

Views: 1019

Answers (1)

gunr2171
gunr2171

Reputation: 17578

public void qlayer

Take out the word void. Constructors don't have a "return" type, including void. Your constructor should look like this:

public qlayer(Vector2 position, Texture2D texture)

and/or

public qlayer(Vector2 position, Texture2D texture, SpriteBatch spritebatch)

Upvotes: 10

Related Questions