Move Item via class code

Started by
1 comment, last by Bruno Filip 8 years, 4 months ago

So I need help, I dont have any errors with my code, it just dosent move Item in-game

here is my class code:


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 First
{
    class Sprite
    {
        public Texture2D texturee;
        public Rectangle rectan;

        public virtual void Update()
        {

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texturee, rectan, Color.White);
        }

        
    }


    // ---------------------------------------------------------------------Background
    class Background : Sprite
    {
        public Background(Texture2D newTexture, Rectangle newRectangle)
        {
            texturee = newTexture;
            rectan = newRectangle;
        }
    }



    // ----------------------------------------------------------------------Item
    class Item : Sprite
    {
        public Item(Texture2D newTexture, Rectangle newRectangle)
        {
            texturee = newTexture;
            rectan = newRectangle;
        }

        public override void Update()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                rectan.X += 2;
            }

            if(Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                rectan.X -= 2;
            }


        }
    }
}

So if I create more than 1 Item I want it to move also, I don't feel like typing this movement code for every item, so I did it in a Item class, but when running, the Item doesnt move :/

EDITEDITEDIT:

Also, the Item is drawn if bollean == true, but I don't think that has impact on item movement

Advertisement
Step through your code. How many units will your sprite move when you press the right arrow?

Step through your code. How many units will your sprite move when you press the right arrow?


if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
rectan.X += 2;
}

well 2 pixsels (rectan.X += 2;)

EDIT------: Well okay I have embarrassed myself right now hahahahh, I forgot to change Keys.Right on 2nd if statment to Keys,Left after copying thanks

This topic is closed to new replies.

Advertisement