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

2022/2/3

how do I set an actors rotation to another actors rotation?

ThatsCrispy ThatsCrispy

2022/2/3

#
I am trying to set an actors rotation when it is added to the world to the same rotation as another actor. Whatever I do, it doesn't work (the rotation doesn't change) Please help The world is called Background, and the 2 actors are called Corridor and Tile. I want to set the Tile actor to the same rotation as the Corridor actor. The code in the World is:
  int tileSize = 50;
    private Tile theTile;

    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 800, 1); 
        theTile = new Tile();
        prepare();
    }
    public Tile getTile()
    {
        return theTile;
    }
The code in the Corridor is:
public void drawMaze()
    {
        int randomRotation = 90*Greenfoot.getRandomNumber(4);
        setRotation(randomRotation);
        
        Tile tile = new Tile();
        getWorld().addObject(tile, getX(), getY());
        
        Background background = (Background)getWorld();
        Tile tileDIR = background.getTile();
        int currentRotation = getRotation();
        tileDIR.copyRotation(currentRotation);
        
        int tileSize = ((Background)getWorld()).returnTileSize();
        move(tileSize);
    }
And the code in the tile is:
public void copyRotation(int corridorRotation)
    {
        setRotation(corridorRotation);
    }
Sorry if this is formated badly, i'm new to this stuff. Thanks
Super_Hippo Super_Hippo

2022/2/3

#
Tile tile = new Tile();
getWorld().addObject(tile, getX(), getY());
With this code, you add the object ‘tile’ into the world. So you have a reference to the object you added. You just need to call the ‘setRotation’ method on that object.
Tile tile = new Tile();
getWorld().addObject(tile, getX(), getY());
tile.setRotation(getRotation());
The reason why your code doesn’t work is that the ‘theTile’ in Background is not the same object as the ‘tile’ in Corridor. You can notice that because both are created with ‘new’, so they can’t be the same object.
danpost danpost

2022/2/4

#
Where is the method drawMaze (in the Corridor class) called from? Show code block.
You need to login to post a reply.