Jump to content
  • Advertisement
Sign in to follow this  
Bruno Filip

Move Item via class code

This topic is 962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by Bru Flu

Share this post


Link to post
Share on other sites
Advertisement

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 

Edited by Bru Flu

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!