1
\$\begingroup\$

So, I have a table. I want to be able to click on a button that allows a mode where I place a sprite on the table, on places that are predefined. How do I do that? I was thinking I could constantly update the mouseState when the button is pressed, and if the mouse is within a ceratin rectangle of a position where you can put it, it will snap to it and if you click, it will create a sprite in that position.

The thing is, I don't really know how to do this in practice.

Some guidance would be great!

\$\endgroup\$
5
  • \$\begingroup\$ You probably don't want to constantly query the mouse state. It is better to wait for a mouse event and update a MousePressed Boolean variable with the state, then handle it in your update function. \$\endgroup\$ Commented May 14, 2013 at 19:47
  • \$\begingroup\$ Hm, alright. Gonna play around for a while. Keep posting answers if you have more ideas. Thanks! \$\endgroup\$ Commented May 14, 2013 at 19:59
  • 1
    \$\begingroup\$ There are no mouse events in XNA, so you'd have to use the containing form to capture mouse events. If you were going to use a pure XNA approach, you would capture the MouseState using Mouse.GetState() in your Update() method and then determine if the mouse buttons were clicked. \$\endgroup\$
    – borrillis
    Commented May 14, 2013 at 20:08
  • \$\begingroup\$ This game uses a similar technique to put 'X's and 'O's on a tic-tac-toe board. github.com/bobfamiliar/MetroTicTacToe \$\endgroup\$
    – borrillis
    Commented May 14, 2013 at 20:18
  • \$\begingroup\$ I've done that now. Now I want it to kind of enter a mode where it let's me put a sprite on one of 3 positions. Not quite sure how to do that. I'd also like it on every one of those locations there would be a semi-transparent sprite of the sprite I'm going to place, and then they disappear when I place the sprite on one of those places, and creates a non-transparent, "real" sprite on that location \$\endgroup\$ Commented May 14, 2013 at 20:30

1 Answer 1

1
\$\begingroup\$

I'm assuming your setup is similar to the following(however if it is not, you should be able to follow the following explanation)

a crude gui layout

If you want to display place-holder sprites (the faded-out sprites you were talking about) then you need either a list or an array to store the sprites positions, and draw them with the alpha value of your choice.

Add some rects to the chart as well. Make sure their width,height,and position are correct or else you might end up with some weird results in terms of dragging sprites onto the chart.

The logic behind dragging sprites is pretty simple - based on the your description. Let us assume that the 3 circles on the grey panel are sprites. The mouse needs to be able to know when it is hovering over these sprites(or buttons - however you want to think about them). I would recommend making a class that handles the dragging and placing of the 3 sprites for functionality.

Within this class you should define a Rect(meaning Rectangle) for these buttons. This class should also know when the mouse is on the image(meaning if the mouse is within the rect you have created for that image). If the mouse is on the image and the mouse is being clicked (with the left mouse button for example) then you should create a duplicate image of the sprite that has just been clicked. If the sprite is not within any of the Rects on the chart and the mouse button is released you should get rid of the texture. If the mouse is released and the mouse is within on of the rects on the chart then you should "snap" the sprites position to the rects position(maybe even scale the sprite based on the rects position if you want to be safe).

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your answer. I understand the logic, though I'm not sure how to code it yet. I guess I have to learn more abour rectangles and arrays and such \$\endgroup\$ Commented May 15, 2013 at 17:32
  • \$\begingroup\$ There are loads of resources online for learning arrays. Here's a link to get you started: c-sharpcorner.com/UploadFile/mahesh/… As for the rectangles just try to visualize it in your head(or even open up your favorite image editor and make the rectangle). It's all about placement. Make sure to check out the documentation as well. \$\endgroup\$
    – Grey
    Commented May 15, 2013 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .