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

2020/4/11

Having trouble adding objects to MyWorld from an actor subclass

bubbadawg bubbadawg

2020/4/11

#
Hello I'm having trouble adding objects to MyWorld from a method, updateTileValues() that is called in act() for an actor subclass. It compiles ok and works fine in theory, I tested with my print statements in the method. Any thoughts? Thanks!
    public void updateTileValues()
    {
        for (int r = 0; r < MyWorld.tileValues.length; r++)
        {
            for (int c = 0; c < MyWorld.tileValues[r].length; c++)
            {
                System.out.print(""+MyWorld.tileValues[r][c].getValue()+"\t");
                String theValueStr = MyWorld.tileValues[r][c].getValueAsString();
                int theXCoord = MyWorld.tileValues[r][c].getXCoord();
                int theYCoord = MyWorld.tileValues[r][c].getYCoord();
                getWorld().removeObject(MyWorld.tileValues[r][c]);
                getWorld().addObject(new TileValue(theXCoord, theYCoord, theValueStr), theXCoord, theYCoord);
            }
            System.out.print("\n");
        }
    }
danpost danpost

2020/4/11

#
bubbadawg wrote...
I'm having trouble adding objects to MyWorld from a method, updateTileValues() that is called in act() for an actor subclass. It compiles ok and works fine in theory, I tested with my print statements in the method. Any thoughts? << Code Omitted >>
How does the output of your print statements compare to what you think they should be? Give the name of the class that this method is in and provide the act method codes.
bubbadawg bubbadawg

2020/4/11

#
The act() method is in my Board class, one of the Actor subclasses.
public void act() 
    {
        if (Greenfoot.isKeyDown("up") && MyWorld.isTimeToMove == true)
        {
            moveUp();
            updateTileValues();
        }
        
        if (Greenfoot.isKeyDown("down") && MyWorld.isTimeToMove == true)
        {
            moveDown();
            updateTileValues();
        }
        
        if (Greenfoot.isKeyDown("right") && MyWorld.isTimeToMove == true)
        {
            moveRight();
            updateTileValues();
        }
        
        if (Greenfoot.isKeyDown("left") && MyWorld.isTimeToMove == true)
        {
            moveLeft();
            updateTileValues();
        }
        
        if (MyWorld.isTimeToMove == false)
        {
            addNewTileValue();
            updateTileValues();
            MyWorld.isTimeToMove = true;
        }
    }
My print statements have the board being filled with random values, like I intend, but my attempts to add them visually to the board compile but do not have them added to the screen.
danpost danpost

2020/4/12

#
bubbadawg wrote...
My print statements have the board being filled with random values, like I intend, but my attempts to add them visually to the board compile but do not have them added to the screen.
What does addNewTileValue do (show code)? Also, show TileValue class codes.
bubbadawg bubbadawg

2020/4/12

#
addNewTileValue() adds either a 1 or 3 in one random spot in the 2d Array of tileValues.
public void addNewTileValue()
    {
        int numOfZeros = 0;
        for (int r = 0; r < MyWorld.tileValues.length; r++)
        {
            for (int c = 0; c < MyWorld.tileValues[r].length; c++)
            {
                 if (MyWorld.tileValues[r][c].getValue() == 0)
                 {
                     numOfZeros++;
                 }
            }
        }
        int indexOfNumToChange = (int)(Math.random()*numOfZeros); //if 3 zeros, index is between 0 and 2
        int count = 0;
        for (int r = 0; r < MyWorld.tileValues.length; r++)
        {
            for (int c = 0; c < MyWorld.tileValues[r].length; c++)
            {
                 if(count == indexOfNumToChange)
                 {
                     MyWorld.tileValues[r][c].setValue(oneOrThree());
                 }
                 count++;
            }
        }
    }
Here is my tileValue class:
public class TileValue extends Actor
{
    private int xCoord;
    private int yCoord;
    private String valueAsString;
    private int value;
    
    public TileValue()
    {
        
    }
    
    public TileValue(int theXCoord, int theYCoord, String theValueAsString)
    {
        xCoord = theXCoord;
        yCoord = theYCoord;
        valueAsString = theValueAsString;
        if (theValueAsString.equals("   "))
        {
            value = 0;
        }
        else
        {
            value = Integer.parseInt(theValueAsString);
        }
        setImage(new GreenfootImage(theValueAsString, 50, Color.BLACK, Color.YELLOW, null));
    }
    
    public int getXCoord()
    {
        return xCoord;
    }
    
    public int getYCoord()
    {
        return yCoord;
    }
    
    public int getValue()
    {
        return value;
    }
    
    public void setValue(int theValue)
    {
        value = theValue;
    }
    
    public String getValueAsString()
    {
        return valueAsString; 
    }
    
    public void act() 
    {
        // Add your action code here.
    }
Thanks for the help danpost!
danpost danpost

2020/4/12

#
Do you populate the entire world with these TileValue objects?
bubbadawg bubbadawg

2020/4/12

#
Yes. In the constructor for my World subclass, MyWorld, I have this nested for loop to populate each 2d array spot with the int value 0. Each spot displays how I expect as a result of this nested for loop.
for (int r = 0; r < tileValues.length; r++)
        {
            for (int c = 0; c < tileValues[r].length; c++)
            {
                tileValues[r][c] = new TileValue(theXCoord, theYCoord, "   ");
                addObject(tileValues[r][c], tileValues[r][c].getXCoord(), tileValues[r][c].getYCoord());
                theXCoord += 92;
            }
            theXCoord = 470;
            theYCoord += 92;
        }
danpost danpost

2020/4/12

#
If each and every location has a tile, then I wonder why you create more of them in your updateTileValues method and add them into your already populated world. You should just change the values that each tile contains. And if the tiles do not move, I wonder why you pass their location coordinates to the tiles when using getX and getY would obtain those values.
bubbadawg bubbadawg

2020/4/13

#
Is there a method where I can update the string parameter of a GreenfootImage? What I tried was removing the existing tileValue and putting another one in the same place but with the new string.
danpost danpost

2020/4/13

#
bubbadawg wrote...
Is there a method where I can update the string parameter of a GreenfootImage? What I tried was removing the existing tileValue and putting another one in the same place but with the new string.
You want to use the tileValue that is already there and just change the text and image. You need to factor in an updateImage method. Move and modify the imaging code in the constructor to the new method. Call the method from the constructor and from the method that has the text change.
You need to login to post a reply.