TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Joseph Howard
NA
1
0
Differences between Constructors and Properties
Nov 22 2009 7:09 PM
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
{
// All of these values are passed to each of the derived classes.
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;
// This constructor calls the next constructor while passing all of it's arguements to it.
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)
{
}
// This constructer is automatically called by the first constructer. This contructor only has one extra perameter at the end.
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
{
// "(int)position.X + collisionOffset - 10": shaves 10 pixels off the sides of collisionBox of the object being created.
// When creating the object the collisionOffset variable is used to see where to start drawing the rectangle.
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
;
// These Constructors access all of the variables in the base class. sprite.cs
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
)
{
}
// Same here.
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
)
{
}
// All of these properties must do exactly what the base class defines, and any anything extra can go here. Except for the Update and Draw methods.
public
override
Vector2 Direction
{
get
{
// Creates a variable to store a direction to be passed back to the sprite base class.
Vector2 inputDirection = Vector2.Zero;
GameTime gameTime =
new
GameTime();
// This adds gravity at all times to the player to make sure he's not standing in thin air.
if
(onFloor ==
false
)
{
inputDirection.Y += gravity.Y * 8;
}
else
{
inputDirection.Y = 0;
}
// Simple keyboard checks here, for left, right, up and down arrow keys.
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;
}
// Creates a gamepad state for player one on an Xbox 360 controller.
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// This first if statement checks if the thumbsticks have moved.
// Idle = 0, Left = -1, Right = 1.
// The above values are the maximum values for the thumbsticks.
if
(gamePadState.ThumbSticks.Left.X != 0)
{
inputDirection.X += gamePadState.ThumbSticks.Left.X;
// This takes the value of the thumbsticks and adds it to the X-velocity for side-to-side movement.
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;
}
}
}
// This makes sure that the player is currently on the floor before even being able to move upwards. (This will probably be taken out soon.)
/* if (gamePadState.ThumbSticks.Left.Y != 0 && onFloor == false)
{
inputDirection.Y -= gamePadState.ThumbSticks.Left.Y;
} */
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);
// Move the sprite according to the direction property.
position += Direction;
// If the mouse moved, set the position of the sprite to the mouse position.
MouseState currentMouseState = Mouse.GetState();
if
(currentMouseState.X != prevMouseState.X || currentMouseState.Y != prevMouseState.Y)
{
//position = new Vector2(currentMouseState.X, currentMouseState.Y);
}
prevMouseState = currentMouseState;
// Keeps the sprite inside of the game window.
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.
Reply
Answers (
2
)
Brand new to all of this coding :( Any help at all is very much appreciated
DirectSound problem (Beginner)