I would like to know the difference between Constructors and Properties. All I know is that a constructor is used to create an instance of the object, but my question is
how?
I'm trying to understand the whole concept of object-oriented programming and I just can't seem to get the grasp on properties. I know how to use methods, but I would like to know how they differ from properties. I've noticed when I use properties, I need to have a "get" and or "set." But These 2 keywords I don't understand how they work. I'll post a copy of my code. It works and does what it supposed to do, but I don't understand how it is getting it's information. Here's a copy of the code:
This is the base class called Sprite.
- namespace WindowsGame1
- {
- abstract class Sprite
- {
-
- Texture2D textureImage;
- public Vector2 position;
- protected Point frameSize;
- protected Vector2 speed;
- int collisionOffset;
- Point currentFrame;
- Point sheetSize;
- int timeSinceLastFrame = 0;
- int millisecondsPerFrame = 10;
- public string collisionCueName { get; private set; }
- const int defaultMillisecondsPerFrame = 8;
-
-
- public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, string collisionCueName)
- : this(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, defaultMillisecondsPerFrame, collisionCueName)
- {
- }
-
-
- public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame, string collisionCueName)
- {
- this.textureImage = textureImage;
- this.position = position;
- this.frameSize = frameSize;
- this.collisionOffset = collisionOffset;
- this.currentFrame = currentFrame;
- this.sheetSize = sheetSize;
- this.speed = speed;
- this.collisionCueName = collisionCueName;
- this.millisecondsPerFrame = millisecondsPerFrame;
- }
-
- public virtual void Update(GameTime gameTime, Rectangle clientBounds)
- {
- 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;
- }
- }
- }
- }
-
- public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(textureImage, position, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
- }
-
- public abstract Vector2 Direction
- {
- get;
- }
-
- public Rectangle CollisionRect
- {
- get
- {
-
-
- return new Rectangle((int)position.X + collisionOffset - 10, (int)position.Y + collisionOffset, frameSize.X - (collisionOffset * 2), frameSize.Y - (collisionOffset * 2));
- }
- }
-
- public bool IsOutOfBounds(Rectangle clientRect)
- {
- if (position.X < -frameSize.X || position.X > clientRect.Width || position.Y < -frameSize.Y || position.Y > clientRect.Height)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public Vector2 GetPosition
- {
- get { return position; }
- }
- }
- }
And here is the derived class called userControlledSprite:
- namespace WindowsGame1
- {
- class UserControlledSprite : Sprite
- {
- MouseState prevMouseState;
- protected Vector2 velocity = new Vector2(0, -5);
- protected Vector2 prevVelocity = new Vector2 (0, -5);
- protected Vector2 acceleration = new Vector2(0, -0.10f);
- protected Vector2 gravity = new Vector2(0, 0.30f);
- protected int timeSinceLastJump = 0;
- bool onFloor = false;
-
-
- public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
- : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, null)
- {
- }
-
-
- public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame)
- : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, millisecondsPerFrame, null)
- {
- }
-
-
- public override Vector2 Direction
- {
- get
- {
-
- Vector2 inputDirection = Vector2.Zero;
- GameTime gameTime = new GameTime();
-
-
- if (onFloor == false)
- {
- inputDirection.Y += gravity.Y * 8;
- }
- else
- {
- inputDirection.Y = 0;
- }
-
-
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- {
- inputDirection.X -= 1;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Right))
- {
- inputDirection.X += 1;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Up))
- {
- inputDirection.Y -= 1;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Down))
- {
- inputDirection.Y += 1;
- }
-
-
- GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
-
-
-
-
- if (gamePadState.ThumbSticks.Left.X != 0)
- {
- inputDirection.X += gamePadState.ThumbSticks.Left.X;
-
-
- velocity.X += gamePadState.ThumbSticks.Left.X;
-
- if (velocity.X > 0.5f || velocity.X < -0.5f)
- {
- if (gamePadState.ThumbSticks.Left.X > 0)
- {
- velocity.X = 0.5f;
- }
- if (gamePadState.ThumbSticks.Left.X < 0)
- {
- velocity.X = -0.5f;
- }
- }
- }
-
-
-
-
-
-
-
- if (gamePadState.Buttons.A == ButtonState.Pressed && timeSinceLastJump >= 500)
- {
- onFloor = false;
-
- if (velocity.Y >= prevVelocity.Y)
- {
- velocity = prevVelocity;
- }
-
- inputDirection.X += velocity.X;
- inputDirection.Y += velocity.Y;
- velocity += acceleration;
-
- timeSinceLastJump = 0;
- }
- else if (gamePadState.Buttons.A == ButtonState.Released)
- {
- inputDirection.Y += gravity.Y;
- }
-
- if (gamePadState.Buttons.B == ButtonState.Pressed)
- {
- inputDirection.X += gamePadState.ThumbSticks.Left.X * 3;
- inputDirection.Y -= gamePadState.ThumbSticks.Left.Y * 3;
- GamePad.SetVibration(PlayerIndex.One, 1, 1);
- }
- else
- {
- GamePad.SetVibration(PlayerIndex.One, 0, 0);
- }
-
- return inputDirection * speed;
- }
- }
-
- public void collision(Rectangle player, Rectangle otherObject, GraphicsDevice graphicsDevice)
- {
- if (player.Intersects(otherObject) || player.Bottom >= graphicsDevice.PresentationParameters.BackBufferHeight)
- {
- onFloor = true;
- position.X = position.X;
- position.Y = otherObject.Y - frameSize.Y;
- }
- if (player.Bottom > otherObject.Top - 1)
- {
- onFloor = false;
- }
- }
-
- public override void Update(GameTime gameTime, Rectangle clientBounds)
- {
- timeSinceLastJump += gameTime.ElapsedGameTime.Milliseconds;
- GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
-
-
- position += Direction;
-
-
- MouseState currentMouseState = Mouse.GetState();
-
- if (currentMouseState.X != prevMouseState.X || currentMouseState.Y != prevMouseState.Y)
- {
-
- }
-
- prevMouseState = currentMouseState;
-
-
- if (position.X < 0)
- {
- position.X = 0;
- }
- if (position.X > clientBounds.Width - frameSize.X)
- {
- position.X = clientBounds.Width - frameSize.X;
- }
- if (position.Y < 0)
- {
- position.Y = 0;
- }
- if (position.Y > clientBounds.Height - frameSize.Y)
- {
- position.Y = clientBounds.Height - frameSize.Y;
- }
-
- base.Update(gameTime, clientBounds);
- }
- }
- }
I would just like an explanation of how the get and set methods are working. I was following a tutorial when I wrote this code, but I have modified a little of it myself. Any help would be appreciated.