Hey guys...Im creating a board game and atm I can drag/drop the letters on tiles(on the board)...but what I want to know is how if i drop the letter on the tile, how can i get the letter to automatically position itself in the center of the tile (onthe board). So i can drop it anywher inside the tile and it will appear to drag itself into the center.
Is this done using rectangles for each tile, then find the center point of each rectangle and if the letter is dropped inside this particular tile then move it to the center.
I think i understanf the logic could anyone give me a startup on it or even point me in the direction of a tutorial ot two that uses this?
Thanks again
Loading
john Mc kennaPosted Feb 25, 2012, 1:14 PM
I think i'm nearly there but im stuck on a problem...
Ive used your coding in my centerLetterOnTile() method...If you take a look you will see I think I am nearly there...however
instead of saying eLetter = new Rectangle(50,50, 50, 50);
I need it to say something like eLetter = new Rectangle(clickablePlayer.Position.X, clickablePlayer.Position.Y, 50, 50);
as my clickablePlayer will be the texture which is being dragged around the screen(i.e the letter)
but when i try this I get an error the bestover load method for eLetter(int int int int) has invalid arguments).....Iv'e included my clickablePlayer class to see if it is any help...
Thanks in advance for any help...P.S is this the normal format for posting on this site or do you have a copy to display the code correctly?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace Scrabble
{
public enum ObjectClickedState
{
Normal,
Clicked,
Released
}
public abstract class ClickableGameplayObject : GameplayObject
{
public static bool OnLeaveTest=false;
public static bool OnLeaveTestRect = false;
ClickablePlayer clickablePlayer;
//int resize = 12;
// public Vector2 resize//used for hud
// {
// get;
// protected set;
// }
Vector2 resize = new Vector2(15, 15);
Point resize1 = new Point(15, 15);
//declaring point and rect
Point p1;
Rectangle eTile, eLetter;
public ObjectClickedState LeftState
{
get;
protected set;
}
public ObjectClickedState RightState
{
get;
protected set;
}
public Mouse ActiveMouse
{
get;
protected set;
}
public ClickableGameplayObject()
: base()
{
LeftState = RightState = ObjectClickedState.Normal;
}
//testing lines to see if onleace works
public virtual void OnLeftClick()
{
OnLeaveTest = false;
}
public virtual void OnHeldLeftClick() { }
public virtual void OnLeftRelease()
{
// OnLeaveTest = true;
}
public virtual void OnLeftNormal() { }
public virtual void OnRightClick() { }
public virtual void OnHeldRightClick() { }
public virtual void OnRightRelease() { }
public virtual void OnRightNormal() { }
public virtual void OnHover() { }
public virtual void OnLeave()
// public override void OnLeave()
{//now add code to //check game board position.
OnLeaveTest = true;
}
public Rectangle TestLetterDropRect
{
get;
set;
}
private void centerLetterOnTile()
{
eTile = new Rectangle(651, 372, 50, 50);
p1 = eTile.Location;
//eLetter = new Rectangle(clickablePlayer.Position.X, clickablePlayer.Position.Y, 50, 50);
//eLetter = new Rectangle(eTile.X, eTile.Y, 50, 50);
eLetter = new Rectangle(50,50, 50, 50);
if (eTile.Contains(eLetter) && OnLeaveTest == true)
{
eLetter.Location = eTile.Location;
}
else
{
OnLeaveTestRect = false;
}
//this code is commented out as i was trying to do it a different way, as eLetter is going to be the clickablePlayer
//but when i tried this code the line //clickablePlayer.Position = new Vector2(100, 100); was getting a null for clickablePlayer
/* //this is the current x and y on mouse position
Level.x = Level.currentState.X;
Level.y = Level.currentState.Y;
TestLetterDropRect = new Rectangle(651, 372, 50, 50);
//if rect contains mouse pointer(draggin lette) and mouse releases letter and moves outside the letter...the letter should move to the center of tile rect
if (!TestLetterDropRect.Contains(Level.x, Level.y) && OnLeaveTest == true)
{
OnLeaveTestRect = true;
//letter new position = rectangle /2
//resize is an int of size 12.
//clickablePlayer.Position = new Vector2(100, 100);
//clickablePlayer.Position = TestLetterDropRect.Y + resize;
}*/
}
public void Update(GameTime gameTime, Mouse activeMouse)
{
base.Update(gameTime);
centerLetterOnTile();
bool intersect = activeMouse.Rectangle.Intersects(Rectangle);
if (intersect || (ActiveMouse != null &&
(LeftState != ObjectClickedState.Normal || RightState != ObjectClickedState.Normal)))
{
ActiveMouse = activeMouse;
ActiveMouse.ClickedObject = this;
if (intersect) OnHover();
if (ActiveMouse.LeftClick && (LeftState == ObjectClickedState.Normal
|| LeftState == ObjectClickedState.Released))
{
LeftState = ObjectClickedState.Clicked;
OnLeftClick();
}
else if (ActiveMouse.LeftClick && LeftState == ObjectClickedState.Clicked)
{
OnHeldLeftClick();
}
else if (ActiveMouse.LeftRelease && LeftState == ObjectClickedState.Clicked)
{
LeftState = ObjectClickedState.Released;
OnLeftRelease();
}
else if (!ActiveMouse.LeftClick && LeftState == ObjectClickedState.Released)
{
LeftState = ObjectClickedState.Normal;
OnLeftNormal();
}
if (ActiveMouse.RightClick && (RightState == ObjectClickedState.Normal
|| RightState == ObjectClickedState.Released))
{
RightState = ObjectClickedState.Clicked;
OnRightClick();
}
else if (ActiveMouse.RightClick && LeftState == ObjectClickedState.Clicked)
{
OnHeldRightClick();
}
else if (ActiveMouse.RightRelease && RightState == ObjectClickedState.Clicked)
{
RightState = ObjectClickedState.Released;
OnRightRelease();
}
else if (!ActiveMouse.RightClick && RightState == ObjectClickedState.Released)
{
RightState = ObjectClickedState.Normal;
OnRightNormal();
}
}
else
//removes the mouse influence over the object do the folowing...(calls on leave)
{
if (ActiveMouse != null)
{
ActiveMouse.ClickedObject = null;
ActiveMouse = null;
OnLeave();
}
}
UpdateGameplayObject(gameTime);
}
protected abstract void UpdateGameplayObject(GameTime gameTime);
}
}
and...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
namespace Scrabble
{
public class ClickablePlayer : ClickableGameplayObject
{
Random random;
public Texture2D ClickedTexture
{
get;
set;
}
Texture2D saved;
public ClickablePlayer(Texture2D texture)
: base()
{
Alpha = 0.5f;
random = new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds);
Texture = texture;
}
public override void OnHover()
{
base.OnHover();
//Alpha = 1.0f;
// Rotation += 0.01f;
}
public override void OnLeave()
{
base.OnLeave();
Alpha = 0.5f;
}
public override void OnLeftClick()
{
base.OnLeftClick();
Position = new Microsoft.Xna.Framework.Vector2(ActiveMouse.Position.X, ActiveMouse.Position.Y);
}
public override void OnHeldLeftClick()
{
base.OnHeldLeftClick();
Position = new Microsoft.Xna.Framework.Vector2(ActiveMouse.Position.X, ActiveMouse.Position.Y);
}
protected override void UpdateGameplayObject(Microsoft.Xna.Framework.GameTime gameTime)
{
}
}
}
FroglegPosted Feb 24, 2012, 6:28 PM
namespace RectDrop
{
public partial class Form1 : Form
{
bool _mouseDown = false;
Point p1;
Rectangle eTile, eLetter;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
eTile = new Rectangle(100, 100, 50, 50);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_mouseDown = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
pictureBox1.Refresh();
p1 = e.Location;
eLetter = new Rectangle(e.X, e.Y, 50, 50);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_mouseDown = false;
if (eTile.IntersectsWith(eLetter))
{
eLetter.Location = eTile.Location;
pictureBox1.Refresh();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Black, eTile);
e.Graphics.DrawRectangle(Pens.Blue, eLetter);
}
}
}