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

2013/2/25

Making an object bigger

Gingervitis Gingervitis

2013/2/25

#
Is there an easy way to have an object move across the screen gradually getting bigger?
Jonas234 Jonas234

2013/2/25

#
you could use the Position of your Object and then scale() (in GreenfootImage). The size would be sth like the right side of your world minus the position of your object and that times a factor.
Gingervitis Gingervitis

2013/2/26

#
I tried scaling it, but it didn't work. I think I messed up the code.
 public void ifInIntro()
    {
        World world;
        world = getWorld();
        if (world instanceof Intro)
        {
            GreenfootImage image = getImage();
            image.scale(5, 5);
        }
    }
Jonas234 Jonas234

2013/2/26

#
public void ifInIntro()  
   {  
       World world;  
       world = getWorld();  
       if (world instanceof Intro)  
       {  
           GreenfootImage image = getImage();  
           int percentage =  (getX()*100 /world.getWidth) ;
          int scale =5*percentage;
           image.scale(scale, scale);  
       }  
   }  
If you want it to get bigger while approaching the right side something like that should be working. You need to get a value (scale) which is dependent on the position of your Object. Jonas
danpost danpost

2013/2/26

#
If minSize is the smallest size at left side of screen and maxSize is the largest size at right side of screen then
double factor = (double)getX()/(double)getWorld().getWidth();
int size = minSize + (int)(factor*(maxSize-minSize));
GreenfootImage image = new GreenfootImage(baseImage);
image.scale(size, size);
setImage(image);
It is neccessary to have line 3 in the code because scaling the same image multiple times will stretch the image non-uniformly. You can replace 'baseImage' with a filename or a reference name to the initial image before scaling.
Gingervitis Gingervitis

2013/2/27

#
Thank you very much for the help.
You need to login to post a reply.