4 Replies - 281 Views - Last Post: 25 February 2012 - 01:08 PM Rate Topic: -----

#1 xnaLearner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-November 11

Drop a dragged item inside rect and it will move to center

Posted 24 February 2012 - 03:58 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Drop a dragged item inside rect and it will move to center

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Drop a dragged item inside rect and it will move to center

Posted 24 February 2012 - 09:59 PM

Quote

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.


Sounds like you have the logic for it. Now give a try at coding it.

What have to you tried to code so far? - Click the link

I don't want you to write my code, just give me ideas on how to solve my problem. - Read this article

"It doesn't work" isn't good enough - How you can help us, to help you

What part of what you have tried isn't working?
Are you getting error messages, or is it just not giving you the results you wanted?

(In other words, you at least have to make and show us your good faith effort to do your own coding. We are here to help you, not do it for you.)
What does this error mean?

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

======================================
Please take a try at coding a solution now that you have been given a direction to research. Once you have updated your code if you are still having issues please post in this thread rather than starting a new one.
Was This Post Helpful? 0
  • +
  • -

#3 xnaLearner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-November 11

Re: Drop a dragged item inside rect and it will move to center

Posted 25 February 2012 - 11:26 AM

Hi Guys.....At the min i'm making a 2d board game, and the players can drag and drop images on the screen...However iv stored the position of a rect, and when a letter is dropped inside this rect I would like it to update its position to the center of the tile...please have a look...thanks

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...

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)
        {

        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Drop a dragged item inside rect and it will move to center

Posted 25 February 2012 - 11:38 AM

You already have a thread going for this.
http://www.dreaminco...1&#entry1560052


Please don't open multiple threads for the same problem.

I'm merging these.

Quote

bestover load method for eLetter(int int int int) has invalid arguments)

So what are the values of the arguments you are sending to this method?
Please show us a screenshot of the Locals pallet in Visual Studio showing these values when the method is called.
Was This Post Helpful? 0
  • +
  • -

#5 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Drop a dragged item inside rect and it will move to center

Posted 25 February 2012 - 01:08 PM

Okay, I know there is a method in XNA called Intersects where you can tell if two Rectangles are intersecting. But I'm not sure if that's in an XNA library, or if it's for all Rectangles.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1