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

2015/4/17

Drag and drop reference issue

xCrunchmanx xCrunchmanx

2015/4/17

#
Hello I'm making a word game that and I'm having issues with references. I have several classes that I want to be able to interact with my Letter class. Below you can see the code for the letter class. So to breakdown the game I have a letter, wordBox(the goal is to get the words here), LetterQueue(If the current top letter of the stack is not the letter you need to solve the word you put the letter in here), LetterStack(When the game is started these are pushed with letter objects and it behaves like a stack), and createLevel(This initializes and stores all the objects, etc..)... I'm unsure as to how I reference the objects that I want to move the letter to. In the letter class you can read that I have a drag and drop method and it just simply will allow you to move the letter object and if you release the mouse on one of the holding objects it just visually changes. I want to know how to say for example in the letter class "if letter is touching Queue and isTop" - "pop the stack that holds this letter and add this letter to the queue.
public class Letter extends Actor
{   //The isSelected variable is so that we can see if we'ere currently holding item and lastStack is so lets can be returned to place
    private boolean isSelected = false;
    public int stack = 0;
    private char letter = ' ';
    int x = 0;
    int y = 0;
    int currentState = 0;
    final int inStack = 0;
    final int inQueue = 1;
    final int inBox = 2;
    boolean isTop = false;
    int[] xCoord = {200, 255, 302, 350, 395};//550 

    
    //^^To see if it's at the top of the stack
    /**
     * Act - do whatever the Letters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     * 
     */
    //use lowercase letters in the parameters**THIS IS WHAT CREATES THE LETTERS AND SETS THE IMAGE*****
    public Letter(char letter,int stack){
        switch(letter){
            case 'a':
            setImage("Star-A.png");
            break;            case 'b':
            setImage("Star-B.png");
            break;            case 'c':
            setImage("Star-C.png");
            break;            case 'd':
            setImage("Star-D.png");
            break;            case 'e':
            setImage("Star-E.png");
            break;            case 'f':
            setImage("Star-F.png");
            break;            case 'g':
            setImage("Star-G.png");
            break;            case 'h':
            setImage("Star-H.png");
            break;            case 'i':
            setImage("Star-I.png");
            break;            case 'j':
            setImage("Star-J.png");
            break;            case 'k':
            setImage("Star-K.png");
            break;            case 'l':
            setImage("Star-L.png");
            break;            case 'm':
            setImage("Star-M.png");
            break;            case 'n':
            setImage("Star-N.png");
            break;            case 'o':
            setImage("Star-O.png");
            break;            case 'p':
            setImage("Star-P.png");
            break;            case 'q':
            setImage("Star-Q.png");
            break;            case 'r':
            setImage("Star-R.png");
            break;            case 's':
            setImage("Star-S.png");
            break;            case 't':
            setImage("Star-T.png");
            break;            case 'u':
            setImage("Star-U.png");
            break;            case 'v':
            setImage("Star-V.png");
            break;            case 'w':
            setImage("Star-W.png");
            break;            case 'x':
            setImage("Star-X.png");
            break;            case 'y':
            setImage("Star-Y.png");
            break;             case 'z':
            setImage("Star-Z.png");
            break;

        }
        this.letter = letter;//This will be used to test if a sentence is correct
        this.stack = stack;//This assigns the stack it will be in
    }
    public void dragAndDrop(){
                if (!isSelected && Greenfoot.mousePressed(this) && currentState != inBox && isTop) // new letter selection
        { // letter is currently unselected and mouse button pressed while over this letter
             isSelected = true; // flags the letter as selected
             MouseInfo mouse = Greenfoot.getMouseInfo();

        
        }
        
        if (isSelected && Greenfoot.mouseDragged(this) ) // follow the mouse
        { // letter is currently selected and the mouse is dragging this letter
            MouseInfo mi = Greenfoot.getMouseInfo(); // gets mouse information
            setLocation(mi.getX(), mi.getY()); // sets location of letter at location of mouse
            return; // exits current execution of this 'act' method
        }
        
        if (isSelected && Greenfoot.mouseDragEnded(this) && currentState == inStack && Letter.this.isTouching(WordBox.class)){ //in Stack and touching wordbox
            currentState = inBox;
            
            isSelected = false; // flags the letter as unselected 
            return; // exits current execution of this 'act' method
        
        }
        
        if (isSelected && Greenfoot.mouseDragEnded(this) && currentState == inStack && Letter.this.isTouching(LetterQueue.class))//in stack and touching queue
        { //Took out the setLocation for this because it needs to be updated in the level folder
            currentState = inQueue;
            
            isSelected = false; // flags the letter as unselected 
            return; // exits current execution of this 'act' method
        
        }
        if (isSelected && Greenfoot.mouseDragEnded(this) && currentState == inQueue && (Letter.this.isTouching(WordBox.class))){//in inque and touching wordbox
            isSelected = false; // flags the letter as unselected 
            return;
        }
        if (isSelected && Greenfoot.mouseDragEnded(this) && currentState == inQueue && Letter.this.isTouching(LetterStack.class))//in que and touching stack
        { //Took out the setLocation for this because it needs to be updated in the level folder
            
            isSelected = false; // flags the letter as unselected 
            return; // exits current execution of this 'act' method
        
        }
        
        if(isSelected && Greenfoot.mouseDragEnded(this) && currentState == inStack){//For if you don't drag to anything 
            Letter.this.setLocation(x,y);
            isSelected = false;
            return;
        }
    }
    
    

    //THIS IS WHERE ALL THE DRAGGING AND DROPPING TAKES PLACE
    public void act() 
    {
        Level level = (Level) getWorld(); // gets a reference to the world
        dragAndDrop();

    
}


}
xCrunchmanx xCrunchmanx

2015/4/17

#
Ignore most of the comments. They are misplaced
Super_Hippo Super_Hippo

2015/4/17

#
Some things: line 139:
Level level = (Level) getWorld(); // gets a reference to the world
You get a reference there but you don't use it I am pretty sure you can replace lines 24 through 79 with this line: (or you use uppercase letters as parameters, then you don't have to change them into uppercase letters)
setImage("Star-"+Character.toUpperCase(letter)+".png");
In the dragAndDrop method, the if conditions often check for the same things at the beginning. You could combine them. This would also makes it easier and faster to understand. How does the int 'stack' refer to an object of type 'LetterStack'? I mean, you pass an int in the constructor. You could pass the object as well. If this number represent something, add this in the code below. To get a reference to a touching object, you can use this instead of the 'touching' method.
if (isTop)
{
    LetterQueue queue = (LetterQueue) getOneIntersectingObject(LetterQueue.class);
    if (queue != null)
    {
        /*reference to the stack object the 'stack' int represent*/.pop();
        queue.add(this);
    }
}
xCrunchmanx xCrunchmanx

2015/4/17

#
There are 3 stacks that hold up to 5 letters each. The letters are scattered among them when the game is started
xCrunchmanx xCrunchmanx

2015/4/18

#
How do I get reference to those so I can pop?
public class LetterStack extends Actor { Stack<Letter> stack = new Stack(); /** * Act - do whatever the LetterStack wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void drop(){ } public void act() { Level level = (Level) getWorld(); stack.peek().isTop = true; } }
Super_Hippo Super_Hippo

2015/4/18

#
What is the difference between LetterStack and Stack? When you create your letters, you probably give every letter a random int which represent the stack. You could pass the stack itself:
//outside methods
private LetterStack[] stack = new LetterStack[5];
private String[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//when creating the objects
for (int s=0; s<3; s++)
{
    stack[s] = new LetterStack();
    addObject(stack[s], 100+s*50, 500); //or wherever they have to be
}

for (int s=0; s<3; s++)
{
    for (int n=0; n<5; n++)
    {
        addObject(new Letter(alphabet[Greenfoot.getRandomNumber(26)], stack[s]), 100+s*50, 500);
    }
}
Then in the Letter class:
private String letter;
private LetterStack stack;

public Letter(String letter, LetterStack stack)
{
    this.letter = letter;
    this.stack = stack;
    setImage("Star-"+letter+".png");
}

//code from above - added the reference
if (isTop)
{
    LetterQueue queue = (LetterQueue) getOneIntersectingObject(LetterQueue.class);
    if (queue != null)
    {
        stack.pop();
        queue.add(this);
    }
}
For this, you will need the public methods 'pop' in the LetterStack class and 'add' in the LetterQueue class ( - you can rename them of course).
You need to login to post a reply.