This site requires JavaScript, please enable it in your browser!
Greenfoot back
Octavius
Octavius wrote ...

2016/9/23

Dragging objects with mouse in-game

Octavius Octavius

2016/9/23

#
Hello, I am planning on making a SSBB fan game. If you look at the character select in SSBB, you will see that you have to drag and drop a little circle on the character you want to select. How would I make an object that I can drag and drop? Also, I think I have an idea of how to have the dragging and dropping actually select the character, but please, if you have any ideas, show me how I can select a character with the drag and drop object.
danpost danpost

2016/9/23

#
Dragging, in game, is as simple as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// mouse dragging
if (Greenfoot.mouseDragged(this))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    setLocation(mouse.getX(), mouse.getY());
}
// locking onto selection (or not)
if (Greenfoot.mouseDragEnded(this))
{
    Actor choice = getOneIntersectingObject(< name of choice class >.class);
    if (choice != null) setLocation(choice.getX(), choice.getY());
    // optional else block
    else setLocation(< initial no choice location >);
}
You could act on the selection immediately, but probably better is to have a button to confirm and proceed which is clicked after the selection is made. That way the user would have a chance to change his or her mind about which selection to make.
You need to login to post a reply.