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

2016/12/13

Selecting an actor class with a mouse click

Boomkin25 Boomkin25

2016/12/13

#
Hello, I'm in a high school coding class and trying to make a VERY basic hearthstone-like game. I'm wondering how could I click the players card basically selecting the card, then click anywhere on the board and get that player card that I selected to move to that position.
danpost danpost

2016/12/13

#
You could add a PlayerHand actor to the world which holds the card and follows the mouse and removes itself when a click occurs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import greenfoot.*;
 
public class PlayerHand extends Actor
{
    Actor charge;
 
    public PlayerHand(Actor charged)
    {
        charge = charged;
        setImage(new GreenfootImage(1, 1);
    }
 
    public void act()
    {
        if (Greenfoot.mouseMoved(null))
        {
            MouseInfo mi = Greenfoot.getMouseInfo();
            charge.setLocation(mi.getX(), mi.getY());
        }
        if (Greenfoot.mouseClicked(null))
        {
            getWorld().removeObject(this);
        }
    }
}
Since the PlayerHand actor actually never moves, it is possible to restrict where the actor places it charge -- and even return the charge back to its original location if needed. If there are restriction on when a card can be selected they can be checked by adding an 'addedToWorld' method to the class and remove itself if the card cannot be currently moved. The class of the card can have this in the act method:
1
if (Greenfoot.mouseClicked(this)) getWorld().addObject(new PlayerHand(this), getX(), getY());
Boomkin25 Boomkin25

2016/12/22

#
When I place the PlayerHand it gives me input bar what do I put into the input bar.
Super_Hippo Super_Hippo

2016/12/22

#
A Card is either in your deck, on your hand, on the board or dead. A dead card probably only has to be tracked in a world array or something (if it is resurrected, it will create a copy of it). I think the world could control the position of the cards in your hand and whenever one is selected and moved around before being played. Oh and you can't create danpost's PlayerHand with right clicking and selecting new PlayerHand.
danpost danpost

2016/12/22

#
Boomkin25 wrote...
When I place the PlayerHand it gives me input bar what do I put into the input bar.
Super_Hippo wrote...
you can't create danpost's PlayerHand with right clicking and selecting new PlayerHand.
That is not true. There are two ways to enter a card for the PlayerHand to move. You can inspect a card first, then enter the name (probably 'card') when creating a PlayerHand object; or, you can double click on the card when the PlayerHand is waiting for input for the parameter. The only "issue" might be that the card will not follow the mouse until the scenario is started.
You need to login to post a reply.