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

2020/3/13

Make actor bigger

108089 108089

2020/3/13

#
Hi, i'm working on a game, inspired by Subway Surfers. Now i'm trying to make it look like the obstacles come closer to you, I tried to do this by moving the actor while scaling up the image. But it doesn't work like I hoped it would. Instead it makes the image totaly different by (I guess) just adding pixels in the middle. edit: what I descriped above is from a previous code I've tried. Now Greenfoot just stops if a car spawns in. (they do or don't spawn in based on a random generated number, every 0,5 sec) This is the code:
public auto()
    {
        GreenfootImage image = getImage();
        image.scale(86, 52);

    }

    /**
     * Act - do whatever the auto wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        setLocation(getX() - 1, getY() + 2); //move the car
        
        double X = (getY()/(52/30)); //make a variable based on the Y-value and the ratio it must be scaled up by
        double Y = (getY()/(86/30));
        int A = (int) X; 
        int B = (int) Y;
        GreenfootImage image = new GreenfootImage("auto.jpeg");
        image.scale(A, B);
        setImage(image);

        if (isAtEdge()) {
            getWorld().removeObjects(getWorld().getObjects(auto.class));

        }
    }    
I hope someone can help me out!
danpost danpost

2020/3/14

#
Save the initial image in a field
private GreenfootImage baseImage;
Assign its image in the constructor:
baseImage = getImage();
Create a new instance of that image:
GreenfootImage image = new GreenfootImage(baseImage);
to scale each time.
108089 108089

2020/3/15

#
Thanks for the reply, but now I have your code implemented in my code it again adds pixels in the middle. You can see how in the picture below. See my code down below aswell. Link to photo
private GreenfootImage baseImage;
    public void act() 
    {
        setLocation(getX() - 1, getY() + 2);
        
        double X = (getY()/(52/30));
        double Y = (getY()/(86/30));
        int A = (int) X;
        int B = (int) Y;
        baseImage = getImage();
        GreenfootImage image = new GreenfootImage(baseImage);
        image.scale(A, B);
        setImage(image);

        if (isAtEdge()) {
            getWorld().removeObjects(getWorld().getObjects(auto.class));

        }
    }    
danpost danpost

2020/3/15

#
Line 10 is misplaced. It needs to be in a constructor block. Like this:
public auto()
{
    baseImage = getImage();
}
The problem with it being in the act method is that you assign it an already scaled image and multiple scaling causes distortions. Your line 16 can simply be this:
getWorld().removeObject(this);
You need to login to post a reply.