-2
\$\begingroup\$

I'm new to XNA and wondering if it is possible to remove a line (that lies between two vectors) when the mouse clicks on that line. I've searched everywhere but I can't seem to find anything.

My extension method in class CreateLine:

public static class CreateLine
{
    public static void DrawLine(this SpriteBatch spriteBatch, Texture2D texture, Vector2 startVector, Vector2 endVector, Color color)
    {
float length = Vector2.Distance(startVector, endVector);
        float angle = (float)(Math.Atan2(startVector.Y - endVector.Y, startVector.X - endVector.X));
     spriteBatch.Draw(texture, new Rectangle((int)startVector.X, (int)startVector.Y, length, 3),
              null, color, angle, new Vector2(0, 0), SpriteEffects.None, 0);   
    }
}

Texture2D boxSprite;
Vector2 sVector1 = new Vector2(100, 200);
Vector2 eVector1 = new Vector2(100, 400);
Vector2 sVector2 = new Vector2(240, 300);
Vector2 eVector2 = new Vector2(350, 300);
Vector2 sVector3 = new Vector2(200, 100);
Vector2 eVector3 = new Vector2(200, 150);
Vector2 sVector4 = new Vector2(500, 90);
Vector2 eVector4 = new Vector2(390, 90);
MouseState previousMouseState;
MouseState currentMouseState = Mouse.GetState();

In the LoadContent() method:

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    boxSprite = Content.Load<Texture2D>("boxSprite"); //// this is a 1x1 white box
 }

In the Draw() method

protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();
    spriteBatch.DrawLine(boxSprite, sVector1, eVector1, Color.Yellow);
    spriteBatch.DrawLine(boxSprite, sVector2, eVector2, Color.Orange);
    spriteBatch.DrawLine(boxSprite, sVector3, eVector3, Color.Green);
    spriteBatch.DrawLine(boxSprite, sVector4, eVector4, Color.Pink);
    spriteBatch.End();
    base.Draw(gameTime);
}

Now I am currently trying to remove each line if I click on it using the mouse.

protected override void Update(GameTime gameTime) 
{
    mouseState = Mouse.GetState();
    Rectangle sV1Rectangle = new Rectangle(sVector1.X, sVector1.Y, boxSprite.Width, boxSprite.Height);`

The rectangle is supposed to contain this line in the draw method >>>> spriteBatch.DrawLine(boxSprite, sVector1, eVector1, Color.Yellow);

if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released)
{  if (sV1Rectangle.Contains(mouseState.X, mouseState.Y)
  {  
     // HOW DO I REMOVE THE LINE I JUST CLICKED ON???
     // HOW DO I REFER TO THIS LINE >>>> spriteBatch.DrawLine(boxSprite, sVector1, eVector1, Color.Yellow); OR ANY OTHER LINE I CLICK ON?????
  } 
}

    previousMouseState == currentMouseState;
    base.Update(gameTime);
}

I was going to use the list.remove/removeAt() method remove the sprite(s), but I couldn't because I am using vector2s to draw the line(s).

Can someone give me advice?

\$\endgroup\$
  • \$\begingroup\$ I don't understand. Are you asking how to detect lines that lie between two other lines? Or how to stop them from being drawn? How does your game store "lines"? \$\endgroup\$ – Anko Mar 31 '14 at 15:14
  • \$\begingroup\$ hey Anko, I've implemented my question, Hopefully it makes more sense :S \$\endgroup\$ – sam Mar 31 '14 at 17:17
  • \$\begingroup\$ Seeing how you're doing something is unhelpful if I don't yet understand what you're trying to do. What are you trying to do? \$\endgroup\$ – Anko Mar 31 '14 at 17:34
  • \$\begingroup\$ I'm trying to stop the lines from being drawn after I click on them using the mouse \$\endgroup\$ – sam Mar 31 '14 at 19:20
  • \$\begingroup\$ This question appear to be off topic because it is asking for a specific implementation of a project-specific problem. \$\endgroup\$ – Seth Battin Apr 4 '14 at 14:09
0
\$\begingroup\$

I am not sure if you are trying to draw sprites (images) or points, but for sprites you should use the spriteBatch.Draw(,,); function, where the first parameter points to a Texture2D object, the second to a Vector2 and the third to a color for further effects. The color can be taken from the Colors item, which is built into the XNA, normally you would put Colors.White for no effect on the sprite. You should be able to find all the documentation in the XNA 2D tutorial in MSDN: http://msdn.microsoft.com/en-us/library/bb203893.aspx

Drawing single pixels turns out to be harder than it seems, as you can see on this thread: http://social.msdn.microsoft.com/Forums/en-US/dd40c33d-c617-4eef-85ad-9d9eaca4e86a/drawing-just-one-pixel
However you could create a 1x1 sprite and then draw that, maybe a white one so you could reuse it for every occasion by changing the color parameter of the Draw method.

Now, if you are trying to remove an already existing line, I am pretty sure that it is being redrawn in every frame, so maybe you just have to put a condition in the code that will just not draw it? I really can't guess what you have in mind so maybe if you could tell us what's the nature of this part of your project someone could give you an exact answer.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the answer! yes that's exactly what I have done, I used the a 1x1 white sprite and drew it using the extension method DrawLine, I used the Drawline method because it allowed me to increase the thickness of the line. I want to delete the line after the mouse clicks on it. \$\endgroup\$ – sam Mar 31 '14 at 12:55
  • \$\begingroup\$ However, I am unsure of how to go refer to each line (in the Update method?) if I have Drawn the sprites this way in the game1 class. protected override void Draw(GameTime gameTime) {spriteBatch.Begin(); spriteBatch.DrawLine(boxSprite, StartVector, endVector, Color.Yellow); spriteBatch.DrawLine(boxSprite, StartVector, endVector, Color.Green); spriteBatch.DrawLine(boxSprite, StartVector, endVector, Color.Green); spriteBatch.DrawLine(boxSprite, StartVector, endVector, Color.Yellow); } \$\endgroup\$ – sam Mar 31 '14 at 13:05
  • \$\begingroup\$ Ok, how about this, are you familiar with lists in C#? try making a list of lines (create a custom class or equivalent) and render all the lines in the list, then you just have to remove the line the player has clicked on. I could dig up some equations we used for finding the distance from a point to a line if you want so you can remove any line that's 1 or 2 pixels away from the mouse. \$\endgroup\$ – user3079666 Mar 31 '14 at 19:17
  • \$\begingroup\$ Yes please I would be vey grateful. Yes I'm familiar with lists in c#, can you give me an example of how to render the lines in a list if I'm reusing the same texture and two vectors? \$\endgroup\$ – sam Mar 31 '14 at 19:41
  • \$\begingroup\$ I do not dare to give the exact code since I have not rendered lines before, but the general idea would be that you have a class containing two vectors, let's call it "myLine", you create a list<myLine>,then you will go through it with the foreach loop when you get an LMB message,this is the actual equation that gives you the distance: [link]en.wikipedia.org/wiki/Distance_from_a_point_to_a_line , the line that is produced from the vector is lx + c - y = 0, where l=(end.x-start.x)/(end.y-start.y)*x c=start.y/start.x. I hope that works,all that's left is to remove the line from the list \$\endgroup\$ – user3079666 Mar 31 '14 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.