Reputation: 35
How do I clone an object then pick a random position, then draw it.
This is the code I have for the object:
public class Trash : ICloneable
{
private Texture2D _texture;
private float _rotation;
public Vector2 Position;
public Vector2 Origin;
public float RotationVelocity = 3f;
public float LinearVelocity = 4f;
public Trash(Texture2D texture)
{
_texture = texture;
}
public void Update()
{
// Do epic stuff here
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_texture, Position, null, Color.White, _rotation, Origin, 1, SpriteEffects.None, 0f);
}
public object Clone()
{
return this.MemberwiseClone();
}
And this is code I have in Game1.cs so far:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private SeaJam.Objects.Trash Trash;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
var texture = Content.Load<Texture2D>("Prototype");
Trash = new Objects.Trash(texture)
{
Position = new Vector2(100, 100),
Origin = new Vector2(texture.Width / 2, texture.Height - 25),
};
}
protected override void Update(GameTime gameTime)
{
Trash.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Trash.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
private void AddTrash()
{
var rnd = new System.Random();
var NewTrash = Trash.Clone();
}
The problem is Whenever I'd try to give a random position for the clone in the AddTrash() Method, I'd only get errors, such as "'object' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"
Upvotes: 1
Views: 560
Reputation: 2122
your constructor:
public Trash(Texture2D texture)
{
_texture = texture;
}
Needs to be extended with the desired changeable parameters. In your case, it needs to add Position
and Origin
as parameter, and then apply it as a value.
Like this:
public Trash(Texture2D texture, Vector2 position, Vector2 origin)
{
_texture = texture;
Position = position;
Origin = origin;
}
And change the way you call it in the game1.cs as well, they need to work similair like texture
:
var texture = Content.Load<Texture2D>("Prototype");
var position = new Vector2(100, 100),
var origin = new Vector2(texture.Width / 2, texture.Height - 25),
Trash = new Objects.Trash(texture, position, origin);
And as a tip: keep consistency in your field names, mixing in underscore and lowercase in one field, and uppercase in an other field will get confusing to understand. especially when the parameters needs a name different from the fields as well. I prefer to keep them all with the first letter uppercase.
Upvotes: 0