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

2013/12/23

My image won’t rotate.

Victor0409 Victor0409

2013/12/23

#
I have a tank but my image is in the wrong way. The image has to turn 90 degrees to the left. But I want that the actor keeps heading the same way. This is the code so far. /** * Creates the Tank and makes sure everything is all right. */ public TankBase() { tankBaseAmount++; GreenfootImage.rotate(90); } /** * Act - do whatever the TankBase wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { drive(); if (tankTurretAmount < tankBaseAmount) { getWorld().addObject (new TankTurret(), getX()+1, getY()); tankTurretAmount = tankTurretAmount + 1; } }
erdelf erdelf

2013/12/23

#
the line
1
GreenfootImage.rotate(90);
should be changed to
1
getImage().setRotation(90);
Victor0409 Victor0409

2013/12/23

#
Then I get an compiling error: cannot find symbol - method setRotation(int) and the „.setRotation” is hi lighted in red
danpost danpost

2013/12/23

#
I believe erdelf meant:
1
getImage().rotate(90);
Victor0409 Victor0409

2013/12/23

#
But then my image gets a square and gets cut of how can I „repair” this? This code won’t help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class TankBase here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TankBase extends Actor
{
    int tankBaseAmount;
    int tankTurretAmount;
     
    /**
     * Creates the Tank and makes sure everything is all right.
     */
    public TankBase()
    {
        tankBaseAmount++;
        getImage().drawRect(getX(), getY(), 200, 200);
        getImage().rotate(90);
    }
 
    /**
     * Act - do whatever the TankBase wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        drive();
        if (tankTurretAmount < tankBaseAmount)
        {
            getWorld().addObject (new TankTurret(), getX()+1, getY());
            tankTurretAmount = tankTurretAmount + 1;
        }
    }
danpost danpost

2013/12/23

#
That code does not look like it would compile without error. You cannot use 'getX' or 'getY' on an actor object that is being constructed. You should probably avoid all the rotation problems by using an image editor and saving the rotated image (facing right). You could actually do this programmatically; but there would be several steps involved: (1) determining largest dimension and creating a blank image with width and height of that length (2) drawing the image on the blank (centered) and then rotating the image (3) creating a blank image of original size with dimensions switch (4) drawing the rotated image properly onto the image created in the last step (centered)
Victor0409 Victor0409

2013/12/23

#
I’ll try to find an image editor
You need to login to post a reply.