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

2014/8/19

Connecting three objects

gdwd4 gdwd4

2014/8/19

#
I have a class called "CursorAA" which needs to connect to both "BlackTile" and "WhiteTile", but I don't know how to make a constructor that will connect to both. Below I have added some of the code that may be helpful: CursorAA()
private BlackTile blacktile;
    
    public CursorAA(BlackTile pointBlackTile)
    {
        blacktile = pointBlackTile;
    }
BlackTile() This shows what methods I would call from CursorAA
public void SetClear()
    {
        setImage("BlackClear.jpg");
    }
    
    public void SetBlackCastle()
    {
        setImage("Black_Castle Black_Tile.jpg");
    }
WhiteTile() This shows what methods I would call from CursorAA
public void SetClear()
    {
        setImage("WhiteClear.jpg");
    }
    
    public void SetBlackCastle()
    {
        setImage("Black_Castle White_Tile.jpg");
    }
World()
CursorAA cursoraa = new CursorAA(blacktile);
        addObject(cursoraa, 45, 49);
Any help would be very helpful. Ask if you think you need more of the code.
erdelf erdelf

2014/8/19

#
    private BlackTile blacktile;  
    private WhiteTile whitetile;
          
        public CursorAA(BlackTile pointBlackTile, Whitetile pointWhitetile)  
        {  
            blacktile = pointBlackTile;
            whitetile = pointWhiteTile;  
        }  
this should be what you need
gdwd4 gdwd4

2014/8/19

#
Thanks alot. I'll try that now. XD
gdwd4 gdwd4

2014/8/19

#
Ok your code worked but now I have another issue. Code from CursorAA():
public void Test()
    {
        blacktile.SetClear();
    }
So when I click on the world it runs the method but it only affects the first object generated from the class BlackTile() and not the blacktile object that I am clicking. There are many black and white tiles. How do I tell the cursor which blacktile to affect. BlackTile() code:
public void SetClear()
    {
        setImage("BlackClear.jpg");
    }
Any help would be appreciated. Ask if you need more of the code.
danpost danpost

2014/8/19

#
You initial constructor given for the CursorAA class, as well as the code given by erdelf are linking one object of the class or classes to your CursorAA object. I am unclear as to whether that is what you wanted to do. Are you trying to link the classes themselves to the CursorAA object or certain objects from the classes as you are doing above? Maybe more background as to what you are trying to achieve is necessary.
danpost danpost

2014/8/20

#
Another thing that is a little confusing (maybe because of lack of information) is why the CursorAA object needs to know which tile was clicked on. It would seem that you could have the tile itself clear its image when clicked on.
gdwd4 gdwd4

2014/8/20

#
Ok, I want the CursorAA object to be able to control the tiles. There a many black and white tiles but I want only one to change its image when the CursorAA is clicked over the tile that needs to change. CursorAA has to be the object in control as it also keeps a track of what image is being displayed and whether it can be changed depending on the rules of the game. Ask if you need further clarity please ask as I know I'm not explaining it brilliantly. Thanks for all the help!
danpost danpost

2014/8/20

#
I think I understand. You do not need the 'blacktile' and 'whitetile' fields in the 'CursorAA' class at all (like I was thinking). Do you have a subclass of Actor that both the WhiteTile and BlackTile classes extends from? if so, what is it called?
gdwd4 gdwd4

2014/8/20

#
No. Would it be better to have one? If so, how would it help. Example would be nice to clarify what you mean.
danpost danpost

2014/8/20

#
Create a subclass of Actor called 'Tile'. Change what WhiteTile and BlackTile extend from 'Actor' to 'Tile'. Remove and place all duplicated fields and methods in the two classes into the Tile class. It will help in consolidating code (all the duplicated fields and methods can be put in one place instead of two). Also, you can refer to the 'Tile' class to refer to either type of tile (you will not have to refer to both one at a time). For example, in your CursorAA class, you could say:
if (Greenfoot.mouseClicked(this))
{
    Tile tile = getOneObjectAtOffset(0, 0, Tile.class);
    if (tile == null) return; // not necessary if CursorAA object is always over a tile
    // the following validates that this tile can be clicked on at this point in the game
    if (isWhiteMove && tile instanceof BlackTile) return; //it is not blacks turn
    if (!isWhiteMove && tile instanceof Whitetile) return; // it is not whites turn
    // other validation checks you may need to do (do the rules apply that this tile can be chosen)
    // process valid click (call method in Tile class using something like tile.clickedOn();')
}
This may not even resemble what you already have and should not be considered as copy/paste code. It is just a short example to show how the Tile class, placed between Actor and the WhiteTile and BlackTile classes can help.
danpost danpost

2014/8/20

#
For the methods like 'setClear', you can keep them in their individual classes (they have different coding), but include a method declaration of them like in the following in the Tile class:
void setClear();
void setBlackCastle();
That way you can just use 'tile.setClear();' without needing to know which subclass the tile is from. The Tile class should probably be declared as 'abstract'. This will prevent the class from being instantiated directly and force methods declared like the 'setClear' method is here to be overridden in its subclasses.
gdwd4 gdwd4

2014/8/20

#
Ok, I have made a Tile.class extends from Actor and have made BlackTile.class and WhiteTile.class extend from Tile.class. BlackTile code:
public void SetClear()
    {
        setImage("BlackClear.jpg");
    }
    
    public void SetBlackCastle()
    {
        setImage("Black_Castle Black_Tile.jpg");
    }
WhiteTile code:
public void SetClear()
    {
        setImage("WhiteClear.jpg");
    }
    
    public void SetBlackCastle()
    {
        setImage("Black_Castle White_Tile.jpg");
    }
CursorAA code:
public void Clicked()
    {
        if(Greenfoot.mouseClicked(this))
        {
            //Locate tile.
            //Other methods I will add.
            //Example:
            //Tell BlackTile that the mosue has clicked to SetBlackCastle.
        }
    }
There is currently no code in Tile.class. What code do I need to put into Tile.class to be able to do what is commented in CursorAA's code?
danpost danpost

2014/8/20

#
Your Tile class should at least look like this now:
import greenfoot.*;

public abstract class Tile extends Actor
{
    public Tile()
    {
    }

    void setClear();

    void setBlackCastle();
}
The 10-line sample code I gave above should help in providing what you need for the 'Clicked' method (at least for your commented lines 5 and 8).
gdwd4 gdwd4

2014/8/20

#
ok the code in Tile.class works but now I'm having an error when I try to use your code from a previous post. Code:
Tile tile = getOneObjectAtOffset(0, 0, Tile.class);  
            if (tile == null) return;
            tile.SetBlackCastle();
Error message: "incompatible types" highlighting "(0, 0, Tile.class);"
danpost danpost

2014/8/20

#
gdwd4 wrote...
ok the code in Tile.class works but now I'm having an error when I try to use your code from a previous post. < Code Omitted > Error message: "incompatible types" highlighting "(0, 0, Tile.class);"
Ok, I missed that. The object has to be cast as a Tile object before setting it to a variable that can only hold a Tile object. Change it to this:
Tile tile = (Tile) getOneObjectAtOffset(0, 0, Tile.class);
You need to login to post a reply.