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

2012/1/6

Why won't my game drag cards?

kiarocks kiarocks

2012/1/6

#
I was fiddling with my solitaire game when the cards suddenly stopped dragging! Any idea why this happened?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Card here.
 * 
 * @author (Kenzie Togami) 
 * @version (12/20/2011)
 */
public class Card extends Actor
{
    CardPics pics = new CardPics();
    private static final Card[] EMPTY_CARD_ARRAY = new Card[0];
    boolean sideShown = false;
    int value = 0;
    int startx,starty;
    String color;
    String suit = "";
    boolean onPile = false;
    SoliWorld sw = null;
    Card cardAbove;
    Card temp;
    CardPile myPile;
    boolean isDraggable = true;
    boolean dragging;
    boolean location;
    boolean stopOnReAdd;
    boolean dragStack;
    boolean onStack;
    boolean ignoreMouse;
    List<Card> dragThese = new ArrayList<Card>();
    int posonstack;
    int cardsinstack;
    int flippos;
    Pad pad = null;
    Pad prevpad;
    public Card()
    {
        sideShown = false;
    }

    public Card(boolean rightSideUp, int value, String suit, boolean fromDeck)
    {
        this.value = value;
        this.suit = suit;
        if(suit == "diamonds" || suit =="hearts")
        {
            color = "red";
        }
        else if(suit == "spades" || suit =="clubs")
        {
            color = "black";
        }
        if(rightSideUp)
        {
            setImage(pics.getImage(value,suit));
        }
        sideShown = rightSideUp;
        location = fromDeck;
    }

    public Card(boolean rightSideUp, int value, String suit, boolean fromDeck, int posinflip)
    {
        this.value = value;
        this.suit = suit;
        if(suit == "diamonds" || suit =="hearts")
        {
            color = "red";
        }
        else if(suit == "spades" || suit =="clubs")
        {
            color = "black";
        }
        if(rightSideUp)
        {
            setImage(pics.getImage(value,suit));
        }
        sideShown = rightSideUp;
        location = fromDeck;
        flippos = posinflip;
    }

    public void flip(boolean side)
    {
        sideShown = side;
        if(sideShown == false)
        {
            setImage(pics.getImage(0,""));
        }
        else
        {
            setImage(pics.getImage(value,suit));
        }
    }

    protected void addedToWorld(World world)
    {
        if(getWorld() instanceof SoliWorld)
        {
            sw = (SoliWorld) getWorld();
        }
        if(!stopOnReAdd && !location)
        {
            pad = sw.getPad(getX());
            pad.addCard(this);
            stopOnReAdd = true;
        }
    }

    /**
     * Act - do whatever the Card wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        isDraggable = sideShown;
        if(!dragging)
        {
            checkDrag();
            if(dragStack) moveStack();
        }
    }  

    public void moveStack()
    {
        for(int i = 0; i < dragThese.size(); i++)
        {
            if(dragThese.get(i) != this) dragThese.get(i).setLocation(getX(),((i+1)*20)+getY());
        }
    }

    public void checkDrag()
    {
        int clicks = 0;
        if(isDraggable && Greenfoot.mouseClicked(this) && !dragging && !sw.cardDragging)
        {
            if(pad != null && !sw.cardDragging) dragStack = drag();
            dragging = true;
            sw.cardDragging = true;
            startx = getX();
            starty = getY();
            reAdd();
            if(pad != null) pad.removeCard(this);
            if(pad != null) prevpad = pad;
            clicks++;
        }
        else if(dragging)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null)
                setLocation(mouse.getX(),mouse.getY());

        }
        if(dragging && Greenfoot.mouseClicked(this) && clicks == 0)
        {
            dragging = false;
            sw.cardDragging = false;
            if(!onValidCard() && !onValidPile() && !onValidPad())
            {
                setLocation(startx,starty);
                pad = prevpad;
                if(pad != null) pad.addCard(this);
            }
            if(onValidCard() && !onValidPile())
            {
                Card landing = (Card) getOneIntersectingObject(Card.class);
                pad = landing.pad;
                setLocation(pad.topCard.getX(),pad.topCard.getY()+20);
                pad.addCard(this);
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                    if(Deck.lastCardGiven < 0)Deck.lastCardGiven--;
                }
            }
            if(onValidPile())
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                    if(Deck.lastCardGiven < 0)Deck.lastCardGiven--;
                }
            }
            if(onValidPad())
            {
                Pad p = (Pad) getOneIntersectingObject(Pad.class);
                pad = p;
                pad.addCard(this);
                setLocation(pad.getX(),pad.getY());
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                    if(Deck.lastCardGiven < 0)Deck.lastCardGiven--;
                }
            }
            if(myPile != null)
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                    if(Deck.lastCardGiven < 0) Deck.lastCardGiven--;
                }
            }
        }
    }

    public boolean drag()
    {
        if(pad.getStack(this) != null || !pad.getStack(this).isEmpty())
        {
            dragThese = pad.getStack(this);
            if(dragThese.size() > 0) return true;
            return false;
        }
        return false;
    }

    public boolean onValidPad()
    {
        Pad p = (Pad) getOneIntersectingObject(Pad.class);
        if(p != null)
        {
            if(p.topCard == null && value == 13)
            {

                return true;
            }
            else return false;
        }
        else return false;
    }

    public boolean onValidPile()
    {
        CardPile pile = (CardPile) getOneIntersectingObject(CardPile.class);
        if(pile != null)
        {
            return pile.tryCardPlace(this);
        }
        else { return false; }
    }

    public boolean onValidCard()
    {
        Card landing = (Card) getOneIntersectingObject(Card.class);
        if(landing != null)
        {
            if(landing.pad != null)
            {
                return checkOurSuitAndVal(landing.pad.topCard);
            }
            else { return false; }
        }
        else { return false; }
    }

    public boolean checkOurSuitAndVal(Card card)
    {
        if(card != null)
        {
            if(color == "red")
            {
                if(card.color == "black" && card.value == value+1) { return true; } else { return false; }
            }
            else if(color == "black")
            {
                if(card.color == "red" && card.value == value+1) { return true; } else { return false; }
            }
            else
                return false;
        }
        else return false;
    }

    public void addCard()
    {
        pad.addCard(this);
    }

    public void setPad(Pad ourpad)
    {
        pad = ourpad;
    }

    public Pad getPad()
    {
        return pad;
    }

    public boolean verify(CardPile pile)
    {
        if(pile != null) { myPile = pile; return myPile != null; }
        else return false;
    }

    public boolean setTopCard()
    {
        if(pad != null) pad.topCard = this;
        if(pad == null) return false; else return true;
    }

    public void reAdd()
    {
        int x = getX();
        int y = getY();
        int rotation = getRotation();
        World world = getWorld();
        world.removeObject(this);
        world.addObject(this, x, y);
    }
}
davmac davmac

2012/1/6

#
I don't know exactly, but, firstly, your code would benefit from some comments - it's quite hard to understand what some parts of it are actually doing (or meant to be doing). Also, you have string comparisons using '==' which should be avoided: if(color == "red") { etc. (use .equals() instead).
kiarocks kiarocks

2012/1/6

#
Ok, i will fix that, but i figured this problem out: i told it not to check to drag if it was dragging. Whoops!!!
You need to login to post a reply.