I am wondering what the best way to make a button in MonoGame is. I tried to look for an answer but none seem to be out there. Is it bad practice to use WinForm buttons? If so, what viable alternatives exist?
I always like making a custom button class, this gives me a lot of flexibility for creating creative buttons.
The class creates a button with a texture and a position x and position y and a unique name, after i've done that i'll check my mouse position and see if it's inside the button or not, if it is inside the button then it can click the button and it will search the button by name and execute the given command :)
Here is an example of my button class: (not the best way, but hey it works perfectly for me)
public class Button : GameObject
{
int buttonX, buttonY;
public int ButtonX
{
get
{
return buttonX;
}
}
public int ButtonY
{
get
{
return buttonY;
}
}
public Button(string name, Texture2D texture, int buttonX, int buttonY)
{
this.Name = name;
this.Texture = texture;
this.buttonX = buttonX;
this.buttonY = buttonY;
}
/**
* #return true: If a player enters the button with mouse
*/
public bool enterButton()
{
if (MouseInput.getMouseX() < buttonX + Texture.Width &&
MouseInput.getMouseX() > buttonX &&
MouseInput.getMouseY() < buttonY + Texture.Height &&
MouseInput.getMouseY() > buttonY)
{
return true;
}
return false;
}
public void Update(GameTime gameTime)
{
if (enterButton() && MouseInput.LastMouseState.LeftButton == ButtonState.Released && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
{
switch (Name)
{
case "buy_normal_fish": //the name of the button
if (Player.Gold >= 10)
{
ScreenManager.addFriendly("normal_fish", new Vector2(100, 100), 100, -3, 10, 100);
Player.Gold -= 10;
}
break;
default:
break;
}
}
}
public void Draw()
{
Screens.ScreenManager.Sprites.Draw(Texture, new Rectangle((int)ButtonX, (int)ButtonY, Texture.Width, Texture.Height), Color.White);
}
}
Related
I'm trying to figure out how to auto scroll a scrollview when a user drags an item to the edge of the screen. I'm really not getting the behavious I expect out of this code. Does anyone have an example or a technique that works?
I have this in my:
onDrag(View v, DragEvent e)
{
case DragAction.Location:
var currentPosition = (int)e.GetX();
var point = GetTouchPositionFromDragEvent(v, e);
System.Diagnostics.Debug.WriteLine($"DragAction.Location from {v.GetType()} => {currentPosition}");
System.Diagnostics.Debug.WriteLine($"DragAction.GLOBAL from {v.GetType()} => {point.X}");
if (point.X > App.AppScreenWidth - 50)
{
_hostScrollView.ScrollToAsync(_hostScrollView.ScrollX + 30, 0, true);
}
if (point.X < 50)
{
_hostScrollView.ScrollToAsync(_hostScrollView.ScrollX - 30, 0, true);
}
}
public static Point GetTouchPositionFromDragEvent(View item, DragEvent e)
{
Rect rItem = new Rect();
item.GetGlobalVisibleRect(rItem);
return new Point(rItem.Left + (int)Math.Round(e.GetX()), rItem.Top + (int)Math.Round(e.GetY()));
}
This also has the knock on effect of only scrolling in one direction strangely and also requires the user to keep moving the item in order to fire the events which leads me to think this is entirely the wrong place to be even trying to do this scrolling.
Any pointers or nudges in the right direction would be really appreciated.
In your Activity
public class MainActivity : AppCompatActivity, View.IOnDragListener,View.IOnScrollChangeListener
private int mScrollDistance;
ScrollView _hostScrollView= FindViewById<ScrollView>(Resource.Id.xxx);
_hostScrollView.SetOnScrollChangeListener(this);
public bool OnDrag(View v, DragEvent e)
{
var action = e.Action;
var scrollView = v as ScrollView;
switch (action)
{
case DragAction.Started:
break;
case DragAction.Location:
int y = Java.Lang.Math.Round(e.GetY());
int translatedY = y - mScrollDistance;
int threshold = 50;
// make a scrolling up due the y has passed the threshold
if (translatedY < threshold)
{
// make a scroll up by 30 px
scrollView.ScrollBy(0, -30);
}
// make a autoscrolling down due y has passed the 500 px border
if (translatedY + threshold > 500)
{
// make a scroll down by 30 px
scrollView.ScrollBy(0, 30);
}
break;
}
return true;
}
public void OnScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
var scrollView = v as ScrollView;
mScrollDistance = scrollView.ScrollY;
}
You could modify the logic as you want if I misunderstand your requirement .
As the title suggests I am trying to pass mouse events from one control to another. I'm still a bit new to c# and not even sure if it's possible. I am creating a basic card game, and when I click the mouse down over the deck, I want to pop a card out and drag it without lifting the mouse button. Below is the code for the deck which is on my Main Form
public partial class Form1 : Form
{
int CardWidth = 63;
int CardHeight = 88;
public List<Card> cards = new List<Card>();
public List<Card> deck = new List<Card>();
public Form1()
{
InitializeComponent();
loadCards();
}
private void loadCards()
{
for(int i = 0; i < 10; i++)
{
cards.Add(new Card(i*CardWidth, CardHeight * 0, CardWidth, CardHeight,this));
}
deck.AddRange(cards);
updateDeck();
}
public void updateDeck()
{
Console.WriteLine($"Deck has {deck.Count} cards in it!");
int min = new[] { 4, deck.Count}.Min();
if(deck.Count == 1)
{
pbDeck.Image = Properties.Resources.CardBackDefault;
}
if (deck.Count > 1)
{
switch (min)
{
case 2:
pbDeck.Image = Properties.Resources.Deck2;
break;
case 3:
pbDeck.Image = Properties.Resources.Deck3;
break;
case 4:
pbDeck.Image = Properties.Resources.Deck4;
break;
}
}
if (deck.Count <= 0)
{
pbDeck.Image = Properties.Resources.DeckEmpty;
}
}
private void pbDeck_MouseDown(object sender, MouseEventArgs e)
{
//Pickup Top card if exists
if (deck.Count > 0)
{
//Get last value in Deck List (a.k.a. Last card placed in Deck
Card card = deck.Last();
//Set the card's location to the deck's location
card.Location = pbDeck.Location;
//Load the Card
card.loadCard();
//Remove the card from the deck
deck.Remove(card);
//Update the deck image
updateDeck();
card.card_MouseDown(sender, e);
return;
}
}
}
This is the Card Class
//TODO: May need to be revisited how this value is accessed and modified
public Point Location { get; set; }
//Our Card's visual canvas
public PictureBox card;
//Reference to our main Form
private Form1 Form;
//How we know if the card has been picked up
public bool isMoving = false;
public Card(int X, int Y, int w, int h, Form1 form)
{
Location = new Point(X, Y);
Form = form;
card = new PictureBox();
card.Image = Properties.Resources.CardDefaultTemplate;
card.Location = Location;
card.Name = "card";
card.Size = new Size(w, h);
card.SizeMode = PictureBoxSizeMode.StretchImage;
card.TabIndex = 0;
card.TabStop = false;
card.MouseDown += new MouseEventHandler(card_MouseDown);
card.MouseMove += new MouseEventHandler(card_MouseMove);
card.MouseUp += new MouseEventHandler(card_MouseUp);
card.Paint += new PaintEventHandler(card_Paint);
}
public void loadCard()
{
//Set the location of the card just in case it's changed
card.Location = Location;
//Add this object to the Form
Form.Controls.Add(card);
//Bring it in front of all other items on the form
card.BringToFront();
}
public void removeCard()
{
//Remove card from form
Form.Controls.Remove(card);
}
public void card_MouseDown(object sender, MouseEventArgs e)
{
//Let everyone know we're moving
isMoving = true;
//Bring card to frint of screen for visibility
card.BringToFront();
}
public void card_MouseMove(object sender, MouseEventArgs e)
{
//If we're moving (a.k.a. Mouse is is down on the card)
if (isMoving)
{
//Move the card center to mouse location
card.Location = Form.PointToClient(new Point(Cursor.Position.X - (card.Width / 2), Cursor.Position.Y - (card.Height / 2)));
}
}
private void card_MouseUp(object sender, MouseEventArgs e)
{
//Save the initial locations of the card
int setX = card.Location.X;
int setY = card.Location.Y;
//Let everyone know we're not moving anymore
isMoving = false;
//If the card is past the right boundry
if(setX+card.Width > Form.Width)
{
//Move it back within boundry
setX = Form.Width - card.Width;
}
//If the card is past the left boundry
else if (setX < 0)
{
//Move it back within boundry
setX = 0;
}
//If the card is past the lower boundry
if (setY+card.Height > Form.Height)
{
//Move it back within boundry
setY = Form.Height-card.Height;
}
//If the card is past the upper boundry
else if (setY < 0)
{
//Move it back within boundry
setY = 0;
}
//Set final location
card.Location = new Point(setX, setY);
//If the final location is over the deck image on the form
if (overDeck())
{
//Add this card to the deck
Form.deck.Add(this);
//Update the deck image
Form.updateDeck();
//Remove the card from the form
removeCard();
}
}
private bool overDeck()
{
//If the card's center X value is within the left and right boundries of the deck image on the form
if (card.Location.X + (card.Width / 2) > Form.pbDeck.Location.X && card.Location.X + (card.Width / 2) < Form.pbDeck.Location.X + Form.pbDeck.Width)
{
//If the cards center Y value is within the left and right boundries of the deck image on the form
if (card.Location.Y + (card.Height / 2) > Form.pbDeck.Location.Y && card.Location.Y + (card.Height / 2) < Form.pbDeck.Location.Y + Form.pbDeck.Height)
{
return true;
}
}
//If the above is not true
return false;
}
The cards drag as I intended when out of the deck, but not when I pop them from the deck. Currently they will follow the cursor if I lift off the mouse button, and will drop when clicked again, which is not the desired result
Edit: Added more code for clarity. Changed Title to a more specific Title
Edit 2: Added missing variables from Card Class
I'm not going to try to figure out how you should incorporate this into your code, but the property you are seeking to transfer the mouse input to another control is the Control.Capture Property.
This is boolean that you can set to true for the card PictureBox that you want to respond to moving the mouse. You can set this in another control's MouseDown event handler.
To demonstrate, create a new WinForm project and add a Label and PictureBox control to the form. Wire-up the following event handlers and run the program. Click down on the label and drag the mouse. The PictureBox will follow the cursor.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Location = this.PointToClient(pictureBox1.PointToScreen(e.Location));
}
private void label1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Capture = true;
}
I am currently creating a 2d platformer in monogame.
I created a block that, when hit by the player, starts dissapearing. When it dissapears, i draw a rectangle around it. Every Tile of the same type (BrittleTile) also starts dissapearing, untill the entire connected mob of BrittleTiles has dissapeared.
The problem is that for every BrittleTile that is destroyed, my game runs noticably slower, until it becomes a slideshow after 10 or so BrittleTiles destroyed. I have no idea as to what may cause this, i've been trying to ease the Update method of the class but nothing seems to help.
Any idea as to what may cause this?
class BrittleTile: Tile, IIncludeSound
{
public Rectangle DestroyedCheckRectangle;
private BrittleTile brittle;
private bool _isPlayed;
private bool _hasBroken;
private SoundEffect _sfxBreak;
private SoundEffectInstance _sfxiBreak;
private TimeSpan _breakTimer = new TimeSpan();
public Rectangle BrokenViewRectangle { get; set; }
private bool _isBreaking = false;
public BrittleTile(Texture2D texture, Rectangle baseViewRectangle, Rectangle brokenViewRectangle):base(texture, baseViewRectangle, true )
{
this.BrokenViewRectangle = brokenViewRectangle;
this.ViewRectangle = baseViewRectangle;
}
public void Update(GameTime gameTime, Hero hero, Entity[,] grid)
{
if (!this._hasBroken)
{
if (!this._isBreaking && !this._hasBroken && this.hasCollision)
_checkCollision(hero, grid);
if (this._isBreaking)
{
if (!this._isPlayed)
_sfxiBreak.Play();
this._isPlayed = true;
this._breakTimer += gameTime.ElapsedGameTime;
if (this._breakTimer.TotalMilliseconds < 250)
this.GhostMode(gameTime);
else
{
this._isBreaking = false;
this.hasCollision = false;
this._breakTimer -= this._breakTimer;
}
}
else
{
if (!this.hasCollision && this.DestroyedCheckRectangle.Width == 0)
{
this.DestroyedCheckRectangle.X = this.DestinationRectangle.X - 10;
this.DestroyedCheckRectangle.Y = this.DestinationRectangle.Y - 10;
this.DestroyedCheckRectangle.Height = this.DestinationRectangle.Height + 20;
this.DestroyedCheckRectangle.Width = this.DestinationRectangle.Width + 20;
this._hasBroken = true;
}
}
}
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(this.Texture, this.DestinationRectangle, this.BrokenViewRectangle, Color.White);
if (this.hasCollision)
spriteBatch.Draw(this.Texture, this.DestinationRectangle, this.ViewRectangle, Color.White * this.GhostDraw * 0.9f);
spriteBatch.Draw(this.Texture, DestroyedCheckRectangle, new Rectangle(1, 1, 1, 1), Color.Yellow);
Console.WriteLine(this.DestroyedCheckRectangle.X + ", " + this.DestroyedCheckRectangle.Y + ", " + this.DestroyedCheckRectangle.Width + ", " + this.DestroyedCheckRectangle.Height);
}
private void _checkCollision(Hero hero, Entity[,] grid)
{
if (this.DestinationRectangle.Intersects(hero.AttackHitBox))
{
this._isBreaking = true;
}
foreach (Shuriken star in hero.Stars)
{
if (this.DestinationRectangle.Intersects(star.DestinationRectangle))
this._isBreaking = true;
}
foreach (Entity entityObject in grid)
{
if (hasCollision && entityObject.GetType() == typeof(BrittleTile)){
brittle = entityObject.DeepCopy() as BrittleTile;
if (this.DestinationRectangle.Intersects(brittle.DestroyedCheckRectangle))
this._isBreaking = true;
}
}
}
override public void LoadSounds(ContentManager content)
{
this._sfxBreak = content.Load<SoundEffect>("SFX/brittleBreak");
_sfxiBreak = _sfxBreak.CreateInstance();
_sfxiBreak.Volume = 0.2f;
}
}
First, this line:
if (!this._isBreaking && !this._hasBroken && this.hasCollision)
the !this._hasBroken is superfluous.
My first warning sign is this line:
brittle = entityObject.DeepCopy() as BrittleTile;
I assume DeepCopy() makes a new version of the object, and copies all it's properties, right? Seeing the code of that might help pin it down, but on that assumption...
For every Tile, you're cycling through every object in you're grid and you're completely duplicating that object as a BrittleTile, why?
My first change would be to modify that foreach to have this within it:
var brittle = entityObject as BrittleTile
if (brittle != null && hasCollision){
if (this.DestinationRectangle.Intersects(brittle.DestroyedCheckRectangle))
this._isBreaking = true;
}
}
Not sure this is the primary cause of the slowdown, but it is definitely an issue. If you're cloning an object and then immediately throwing an object away, you're almost certainly doing something wrong. If you're cloning an object and you're not editing the properties of that copy at all (and using those edits), you're definitely doing something wrong. I'd be wary before using a method like that unless you're really sure that's what you want.
Hi,
Im working on Visual Studio Winforms and I'm trying to build tic-tac-toe.
I got stuck in the Check if player win method.
I have tried to do that in activated. little example:
private void StartForm_Activated(object sender, EventArgs e)
{
if ((_pcb1.Image == imagecircle) && (_pcb2.Image == imagecircle) && (_pcb3.Image == imagecircle))
{
MessageBox.Show("You Win!");
}
if ((_pcb4.Image == imagecircle) && (_pcb5.Image == imagecircle) && (_pcb6.Image == imagecircle))
{
MessageBox.Show("You Win!");
}
if ((_pcb7.Image == imagecircle) && (_pcb8.Image == imagecircle) && (_pcb9.Image == imagecircle))
{
MessageBox.Show("You Win!");
}
}
(I know that there are more situations to win).
It's never get into the method, I trying to find a method that active always when the form is open. please help :)
You should check player's victory after each move of that player. I assume, that your players make their moves by clicking on "Make a Move" button or clicking on the game field. Also I assume, that you have Click event of your button or game field. So you should paste your check code in Click event of your button or game field.
Here is some code that will extend to whatever game size:
public class Level
{
private readonly Team[,] _game;
public Level(int extent)
{
_game = new Team[extent, extent];
}
public bool HasWon(Team team)
{
var yMax = GetYMax();
var xMax = GetXMax();
var won = false;
// check horizontally
for (var y = 0; y < yMax; y++)
{
won = false;
for (var x = 0; x < xMax; x++)
won |= _game[y, x] == team;
if (won) return true;
}
// TODO check vertically
// TODO check diagonally
return won;
}
public void SetTile(Team team, int x, int y)
{
var xMax = GetXMax();
var yMax = GetYMax();
if ((x < 0) || (x > xMax))
throw new ArgumentOutOfRangeException("x");
if ((y < 0) || (y > yMax))
throw new ArgumentOutOfRangeException("y");
_game[y, x] = team;
}
private int GetXMax()
{
var xMax = _game.GetUpperBound(1);
return xMax;
}
private int GetYMax()
{
var yMax = _game.GetUpperBound(0);
return yMax;
}
}
public enum Team
{
Red,
Blue
}
Things for you to do:
implement logic for checking if a team has won vertically or diagonally
implement your game using this class, drawing pictures according the array _game (make it available publicly)
study the | OR operator to understand how it's used in here : https://msdn.microsoft.com/en-us/library/6373h346.aspx
further debug the code (I haven't :)
If you want to find some method that is constantly called, so as to check whether the players have won, you can just create a Timer that runs every 1 millisecond. Then the event handler for Timer.Tick would be such a method.
However, you shouldn't find such a method to check for winning/losing. It's a waste of resources to check for that every 1 millisecond.
Think, what action can affect the result of a game of tic tac toe? It's of course when either player places something (X or O) on the board! That is when you need to check whether someone has won. Also, if the thing placed is a X, you don't need to check whether the player who is playing O has won, because that's simply impossible!
Also, checking whether the image in a picture box is equal to the X or O image is really "non abstract" code. Try to put these things into a model by abstraction.
I was looking for a way to block other object from the same class to be dragged, when I begin dragging one and hover another it id dragged as well...
Here is my main item definition,
class DrawableObject
{
public Rectangle RectObject
public void Update(GameTime gameTime, MouseState ms)
{
if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
if (RectObject.Intersects(new Rectangle(ms.X, ms.Y, 0, 0)))
Dragged = true;
else
Dragged = false;
if (Dragged)
RectObject = new Rectangle(ms.X - RectObject.Width / 2, ms.Y - RectObject.Height / 2, RectObject.Width, RectObject.Height);
}
}
The issue will be more tight as I will be creating more than 50 instance of this class.
You could keep a "global" property with the current object being dragged.
Then before accepting dragging each object will check if another is not yet dragged:
public void Update(GameTime gameTime, MouseState ms)
{
if (currentObject != null && this != currentObject) return;
if ((ButtonState.Pressed == Mouse.GetState().LeftButton))
{
if (RectObject.Intersects(new Rectangle(ms.X, ms.Y, 0, 0)))
{
Dragged = true;
currentObject = this;
}
}
else
...
}