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

2013/4/26

Getting atribute of actor by ID

CplShephard CplShephard

2013/4/26

#
I want to get the ID of an actor, here is the class and constructor (I'm making the Hanoi game btw, and i want to know the ID or Value of the piece to put in the pole, to compare to the last piece, to verify if it can be put in there)
public class Piece extends Actor
{
    int valueId;
    boolean movable;
    public Piece(int valueId,String image)
    {
        GreenfootImage img = new GreenfootImage(image);
        this.setImage(img);
        this.valueId = valueId;
    }
}
This is how i make them:
private void prepare()
    {
        Palo palo1 = new Palo(1);
        addObject(palo1, 150, 220);
        Palo palo2 = new Palo(2);
        addObject(palo2, 300, 220);
        Palo palo3 = new Palo(3);
        addObject(palo3, 450, 220);

        Pieza pieza1 = new Pieza(1,"1.png");
        addObject(pieza1, 150, 220);
        Pieza pieza2 = new Pieza(2,"2.png");
        addObject(pieza2, 150, 200);
}
and in anothe class i have:
public boolean validaPush(Pieza pieza){
        boolean returnValue;
        if(stackPieza.get(stackPieza.size()) != null &&  stackPieza.get(stackPieza.size()) > pieza.valueId){
            returnValue = true;
        }else{
            returnValue = false;
        }
        return returnValue;
    }
CplShephard CplShephard

2013/4/26

#
Piece is Pieza... wanted to translate the language on variables so it's easier to understand
CplShephard CplShephard

2013/4/26

#
i think a workaround is to make a subclass of Actor for each Piece... and use that class for intersections and check for every object... but that's too stupid and inefficient in POO, given that all pieces are of the same class.
CplShephard CplShephard

2013/4/26

#
Nevermind, in the line3 of the last code example the "stackPieza" is a list of Pieza, so... so just needed to add ".valueId"
You need to login to post a reply.